Skip to content

Commit d8fda07

Browse files
authored
Merge pull request #19 from pettermahlen/any-redacted
permit any annotation named Redacted when redacting
2 parents ab188c0 + 6a3606a commit d8fda07

5 files changed

Lines changed: 75 additions & 16 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ if (message.isLogin()) {
255255
```java
256256
dataenum_case UserInfo(String name, @Redacted String password);
257257
```
258+
We provide an annotation in the runtime dependencies, but any annotation named `Redacted` will work.
258259

259260
## Configuration
260261

dataenum-integration-test/src/test/java/com/spotify/dataenum/it/OuterIntegrationTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,9 @@ public void shouldRedactAnnotatedFieldsInToString() {
9494
.contains("i want to see this")
9595
.doesNotContain("866");
9696
}
97+
98+
@Test
99+
public void shouldSupportAnyRedactedAnnotation() {
100+
assertThat(RedactedTesting.redactMe("hide this").toString()).doesNotContain("hide this");
101+
}
97102
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* -\-\-
3+
* DataEnum
4+
* --
5+
* Copyright (c) 2017 Spotify AB
6+
* --
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* -/-/-
19+
*/
20+
package com.spotify.dataenum.it;
21+
22+
import java.lang.annotation.ElementType;
23+
import java.lang.annotation.Retention;
24+
import java.lang.annotation.RetentionPolicy;
25+
import java.lang.annotation.Target;
26+
27+
/**
28+
* Proprietary 'Redacted' annotation to ensure that you can use any - this helps avoiding conflicts
29+
* with auto-value-redacted, for instance.
30+
*/
31+
@Retention(RetentionPolicy.SOURCE)
32+
@Target(ElementType.PARAMETER)
33+
public @interface Redacted {}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* -\-\-
3+
* DataEnum
4+
* --
5+
* Copyright (c) 2017 Spotify AB
6+
* --
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* -/-/-
19+
*/
20+
package com.spotify.dataenum.it;
21+
22+
import com.spotify.dataenum.DataEnum;
23+
import com.spotify.dataenum.dataenum_case;
24+
25+
@DataEnum
26+
interface RedactedTesting_dataenum {
27+
28+
dataenum_case RedactMe(@Redacted String foo);
29+
}

dataenum-processor/src/main/java/com/spotify/dataenum/processor/parser/ValueParser.java

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
*/
2020
package com.spotify.dataenum.processor.parser;
2121

22-
import com.spotify.dataenum.Redacted;
2322
import com.spotify.dataenum.dataenum_case;
2423
import com.spotify.dataenum.function.Function;
2524
import com.spotify.dataenum.processor.data.Parameter;
@@ -90,8 +89,7 @@ static Value parse(Element element, ProcessingEnvironment processingEnv) {
9089
TypeName parameterType = TypeName.get(parameterElement.asType());
9190

9291
boolean nullable = isAnnotationPresent(parameterElement, ValueParser::isNullableAnnotation);
93-
boolean redacted =
94-
isAnnotationPresent(parameterElement, ValueParser.isRedactedAnnotation(processingEnv));
92+
boolean redacted = isAnnotationPresent(parameterElement, ValueParser::isRedactedAnnotation);
9593
Element parameterTypeElement =
9694
processingEnv.getTypeUtils().asElement(parameterElement.asType());
9795
boolean isEnum =
@@ -104,17 +102,6 @@ static Value parse(Element element, ProcessingEnvironment processingEnv) {
104102
return new Value(valueSimpleName, parameters);
105103
}
106104

107-
private static Function<AnnotationMirror, Boolean> isRedactedAnnotation(
108-
ProcessingEnvironment processingEnv) {
109-
final Types types = processingEnv.getTypeUtils();
110-
final Elements elements = processingEnv.getElementUtils();
111-
112-
return annotationMirror ->
113-
types.isSameType(
114-
annotationMirror.getAnnotationType(),
115-
elements.getTypeElement(Redacted.class.getCanonicalName()).asType());
116-
}
117-
118105
private static boolean isAnnotationPresent(
119106
VariableElement parameterElement, Function<AnnotationMirror, Boolean> criterion) {
120107
for (AnnotationMirror annotation : parameterElement.getAnnotationMirrors()) {
@@ -135,7 +122,11 @@ private static boolean isValueSpecMarker(
135122
}
136123

137124
private static boolean isNullableAnnotation(AnnotationMirror annotation) {
138-
Element annotationElement = annotation.getAnnotationType().asElement();
139-
return "Nullable".contentEquals(annotationElement.getSimpleName());
125+
return "Nullable".contentEquals(annotation.getAnnotationType().asElement().getSimpleName());
126+
}
127+
128+
private static boolean isRedactedAnnotation(AnnotationMirror annotationMirror) {
129+
return "Redacted"
130+
.contentEquals(annotationMirror.getAnnotationType().asElement().getSimpleName());
140131
}
141132
}

0 commit comments

Comments
 (0)