Skip to content

Commit

Permalink
add @secret for @tostring
Browse files Browse the repository at this point in the history
  • Loading branch information
elifKurtay committed Sep 25, 2024
1 parent ec7c055 commit 17f6e2d
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2017-2021 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.sourcegen.annotations;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
* The annotation to be used with {@link ToString} on a property to hide the value from being printed.
*
* @author Elif Kurtay
* @since 1.3
*/

@Retention(RUNTIME)
@Target({FIELD, PARAMETER})
public @interface Secret {
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.micronaut.inject.visitor.VisitorContext;
import io.micronaut.sourcegen.annotations.Equals;
import io.micronaut.sourcegen.annotations.HashCode;
import io.micronaut.sourcegen.annotations.Secret;
import io.micronaut.sourcegen.annotations.ToString;
import io.micronaut.sourcegen.generator.SourceGenerator;
import io.micronaut.sourcegen.generator.SourceGenerators;
Expand All @@ -36,9 +37,9 @@
* The visitor that generates the Utils class of a bean.
* The Utils class can have functions substituting toString, equals, and hashcode.
* However, each method needs to be annotated to be generated.
* \@ToString annotation for toString function
* \@Equals annotation for equals function
* \@HashCode annotation for hashCode function
* @link ToString annotation for toString function
* @link Equals annotation for equals function
* @link HashCode annotation for hashCode function
*
* @author Elif Kurtay
* @since 1.3
Expand Down Expand Up @@ -148,7 +149,8 @@ private static void createToStringMethod(ClassDef.ClassDefBuilder classDefBuilde
for (int i = 0; i < properties.size(); i++) {
PropertyElement beanProperty = properties.get(i);
TypeDef propertyTypeDef = TypeDef.of(beanProperty.getType());
ExpressionDef.CallInstanceMethod thisProperty = thisVariable
ExpressionDef thisProperty = (beanProperty.hasAnnotation(Secret.class)) ?
ExpressionDef.constant("******") : thisVariable
.invoke(
"get" + StringUtils.capitalize(beanProperty.getSimpleName()),
propertyTypeDef,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2017-2023 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.sourcegen.example;

import io.micronaut.sourcegen.annotations.Secret;
import io.micronaut.sourcegen.annotations.ToString;

@ToString
public class Elephant {
@Secret
private String name;

public Elephant(String name) {
this.name = name;
}

public String getName() {
return name;
}

public String toString() {
return ElephantUtils.toString(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class PersonUtilsTest {
public class UtilsTest {

@Test
public void testToString() {
Expand All @@ -32,6 +32,15 @@ public void testToString() {
assertEquals("Person4[id=123, title=MR, name=Cédric, bytes=[1, 2, 3]]", person.toString());
}

@Test
public void testToStringWithSecret() {
var elephant = new Elephant("Daisy");

assertNotNull(ElephantUtils.toString(elephant));
assertTrue(elephant.toString().contains("Elephant["));
assertEquals("Elephant[name=******]", elephant.toString());
}

@Test
public void testEqualsWithCorrectObjects() {
var person = new Person4(123L, Person4.Title.MR,"Cédric", new byte[]{1,2,3});
Expand Down

0 comments on commit 17f6e2d

Please sign in to comment.