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 @@ -17,18 +17,29 @@
import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableList;
import java.util.List;
import java.util.Objects;
import javax.annotation.Nullable;

@AutoValue
public abstract class ConcreteReference implements Reference {
private static final String EXTENDS = "extends";

private static final String COMMA = ", ";
private static final String DOT = ".";
private static final String SPACE = " ";
private static final String LEFT_ANGLE = "<";
private static final String RIGHT_ANGLE = ">";
private static final String QUESTION_MARK = "?";

private static final Class WILDCARD_CLAZZ = ReferenceWildcard.class;

// Private.
abstract Class clazz();

@Nullable
@Override
public abstract Reference wildcardUpperBound();

@Override
public abstract ImmutableList<Reference> generics();

Expand All @@ -38,8 +49,15 @@ public abstract class ConcreteReference implements Reference {
@Override
public String name() {
StringBuilder sb = new StringBuilder();
if (this.equals(TypeNode.WILDCARD_REFERENCE)) {
if (isWildcard()) {
sb.append(QUESTION_MARK);
if (wildcardUpperBound() != null) {
// Handle the upper bound.
sb.append(SPACE);
sb.append(EXTENDS);
sb.append(SPACE);
sb.append(wildcardUpperBound().name());
}
} else {
if (hasEnclosingClass() && !isStaticImport()) {
sb.append(clazz().getEnclosingClass().getSimpleName());
Expand Down Expand Up @@ -117,19 +135,28 @@ public boolean isAssignableFrom(Reference other) {
return clazz().isAssignableFrom(((ConcreteReference) other).clazz());
}

@Override
public boolean isWildcard() {
return clazz().equals(WILDCARD_CLAZZ);
}

@Override
public boolean equals(Object o) {
if (!(o instanceof ConcreteReference)) {
return false;
}

ConcreteReference ref = (ConcreteReference) o;
return clazz().equals(ref.clazz()) && generics().equals(ref.generics());
return clazz().equals(ref.clazz())
&& generics().equals(ref.generics())
&& Objects.equals(wildcardUpperBound(), ref.wildcardUpperBound());
}

@Override
public int hashCode() {
return 17 * clazz().hashCode() + 31 * generics().hashCode();
int wildcardUpperBoundHash =
wildcardUpperBound() == null ? 0 : 11 * wildcardUpperBound().hashCode();
return 17 * clazz().hashCode() + 31 * generics().hashCode() + wildcardUpperBoundHash;
}

@Override
Expand All @@ -141,6 +168,14 @@ public static ConcreteReference withClazz(Class clazz) {
return builder().setClazz(clazz).build();
}

public static ConcreteReference wildcard() {
return withClazz(ReferenceWildcard.class);
}

public static ConcreteReference wildcardWithUpperBound(Reference upperBoundReference) {
return builder().setClazz(WILDCARD_CLAZZ).setWildcardUpperBound(upperBoundReference).build();
}

public static Builder builder() {
return new AutoValue_ConcreteReference.Builder()
.setGenerics(ImmutableList.of())
Expand All @@ -154,6 +189,8 @@ public static Builder builder() {
public abstract static class Builder {
public abstract Builder setClazz(Class clazz);

public abstract Builder setWildcardUpperBound(Reference reference);

public abstract Builder setGenerics(List<Reference> clazzes);

public abstract Builder setIsStaticImport(boolean isStaticImport);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public interface Reference {
@Nullable
String enclosingClassName();

@Nullable
Reference wildcardUpperBound();

Reference copyAndSetGenerics(List<Reference> generics);

// Valid only for nested classes.
boolean isStaticImport();

Expand All @@ -42,5 +47,5 @@ public interface Reference {

boolean isAssignableFrom(Reference other);

Reference copyAndSetGenerics(List<Reference> generics);
boolean isWildcard();
}
20 changes: 17 additions & 3 deletions src/main/java/com/google/api/generator/engine/ast/TypeNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
@AutoValue
public abstract class TypeNode implements AstNode {
static final Reference EXCEPTION_REFERENCE = ConcreteReference.withClazz(Exception.class);
public static final Reference WILDCARD_REFERENCE =
ConcreteReference.withClazz(ReferenceWildcard.class);
public static final Reference WILDCARD_REFERENCE = ConcreteReference.wildcard();

public enum TypeKind {
BYTE,
Expand Down Expand Up @@ -101,7 +100,22 @@ public abstract static class Builder {

public abstract Builder setReference(Reference reference);

public abstract TypeNode build();
// Private.
abstract Reference reference();

abstract TypeNode autoBuild();

public TypeNode build() {
if (reference() != null) {
// Disallow top-level wildcard references.
Preconditions.checkState(
!reference().isWildcard(),
String.format(
"The top-level referenece in a type cannot be a wildcard, found %s",
reference().name()));
}
return autoBuild();
}
}

// TODO(miraleung): More type creation helpers to come...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ public abstract class VaporReference implements Reference {
@Override
public abstract String enclosingClassName();

@Nullable
@Override
public Reference wildcardUpperBound() {
return null;
}

@Override
public String fullName() {
// TODO(unsupported): Nested classes with depth greater than 1.
Expand Down Expand Up @@ -75,6 +81,11 @@ public boolean isAssignableFrom(Reference other) {
return false;
}

@Override
public boolean isWildcard() {
return false;
}

abstract String plainName();

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import com.google.api.generator.engine.ast.WhileStatement;
import com.google.common.base.Preconditions;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -340,9 +341,15 @@ private void variableExpressions(List<VariableExpr> expressions) {
private void references(List<Reference> refs) {
for (Reference ref : refs) {
// Don't need to import this.
if ((!ref.isStaticImport()
&& (ref.isFromPackage(PKG_JAVA_LANG) || ref.isFromPackage(currentPackage)))
|| ref.equals(TypeNode.WILDCARD_REFERENCE)) {
if (!ref.isStaticImport()
&& (ref.isFromPackage(PKG_JAVA_LANG) || ref.isFromPackage(currentPackage))) {
continue;
}

if (ref.isWildcard()) {
if (ref.wildcardUpperBound() != null) {
references(Arrays.asList(ref.wildcardUpperBound()));
}
continue;
}

Expand Down
Loading