diff --git a/sourcegen-annotations/src/main/java/io/micronaut/sourcegen/annotations/Secret.java b/sourcegen-annotations/src/main/java/io/micronaut/sourcegen/annotations/Secret.java new file mode 100644 index 00000000..03edf98c --- /dev/null +++ b/sourcegen-annotations/src/main/java/io/micronaut/sourcegen/annotations/Secret.java @@ -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 { +} diff --git a/sourcegen-generator/src/main/java/io/micronaut/sourcegen/generator/visitors/UtilsAnnotationVisitor.java b/sourcegen-generator/src/main/java/io/micronaut/sourcegen/generator/visitors/UtilsAnnotationVisitor.java index fd8aa6fd..5654ec35 100644 --- a/sourcegen-generator/src/main/java/io/micronaut/sourcegen/generator/visitors/UtilsAnnotationVisitor.java +++ b/sourcegen-generator/src/main/java/io/micronaut/sourcegen/generator/visitors/UtilsAnnotationVisitor.java @@ -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; @@ -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 @@ -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, diff --git a/test-suite-java/src/main/java/io/micronaut/sourcegen/example/Elephant.java b/test-suite-java/src/main/java/io/micronaut/sourcegen/example/Elephant.java new file mode 100644 index 00000000..f0bda3ec --- /dev/null +++ b/test-suite-java/src/main/java/io/micronaut/sourcegen/example/Elephant.java @@ -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); + } +} diff --git a/test-suite-java/src/test/java/io/micronaut/sourcegen/example/PersonUtilsTest.java b/test-suite-java/src/test/java/io/micronaut/sourcegen/example/UtilsTest.java similarity index 92% rename from test-suite-java/src/test/java/io/micronaut/sourcegen/example/PersonUtilsTest.java rename to test-suite-java/src/test/java/io/micronaut/sourcegen/example/UtilsTest.java index a6dd8bed..2af91278 100644 --- a/test-suite-java/src/test/java/io/micronaut/sourcegen/example/PersonUtilsTest.java +++ b/test-suite-java/src/test/java/io/micronaut/sourcegen/example/UtilsTest.java @@ -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() { @@ -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});