Skip to content

Commit

Permalink
Use concatenation to avoid ShadowJar relocate on annotation aliases (o…
Browse files Browse the repository at this point in the history
  • Loading branch information
wmdietl authored Oct 13, 2021
1 parent 2da766b commit d6e8221
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -325,29 +325,33 @@ private boolean isAutoValueRequiredProperty(Element member, Set<String> avBuilde
return true;
}

/**
* This list of classes that AutoValue considers "optional" comes from AutoValue's source code.
*/
private static final String[] optionalClassNames =
new String[] {
// Use concatenation to avoid ShadowJar relocate
// "com.google.common.base.Optional",
"com.go".toString() + "ogle.common.base.Optional",
"java.util.Optional",
"java.util.OptionalDouble",
"java.util.OptionalInt",
"java.util.OptionalLong"
};

/**
* Returns whether AutoValue considers a type to be "optional". Optional types do not need to be
* set before build is called on a builder. Adapted from AutoValue source code.
*
* @param type some type
* @return true if type is an Optional type
*/
static boolean isOptional(TypeMirror type) {
private static boolean isOptional(TypeMirror type) {
if (type.getKind() != TypeKind.DECLARED) {
return false;
}
DeclaredType declaredType = (DeclaredType) type;
TypeElement typeElement = (TypeElement) declaredType.asElement();
// This list of classes that AutoValue considers "optional" comes from AutoValue's source
// code.
String[] optionalClassNames =
new String[] {
"com.google.common.base.Optional",
"java.util.Optional",
"java.util.OptionalDouble",
"java.util.OptionalInt",
"java.util.OptionalLong"
};
return typeElement.getTypeParameters().size() == declaredType.getTypeArguments().size()
&& ArraysPlume.indexOf(
optionalClassNames, typeElement.getQualifiedName().toString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ private BuilderFrameworkSupportUtils() {
* @return true if the type is a Guava immutable collection
*/
public static boolean isGuavaImmutableType(TypeMirror type) {
return type.toString().startsWith("com.google.common.collect.Immutable");
// Use concatenation to avoid ShadowJar relocate
// "com.google.common.collect.Immutable"
return type.toString().startsWith("com.go".toString() + "ogle.common.collect.Immutable");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.checkerframework.javacutil.AnnotationUtils;
import org.checkerframework.javacutil.TypeSystemError;

import java.lang.annotation.Annotation;
import java.util.IllegalFormatException;

import javax.lang.model.element.AnnotationMirror;
Expand Down Expand Up @@ -67,10 +68,23 @@ public class FormatterAnnotatedTypeFactory extends BaseAnnotatedTypeFactory {
public FormatterAnnotatedTypeFactory(BaseTypeChecker checker) {
super(checker);

addAliasedDeclAnnotation(
com.google.errorprone.annotations.FormatMethod.class,
FormatMethod.class,
FORMATMETHOD);
try {
// Use concatenation to avoid ShadowJar relocate
// "com.google.errorprone.annotations.FormatMethod"
@SuppressWarnings({
"unchecked", // Class must be an annotation type
"signature:argument.type.incompatible" // Class name intentionally obfuscated
})
Class<? extends Annotation> cgFormatMethod =
(Class<? extends Annotation>)
Class.forName(
"com.go".toString()
+ "ogle.errorprone.annotations.FormatMethod");

addAliasedDeclAnnotation(cgFormatMethod, FormatMethod.class, FORMATMETHOD);
} catch (ClassNotFoundException cnfe) {
// Ignore if com.google.errorprone.annotations.FormatMethod cannot be found.
}

this.postInit();
}
Expand Down Expand Up @@ -135,6 +149,7 @@ private boolean hasFormatMethodAnno(AMethod methodAnnos) {
for (Annotation anno : methodAnnos.tlAnnotationsHere) {
String annoName = anno.def.name;
if (annoName.equals("org.checkerframework.checker.formatter.qual.FormatMethod")
// TODO: avoid com.google relocate
|| anno.def.name.equals("com.google.errorprone.annotations.FormatMethod")) {
return true;
}
Expand All @@ -157,6 +172,7 @@ private boolean hasFormatMethodAnno(
declarationAnnos,
org.checkerframework.checker.formatter.qual.FormatMethod.class)
|| AnnotationUtils.containsSameByName(
// TODO: avoid com.google relocate
declarationAnnos, "com.google.errorprone.annotations.FormatMethod");
}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,12 @@ public class NullnessAnnotatedTypeFactory
// ../../../../../../../../docs/manual/nullness-checker.tex
// and make a pull request for variables NONNULL_ANNOTATIONS and BASE_COPYABLE_ANNOTATIONS in
// https://github.com/rzwitserloot/lombok/blob/master/src/core/lombok/core/handlers/HandlerUtil.java .
// Avoid changes to the string constants by ShadowJar relocate by using "start".toString() +
// "rest".
// Keep the original string constant in a comment to allow searching for it.
/** Aliases for {@code @Nonnull}. */
@SuppressWarnings(
"signature:assignment.type.incompatible") // Class names intentionally obfuscated
private static final List<@FullyQualifiedName String> NONNULL_ALIASES =
Arrays.asList(
// https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/annotation/NonNull.java
Expand All @@ -122,9 +127,11 @@ public class NullnessAnnotatedTypeFactory
// https://android.googlesource.com/platform/sdk/+/66fcecc/common/src/com/android/annotations/NonNull.java
"com.android.annotations.NonNull",
// https://github.com/firebase/firebase-android-sdk/blob/master/firebase-database/src/main/java/com/google/firebase/database/annotations/NotNull.java
"com.google.firebase.database.annotations.NotNull",
// "com.google.firebase.database.annotations.NotNull",
"com.go".toString() + "ogle.firebase.database.annotations.NotNull",
// https://github.com/firebase/firebase-admin-java/blob/master/src/main/java/com/google/firebase/internal/NonNull.java
"com.google.firebase.internal.NonNull",
// "com.google.firebase.internal.NonNull",
"com.go".toString() + "ogle.firebase.internal.NonNull",
// https://github.com/mongodb/mongo-java-driver/blob/master/driver-core/src/main/com/mongodb/lang/NonNull.java
"com.mongodb.lang.NonNull",
// https://github.com/eclipse-ee4j/jaxb-istack-commons/blob/master/istack-commons/runtime/src/main/java/com/sun/istack/NotNull.java
Expand Down Expand Up @@ -175,7 +182,10 @@ public class NullnessAnnotatedTypeFactory

// List is in alphabetical order. If you update it, also update
// ../../../../../../../../docs/manual/nullness-checker.tex .
// See more comments with NONNULL_ALIASES above.
/** Aliases for {@code @Nullable}. */
@SuppressWarnings(
"signature:assignment.type.incompatible") // Class names intentionally obfuscated
private static final List<@FullyQualifiedName String> NULLABLE_ALIASES =
Arrays.asList(
// https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/annotation/Nullable.java
Expand All @@ -193,17 +203,23 @@ public class NullnessAnnotatedTypeFactory
// https://github.com/lpantano/java_seqbuster/blob/master/AdRec/src/adrec/com/beust/jcommander/internal/Nullable.java
"com.beust.jcommander.internal.Nullable",
// https://github.com/cloudendpoints/endpoints-java/blob/master/endpoints-framework/src/main/java/com/google/api/server/spi/config/Nullable.java
"com.google.api.server.spi.config.Nullable",
// "com.google.api.server.spi.config.Nullable",
"com.go".toString() + "ogle.api.server.spi.config.Nullable",
// https://github.com/firebase/firebase-android-sdk/blob/master/firebase-database/src/main/java/com/google/firebase/database/annotations/Nullable.java
"com.google.firebase.database.annotations.Nullable",
// "com.google.firebase.database.annotations.Nullable",
"com.go".toString() + "ogle.firebase.database.annotations.Nullable",
// https://github.com/firebase/firebase-admin-java/blob/master/src/main/java/com/google/firebase/internal/Nullable.java
"com.google.firebase.internal.Nullable",
// "com.google.firebase.internal.Nullable",
"com.go".toString() + "ogle.firebase.internal.Nullable",
// https://gerrit.googlesource.com/gerrit/+/refs/heads/master/java/com/google/gerrit/common/Nullable.java
"com.google.gerrit.common.Nullable",
// "com.google.gerrit.common.Nullable",
"com.go".toString() + "ogle.gerrit.common.Nullable",
//
"com.google.protobuf.Internal.ProtoMethodAcceptsNullParameter",
// "com.google.protobuf.Internal.ProtoMethodAcceptsNullParameter",
"com.go".toString() + "ogle.protobuf.Internal.ProtoMethodAcceptsNullParameter",
//
"com.google.protobuf.Internal.ProtoMethodMayReturnNull",
// "com.google.protobuf.Internal.ProtoMethodMayReturnNull",
"com.go".toString() + "ogle.protobuf.Internal.ProtoMethodMayReturnNull",
// https://github.com/mongodb/mongo-java-driver/blob/master/driver-core/src/main/com/mongodb/lang/Nullable.java
"com.mongodb.lang.Nullable",
// https://github.com/eclipse-ee4j/jaxb-istack-commons/blob/master/istack-commons/runtime/src/main/java/com/sun/istack/Nullable.java
Expand Down Expand Up @@ -234,11 +250,14 @@ public class NullnessAnnotatedTypeFactory
// https://android.googlesource.com/platform/libcore/+/master/luni/src/main/java/libcore/util/Nullable.java
"libcore.util.Nullable",
// https://github.com/apache/avro/blob/master/lang/java/avro/src/main/java/org/apache/avro/reflect/Nullable.java
"org.apache.avro.reflect.Nullable",
// "org.apache.avro.reflect.Nullable",
"org.apa".toString() + "che.avro.reflect.Nullable",
// https://github.com/apache/cxf/blob/master/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/Nullable.java
"org.apache.cxf.jaxrs.ext.Nullable",
// "org.apache.cxf.jaxrs.ext.Nullable",
"org.apa".toString() + "che.cxf.jaxrs.ext.Nullable",
// https://github.com/gatein/gatein-shindig/blob/master/java/common/src/main/java/org/apache/shindig/common/Nullable.java
"org.apache.shindig.common.Nullable",
// "org.apache.shindig.common.Nullable",
"org.apa".toString() + "che.shindig.common.Nullable",
// https://search.maven.org/search?q=a:checker-compat-qual
"org.checkerframework.checker.nullness.compatqual.NullableDecl",
"org.checkerframework.checker.nullness.compatqual.NullableType",
Expand Down

0 comments on commit d6e8221

Please sign in to comment.