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
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class OutputSpec extends Spec {
private final Iterable<OutputValue> outputValues;

public OutputSpec(Spec input, ClassName outputClass, Iterable<OutputValue> outputValues) {
super(input.specClass(), input.typeVariables(), input.values());
super(input.specClass(), input.typeVariables(), input.superInterfaces(), input.values());
this.outputClass = outputClass;
this.outputValues = outputValues;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,17 @@
public class Spec {
private final ClassName specClass;
private final Iterable<TypeVariableName> typeVariables;
private final Iterable<ClassName> interfaces;
private final Iterable<Value> values;

public Spec(
ClassName specClass, Iterable<TypeVariableName> typeVariables, Iterable<Value> values) {
ClassName specClass,
Iterable<TypeVariableName> typeVariables,
Iterable<ClassName> interfaces,
Iterable<Value> values) {
this.specClass = specClass;
this.typeVariables = typeVariables;
this.interfaces = interfaces;
this.values = values;
}

Expand All @@ -49,6 +54,10 @@ public Iterable<TypeVariableName> typeVariables() {
return typeVariables;
}

public Iterable<ClassName> superInterfaces() {
return interfaces;
}

public boolean hasTypeVariables() {
return !Iterables.isEmpty(typeVariables());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ public static TypeSpec create(
"value", CodeBlock.of("$S", DataEnumProcessor.class.getCanonicalName()))
.build())
.addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT)
.addTypeVariables(spec.typeVariables());
.addTypeVariables(spec.typeVariables())
.addSuperinterfaces(spec.superInterfaces());

// add constructor with correct access
enumBuilder.addMethod(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@
import com.squareup.javapoet.TypeVariableName;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.processing.Messager;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.TypeParameterElement;
import javax.lang.model.type.DeclaredType;
import javax.tools.Diagnostic;

public final class SpecParser {
Expand All @@ -53,12 +55,17 @@ public static Spec parse(Element element, ProcessingEnvironment processingEnv) {
typeVariableNames.add(TypeVariableName.get(typeParameterElement));
}

List<ClassName> interfaces =
dataEnum.getInterfaces().stream()
.map(x -> ClassName.get((TypeElement) ((DeclaredType) x).asElement()))
.collect(Collectors.toList());

List<Value> values = ValuesParser.parse(dataEnum, processingEnv);
if (values == null) {
return null;
}

ClassName enumInterface = ClassName.get(dataEnum);
return new Spec(enumInterface, typeVariableNames, values);
return new Spec(enumInterface, typeVariableNames, interfaces, values);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,9 @@ public void shouldCopyDocFromCaseSourceToJavadocCommentOnFactoryMethod() {
public void shouldCopyAnnotationsFromCaseSourceToFactoryMethod() {
assertThatEnumGeneratedMatchingFile("annotation/Annotation", "annotation/MyAnnotation.java");
}

@Test
public void superInterfaces() {
assertThatEnumGeneratedMatchingFile("SuperInterfaces");
}
}
175 changes: 175 additions & 0 deletions dataenum-processor/src/test/resources/SuperInterfaces.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
/*
* -\-\-
* 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.
* -/-/-
*/
import static com.spotify.dataenum.DataenumUtils.checkNotNull;

import com.spotify.dataenum.function.Consumer;
import com.spotify.dataenum.function.Function;
import java.io.Serializable;
import java.lang.Integer;
import java.lang.Object;
import java.lang.Override;
import java.lang.String;
import java.lang.StringBuilder;
import javax.annotation.Generated;
import javax.annotation.Nonnull;

/**
* Generated from {@link SuperInterfaces_dataenum}
*/
@Generated("com.spotify.dataenum.processor.DataEnumProcessor")
public abstract class SuperInterfaces implements Serializable {
SuperInterfaces() {
}

/**
* @return a {@link Value1} (see {@link SuperInterfaces_dataenum#Value1} for source)
*/
public static SuperInterfaces value1(@Nonnull String msg) {
return new Value1(msg);
}

/**
* @return a {@link Value2} (see {@link SuperInterfaces_dataenum#Value2} for source)
*/
public static SuperInterfaces value2(@Nonnull String msg, int code) {
return new Value2(msg, code);
}

public final boolean isValue1() {
return (this instanceof Value1);
}

public final boolean isValue2() {
return (this instanceof Value2);
}

public final Value1 asValue1() {
return (Value1) this;
}

public final Value2 asValue2() {
return (Value2) this;
}

public abstract void match(@Nonnull Consumer<Value1> value1, @Nonnull Consumer<Value2> value2);

public abstract <R_> R_ map(@Nonnull Function<Value1, R_> value1,
@Nonnull Function<Value2, R_> value2);

public static final class Value1 extends SuperInterfaces {
private final String msg;

Value1(String msg) {
this.msg = checkNotNull(msg);
}

@Nonnull
public final String msg() {
return msg;
}

@Override
public boolean equals(Object other) {
if (other == this) return true;
if (!(other instanceof Value1)) return false;
Value1 o = (Value1) other;
return o.msg.equals(this.msg);
}

@Override
public int hashCode() {
int result = 0;
return result * 31 + this.msg.hashCode();
}

@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Value1{msg=").append(this.msg);
return builder.append('}').toString();
}

@Override
public final void match(@Nonnull Consumer<Value1> value1, @Nonnull Consumer<Value2> value2) {
value1.accept(this);
}

@Override
public final <R_> R_ map(@Nonnull Function<Value1, R_> value1,
@Nonnull Function<Value2, R_> value2) {
return value1.apply(this);
}
}

public static final class Value2 extends SuperInterfaces {
private final String msg;

private final int code;

Value2(String msg, int code) {
this.msg = checkNotNull(msg);
this.code = code;
}

@Nonnull
public final String msg() {
return msg;
}

public final int code() {
return code;
}

@Override
public boolean equals(Object other) {
if (other == this) return true;
if (!(other instanceof Value2)) return false;
Value2 o = (Value2) other;
return o.code == code
&& o.msg.equals(this.msg);
}

@Override
public int hashCode() {
int result = 0;
result = result * 31 + this.msg.hashCode();
return result * 31 + Integer.valueOf(this.code).hashCode();
}

@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Value2{msg=").append(this.msg);
builder.append(", code=").append(this.code);
return builder.append('}').toString();
}

@Override
public final void match(@Nonnull Consumer<Value1> value1, @Nonnull Consumer<Value2> value2) {
value2.accept(this);
}

@Override
public final <R_> R_ map(@Nonnull Function<Value1, R_> value1,
@Nonnull Function<Value2, R_> value2) {
return value2.apply(this);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* -\-\-
* 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.
* -/-/-
*/
import com.spotify.dataenum.DataEnum;
import com.spotify.dataenum.dataenum_case;
import java.io.Serializable;

@DataEnum
interface SuperInterfaces_dataenum extends Serializable {
dataenum_case Value1(String msg);
dataenum_case Value2(String msg, int code);
}