Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove warnings (unused vars, dead code, missing annotations, missing generic reference, etc) #840

Merged
merged 5 commits into from
Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -25,7 +25,7 @@ public abstract class AnnotationNode implements AstNode {
public static AnnotationNode DEPRECATED =
AnnotationNode.builder().setType(annotationType(Deprecated.class)).build();

private static TypeNode annotationType(Class clazz) {
private static TypeNode annotationType(Class<?> clazz) {
return TypeNode.withReference(ConcreteReference.withClazz(clazz));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ public abstract class ArithmeticOperationExpr implements OperationExpr {

public abstract Expr rhsExpr();

@Override
public abstract OperatorKind operatorKind();

@Override
public abstract TypeNode type();

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public abstract class AssignmentOperationExpr implements OperationExpr {

public abstract Expr valueExpr();

@Override
public abstract OperatorKind operatorKind();

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

@AutoValue
public abstract class BlockComment implements Comment {
@Override
public abstract String comment();

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ public abstract class ConcreteReference implements Reference {
private static final String RIGHT_ANGLE = ">";
private static final String QUESTION_MARK = "?";

private static final Class WILDCARD_CLAZZ = ReferenceWildcard.class;
private static final Class<?> WILDCARD_CLAZZ = ReferenceWildcard.class;

// Private.
abstract Class clazz();
abstract Class<?> clazz();

@Override
public void accept(AstNodeVisitor visitor) {
Expand Down Expand Up @@ -105,7 +105,7 @@ public ImmutableList<String> enclosingClassNames() {
}
// The innermost type will be the last element in the list.
ImmutableList.Builder<String> listBuilder = new ImmutableList.Builder<>();
Class currentClz = clazz();
Class<?> currentClz = clazz();
while (currentClz.getEnclosingClass() != null) {
listBuilder.add(currentClz.getEnclosingClass().getSimpleName());
currentClz = currentClz.getEnclosingClass();
Expand Down Expand Up @@ -198,7 +198,7 @@ public Reference copyAndSetGenerics(List<Reference> generics) {
return toBuilder().setGenerics(generics).build();
}

public static ConcreteReference withClazz(Class clazz) {
public static ConcreteReference withClazz(Class<?> clazz) {
return builder().setClazz(clazz).build();
}

Expand All @@ -222,7 +222,7 @@ public static Builder builder() {

@AutoValue.Builder
public abstract static class Builder {
public abstract Builder setClazz(Class clazz);
public abstract Builder setClazz(Class<?> clazz);

public abstract Builder setUseFullName(boolean useFullName);

Expand All @@ -239,7 +239,7 @@ public Builder setGenerics(Reference... references) {
public abstract ConcreteReference autoBuild();

// Private.
abstract Class clazz();
abstract Class<?> clazz();

abstract ImmutableList<Reference> generics();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
public abstract class EnumRefExpr implements Expr {
public abstract IdentifierNode identifier();

@Override
public abstract TypeNode type();

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
public interface Expr extends AstNode {
TypeNode type();

@Override
void accept(AstNodeVisitor visitor);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public abstract class LogicalOperationExpr implements OperationExpr {

public abstract Expr rhsExpr();

@Override
public abstract OperatorKind operatorKind();

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,19 +244,19 @@ public MethodDefinition build() {
"Abstract methods cannot be static, final, or private");
}

// If this method overrides another, ensure that the Override annotaiton is the last one.
// If this method overrides another, ensure that the Override annotation is the last one.
ImmutableList<AnnotationNode> processedAnnotations = annotations();
if (isOverride()) {
processedAnnotations =
annotations()
ImmutableList
.<AnnotationNode>builder()
.addAll(annotations())
.add(AnnotationNode.OVERRIDE)
.build();
}
// Remove duplicates while maintaining insertion order.
setAnnotations(
new LinkedHashSet<AnnotationNode>(processedAnnotations)
new LinkedHashSet<>(processedAnnotations)
.stream().collect(Collectors.toList()));

MethodDefinition method = autoBuild();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

@AutoValue
public abstract class NewObjectExpr implements Expr {
@Override
public abstract TypeNode type();

public abstract ImmutableList<Expr> arguments();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
package com.google.api.generator.engine.ast;

public interface ObjectValue extends Value {
@Override
TypeNode type();

@Override
String value();
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ public interface OperationExpr extends Expr {

OperatorKind operatorKind();

@Override
TypeNode type();

@Override
void accept(AstNodeVisitor visitor);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import javax.annotation.Nullable;

public interface Reference extends AstNode {
@Override
void accept(AstNodeVisitor visitor);

ImmutableList<Reference> generics();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public enum KeywordKind {
SUPER
}

@Override
public abstract TypeNode type();

public abstract KeywordKind keywordKind();
Expand Down Expand Up @@ -73,7 +74,7 @@ public Builder setArguments(Expr... arguments) {
public ReferenceConstructorExpr build() {
ReferenceConstructorExpr referenceConstructorExpr = autoBuild();
Preconditions.checkState(
referenceConstructorExpr.type().isReferenceType(referenceConstructorExpr.type()),
TypeNode.isReferenceType(referenceConstructorExpr.type()),
"A this/super constructor must have a reference type.");

NodeValidator.checkNoNullElements(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public abstract class RelationalOperationExpr implements OperationExpr {

public abstract Expr rhsExpr();

@Override
public abstract OperatorKind operatorKind();

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
package com.google.api.generator.engine.ast;

public interface Statement extends AstNode {
@Override
void accept(AstNodeVisitor visitor);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

@AutoValue
public abstract class StringObjectValue implements ObjectValue {
@Override
public abstract String value();

@Override
Expand Down
22 changes: 4 additions & 18 deletions src/main/java/com/google/api/generator/engine/ast/TypeNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public int compareTo(TypeNode other) {
return -1;
}

if (this.equals(TypeNode.NULL)) {
if (equals(TypeNode.NULL)) {
// Can't self-compare, so we don't need to check whether the other one is TypeNode.NULL.
return other.isPrimitiveType() ? 1 : -1;
}
Expand Down Expand Up @@ -161,7 +161,7 @@ public static TypeNode withReference(Reference reference) {
return TypeNode.builder().setTypeKind(TypeKind.OBJECT).setReference(reference).build();
}

public static TypeNode withExceptionClazz(Class clazz) {
public static TypeNode withExceptionClazz(Class<?> clazz) {
Preconditions.checkState(Exception.class.isAssignableFrom(clazz));
return withReference(ConcreteReference.withClazz(clazz));
}
Expand Down Expand Up @@ -200,11 +200,11 @@ public boolean isPrimitiveType() {
}

public boolean isProtoPrimitiveType() {
return isPrimitiveType() || this.equals(TypeNode.STRING) || this.equals(TypeNode.BYTESTRING);
return isPrimitiveType() || equals(TypeNode.STRING) || equals(TypeNode.BYTESTRING);
}

public boolean isSupertypeOrEquals(TypeNode other) {
boolean oneTypeIsNull = this.equals(TypeNode.NULL) ^ other.equals(TypeNode.NULL);
boolean oneTypeIsNull = equals(TypeNode.NULL) ^ other.equals(TypeNode.NULL);
return !isPrimitiveType()
&& !other.isPrimitiveType()
&& isArray() == other.isArray()
Expand Down Expand Up @@ -256,20 +256,6 @@ boolean isBoxedTypeEquals(TypeNode other) {
return Objects.equals(other, BOXED_TYPE_MAP.get(this));
}

private static TypeNode createPrimitiveType(TypeKind typeKind) {
if (!isPrimitiveType(typeKind)) {
throw new IllegalArgumentException("Object is not a primitive type.");
}
return TypeNode.builder().setTypeKind(typeKind).build();
}

private static TypeNode createPrimitiveArrayType(TypeKind typeKind) {
if (typeKind.equals(TypeKind.OBJECT)) {
throw new IllegalArgumentException("Object is not a primitive type.");
}
return TypeNode.builder().setTypeKind(typeKind).setIsArray(true).build();
}

private static boolean isPrimitiveType(TypeKind typeKind) {
return !typeKind.equals(TypeKind.OBJECT);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ public abstract class UnaryOperationExpr implements OperationExpr {

public abstract Expr expr();

@Override
public abstract OperatorKind operatorKind();

@Override
public abstract TypeNode type();

@Override
public void accept(AstNodeVisitor visitor) {
visitor.visit(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@

public class ImportWriterVisitor implements AstNodeVisitor {
private static final String DOT = ".";
private static final String NEWLINE = "\n";
private static final String PKG_JAVA_LANG = "java.lang";

private final Set<String> staticImports = new TreeSet<>();
Expand All @@ -94,7 +93,7 @@ public void clear() {

public void initialize(@Nonnull String currentPackage) {
this.currentPackage = currentPackage;
this.currentClassName = null;
currentClassName = null;
}

public void initialize(@Nonnull String currentPackage, @Nonnull String currentClassName) {
Expand Down Expand Up @@ -124,7 +123,8 @@ public boolean collidesWithImport(String pakkage, String shortName) {
updateShortNames();
}
return importShortNames.contains(shortName)
&& imports.stream()
&& imports
.stream()
.filter(s -> s.equals(String.format("%s.%s", pakkage, shortName)))
.findFirst()
.orElse("")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ public class JavaWriterVisitor implements AstNodeVisitor {
private static final String BLOCK_COMMENT_START = "/*";
private static final String BLOCK_COMMENT_END = "*/";
private static final String DOT = ".";
private static final String ESCAPED_QUOTE = "\"";
private static final String EQUALS = "=";
private static final String LEFT_ANGLE = "<";
private static final String LEFT_BRACE = "{";
Expand Down Expand Up @@ -751,6 +750,7 @@ public void visit(BreakStatement breakStatement) {
}

/** =============================== COMMENT =============================== */
@Override
public void visit(LineComment lineComment) {
// Split comments by new line and add `//` to each line.
String formattedSource =
Expand All @@ -759,6 +759,7 @@ public void visit(LineComment lineComment) {
buffer.append(formattedSource);
}

@Override
public void visit(BlockComment blockComment) {
// Split comments by new line and embrace the comment block with `/* */`.
StringBuilder sourceComment = new StringBuilder();
Expand All @@ -772,6 +773,7 @@ public void visit(BlockComment blockComment) {
buffer.append(JavaFormatter.format(sourceComment.toString()));
}

@Override
public void visit(JavaDocComment javaDocComment) {
StringBuilder sourceComment = new StringBuilder();
sourceComment.append(JAVADOC_COMMENT_START).append(NEWLINE);
Expand Down Expand Up @@ -865,7 +867,6 @@ public void visit(MethodDefinition methodDefinition) {
buffer.append(THROWS);
space();

int numExceptionsThrown = methodDefinition.throwsExceptions().size();
Iterator<TypeNode> exceptionIter = methodDefinition.throwsExceptions().iterator();
while (exceptionIter.hasNext()) {
TypeNode exceptionType = exceptionIter.next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@

public class ServiceClientCommentComposer {
// Tokens.
private static final String COLON = ":";
private static final String EMPTY_STRING = "";
private static final String API_EXCEPTION_TYPE_NAME = "com.google.api.gax.rpc.ApiException";
private static final String EXCEPTION_CONDITION = "if the remote call fails";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
public class SettingsCommentComposer {
private static final String COLON = ":";

private static final String STUB_PATTERN = "%sStub";
private static final String BUILDER_CLASS_DOC_PATTERN = "Builder for %s.";
private static final String CALL_SETTINGS_METHOD_DOC_PATTERN =
"Returns the object with the settings used for calls to %s.";
Expand Down
Loading