Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: java

jdk:
- oraclejdk11
- openjdk11

script:
- mvn -Pcoverage clean verify
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package com.spotify.dataenum.processor.data;

import com.spotify.dataenum.processor.util.Iterables;
import com.squareup.javapoet.AnnotationSpec;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.ParameterizedTypeName;
import com.squareup.javapoet.TypeName;
Expand All @@ -33,18 +34,21 @@ public class OutputValue {
private final String name;
@Nullable private final String javadoc;
private final Iterable<Parameter> parameters;
private final Iterable<AnnotationSpec> annotations;

public OutputValue(
ClassName outputClass,
String name,
String javadoc,
Iterable<Parameter> parameters,
Iterable<TypeVariableName> typeVariables) {
Iterable<TypeVariableName> typeVariables,
Iterable<AnnotationSpec> annotations) {
this.outputClass = outputClass;
this.name = name;
this.javadoc = javadoc;
this.parameters = parameters;
this.typeVariables = typeVariables;
this.annotations = annotations;
}

public ClassName outputClass() {
Expand Down Expand Up @@ -84,4 +88,8 @@ public Iterable<Parameter> parameters() {
public boolean hasParameters() {
return !Iterables.isEmpty(parameters());
}

public Iterable<AnnotationSpec> annotations() {
return annotations;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,25 @@
package com.spotify.dataenum.processor.data;

import com.spotify.dataenum.processor.util.Iterables;
import com.squareup.javapoet.AnnotationSpec;
import javax.annotation.Nullable;

/** Represents one of the possible values of a dataenum spec. */
public class Value {
private final String simpleName;
@Nullable private final String javadoc;
private final Iterable<Parameter> parameters;
private final Iterable<AnnotationSpec> annotations;

public Value(String simpleName, String javadoc, Iterable<Parameter> parameters) {
public Value(
String simpleName,
String javadoc,
Iterable<Parameter> parameters,
Iterable<AnnotationSpec> annotations) {
this.simpleName = simpleName;
this.javadoc = javadoc;
this.parameters = parameters;
this.annotations = annotations;
}

public String name() {
Expand All @@ -50,4 +57,8 @@ public Iterable<Parameter> parameters() {
public boolean hasParameters() {
return !Iterables.isEmpty(parameters());
}

public Iterable<AnnotationSpec> annotations() {
return annotations;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ static OutputValue create(Value value, ClassName specOutputClass, Spec spec)
parameters.add(parameterWithoutDataEnumSuffix(parameter));
}

return new OutputValue(outputClass, value.name(), value.javadoc(), parameters, typeVariables);
return new OutputValue(
outputClass, value.name(), value.javadoc(), parameters, typeVariables, value.annotations());
}

private static Parameter parameterWithoutDataEnumSuffix(Parameter parameter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.spotify.dataenum.processor.data.OutputSpec;
import com.spotify.dataenum.processor.data.OutputValue;
import com.spotify.dataenum.processor.data.Parameter;
import com.squareup.javapoet.AnnotationSpec;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.MethodSpec.Builder;
import com.squareup.javapoet.ParameterSpec;
Expand Down Expand Up @@ -56,6 +57,10 @@ public MethodSpec createFactoryMethod(OutputSpec spec) {
spec.specClass(),
value.name());

for (AnnotationSpec annotationSpec : value.annotations()) {
factory.addAnnotation(annotationSpec);
}

StringBuilder newString = new StringBuilder();
List<Object> newArgs = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,17 @@
import com.spotify.dataenum.function.Function;
import com.spotify.dataenum.processor.data.Parameter;
import com.spotify.dataenum.processor.data.Value;
import com.squareup.javapoet.AnnotationSpec;
import com.squareup.javapoet.AnnotationSpec.Builder;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.TypeName;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
import javax.annotation.processing.Messager;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
Expand All @@ -37,6 +42,7 @@
import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;
import javax.tools.Diagnostic;
import javax.tools.Diagnostic.Kind;

final class ValueParser {

Expand Down Expand Up @@ -105,7 +111,39 @@ static Value parse(Element element, ProcessingEnvironment processingEnv) {
}

String valueSimpleName = methodElement.getSimpleName().toString();
return new Value(valueSimpleName, javadoc, parameters);
return new Value(
valueSimpleName, javadoc, parameters, parseMethodAnnotations(methodElement, messager));
}

private static Iterable<AnnotationSpec> parseMethodAnnotations(
ExecutableElement methodElement, Messager messager) {
ArrayList<AnnotationSpec> annotations = new ArrayList<>();

for (AnnotationMirror annotationMirror : methodElement.getAnnotationMirrors()) {
TypeName annotationTypeName =
ClassName.get(annotationMirror.getAnnotationType().asElement().asType());

if (!(annotationTypeName instanceof ClassName)) {
messager.printMessage(
Kind.ERROR,
"Annotation is not a class; this shouldn't happen",
methodElement,
annotationMirror);
continue;
}

Builder builder = AnnotationSpec.builder(((ClassName) annotationTypeName));

for (Entry<? extends ExecutableElement, ? extends AnnotationValue> entry :
annotationMirror.getElementValues().entrySet()) {

builder.addMember(entry.getKey().getSimpleName().toString(), entry.getValue().toString());
}

annotations.add(builder.build());
}

return annotations;
}

private static boolean isAnnotationPresent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,4 +274,9 @@ public void shouldCopyDocFromCaseSourceToJavadocCommentOnFactoryMethod() {
+ " * @return a {@link Documented} (see {@link Javadoc_dataenum#Documented} for source)\n"
+ " */\n");
}

@Test
public void shouldCopyAnnotationsFromCaseSourceToFactoryMethod() {
assertThatEnumGeneratedMatchingFile("annotation/Annotation", "annotation/MyAnnotation.java");
}
}
134 changes: 134 additions & 0 deletions dataenum-processor/src/test/resources/annotation/Annotation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* -\-\-
* DataEnum
* --
* Copyright (c) 2017 Spotify AB
* --
* 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
*
* http://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 annotation;

import com.spotify.dataenum.function.Consumer;
import com.spotify.dataenum.function.Function;
import java.lang.Deprecated;
import java.lang.Object;
import java.lang.Override;
import java.lang.String;
import java.lang.SuppressWarnings;
import javax.annotation.Generated;
import javax.annotation.Nonnull;

@Generated("com.spotify.dataenum.processor.DataEnumProcessor")
public abstract class Annotation {
Annotation() {
}

@SuppressWarnings({"floopity", "floop"})
@Deprecated
@MyAnnotation(foo = "hi", fie = 15)
public static Annotation annotatedWithParams() {
return new AnnotatedWithParams();
}

@MyAnnotation(foo = "hi")
public static Annotation annotatedWithDefault() {
return new AnnotatedWithDefault();
}

public final boolean isAnnotatedWithParams() {
return (this instanceof AnnotatedWithParams);
}

public final boolean isAnnotatedWithDefault() {
return (this instanceof AnnotatedWithDefault);
}

public final AnnotatedWithParams asAnnotatedWithParams() {
return (AnnotatedWithParams) this;
}

public final AnnotatedWithDefault asAnnotatedWithDefault() {
return (AnnotatedWithDefault) this;
}

public abstract void match(@Nonnull Consumer<AnnotatedWithParams> annotatedWithParams,
@Nonnull Consumer<AnnotatedWithDefault> annotatedWithDefault);

public abstract <R_> R_ map(@Nonnull Function<AnnotatedWithParams, R_> annotatedWithParams,
@Nonnull Function<AnnotatedWithDefault, R_> annotatedWithDefault);

public static final class AnnotatedWithParams extends Annotation {
AnnotatedWithParams() {
}

@Override
public boolean equals(Object other) {
return other instanceof AnnotatedWithParams;
}

@Override
public int hashCode() {
return 0;
}

@Override
public String toString() {
return "AnnotatedWithParams{}";
}

@Override
public final void match(@Nonnull Consumer<AnnotatedWithParams> annotatedWithParams,
@Nonnull Consumer<AnnotatedWithDefault> annotatedWithDefault) {
annotatedWithParams.accept(this);
}

@Override
public final <R_> R_ map(@Nonnull Function<AnnotatedWithParams, R_> annotatedWithParams,
@Nonnull Function<AnnotatedWithDefault, R_> annotatedWithDefault) {
return annotatedWithParams.apply(this);
}
}

public static final class AnnotatedWithDefault extends Annotation {
AnnotatedWithDefault() {
}

@Override
public boolean equals(Object other) {
return other instanceof AnnotatedWithDefault;
}

@Override
public int hashCode() {
return 0;
}

@Override
public String toString() {
return "AnnotatedWithDefault{}";
}

@Override
public final void match(@Nonnull Consumer<AnnotatedWithParams> annotatedWithParams,
@Nonnull Consumer<AnnotatedWithDefault> annotatedWithDefault) {
annotatedWithDefault.accept(this);
}

@Override
public final <R_> R_ map(@Nonnull Function<AnnotatedWithParams, R_> annotatedWithParams,
@Nonnull Function<AnnotatedWithDefault, R_> annotatedWithDefault) {
return annotatedWithDefault.apply(this);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* -\-\-
* DataEnum
* --
* Copyright (c) 2017 Spotify AB
* --
* 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
*
* http://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 annotation;

import com.spotify.dataenum.DataEnum;
import com.spotify.dataenum.dataenum_case;

@DataEnum
interface Annotation_dataenum {
@SuppressWarnings({"floopity", "floop"})
@Deprecated
@MyAnnotation(foo = "hi", fie = 15)
dataenum_case AnnotatedWithParams();

@MyAnnotation(foo = "hi")
dataenum_case AnnotatedWithDefault();
}
33 changes: 33 additions & 0 deletions dataenum-processor/src/test/resources/annotation/MyAnnotation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* -\-\-
* DataEnum
* --
* Copyright (c) 2017 Spotify AB
* --
* 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
*
* http://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 annotation;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

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

@Retention(RUNTIME)
@Target(METHOD)
public @interface MyAnnotation {
String foo();
int fie() default 99;
}