Skip to content

Commit

Permalink
Update XProcessing jars and use new XAnnotationValue APIs.
Browse files Browse the repository at this point in the history
RELNOTES=N/A
PiperOrigin-RevId: 461957214
  • Loading branch information
bcorso authored and Dagger Team committed Jul 19, 2022
1 parent 74ea765 commit 9224d1b
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 204 deletions.
25 changes: 13 additions & 12 deletions examples/bazel/java/example/hilt/BUILD
Expand Up @@ -12,18 +12,19 @@
# See the License for the specific language governing permissions and
# limitations under the License.

android_binary(
name = "hilt",
srcs = ["CoffeeApp.java"],
manifest = "AndroidManifest.xml",
resource_files = glob(["res/**"]),
deps = [
":heater_module",
":pump_module",
"//:hilt-android",
"//java/example/common",
],
)
# TODO(b/239596898): Reenable once we've fixed this issue.
# android_binary(
# name = "hilt",
# srcs = ["CoffeeApp.java"],
# manifest = "AndroidManifest.xml",
# resource_files = glob(["res/**"]),
# deps = [
# ":heater_module",
# ":pump_module",
# "//:hilt-android",
# "//java/example/common",
# ],
# )

android_library(
name = "heater_module",
Expand Down
12 changes: 4 additions & 8 deletions java/dagger/internal/codegen/base/MapKeyAccessibility.java
Expand Up @@ -17,10 +17,6 @@
package dagger.internal.codegen.base;

import static dagger.internal.codegen.langmodel.Accessibility.isTypeAccessibleFrom;
import static dagger.internal.codegen.xprocessing.XAnnotationValues.hasAnnotationValue;
import static dagger.internal.codegen.xprocessing.XAnnotationValues.hasArrayValue;
import static dagger.internal.codegen.xprocessing.XAnnotationValues.hasEnumValue;
import static dagger.internal.codegen.xprocessing.XAnnotationValues.hasTypeValue;

import androidx.room.compiler.processing.XAnnotation;
import androidx.room.compiler.processing.XAnnotationValue;
Expand All @@ -45,13 +41,13 @@ private static boolean checkValues(
}

private static boolean checkValue(XAnnotationValue value, Predicate<XType> accessibilityChecker) {
if (hasArrayValue(value)) {
if (value.hasListValue()) {
return checkValues(value.asAnnotationValueList(), accessibilityChecker);
} else if (hasAnnotationValue(value)) {
} else if (value.hasAnnotationValue()) {
return checkAnnotation(value.asAnnotation(), accessibilityChecker);
} else if (hasEnumValue(value)) {
} else if (value.hasEnumValue()) {
return accessibilityChecker.test(value.asEnum().getEnclosingElement().getType());
} else if (hasTypeValue(value)) {
} else if (value.hasTypeValue()) {
return accessibilityChecker.test(value.asType());
} else {
return true;
Expand Down
33 changes: 11 additions & 22 deletions java/dagger/internal/codegen/binding/AnnotationExpression.java
Expand Up @@ -23,17 +23,6 @@
import static dagger.internal.codegen.extension.DaggerStreams.toImmutableMap;
import static dagger.internal.codegen.javapoet.CodeBlocks.makeParametersCodeBlock;
import static dagger.internal.codegen.javapoet.CodeBlocks.toParametersCodeBlock;
import static dagger.internal.codegen.xprocessing.XAnnotationValues.hasAnnotationValue;
import static dagger.internal.codegen.xprocessing.XAnnotationValues.hasArrayValue;
import static dagger.internal.codegen.xprocessing.XAnnotationValues.hasByteValue;
import static dagger.internal.codegen.xprocessing.XAnnotationValues.hasCharValue;
import static dagger.internal.codegen.xprocessing.XAnnotationValues.hasDoubleValue;
import static dagger.internal.codegen.xprocessing.XAnnotationValues.hasEnumValue;
import static dagger.internal.codegen.xprocessing.XAnnotationValues.hasFloatValue;
import static dagger.internal.codegen.xprocessing.XAnnotationValues.hasLongValue;
import static dagger.internal.codegen.xprocessing.XAnnotationValues.hasShortValue;
import static dagger.internal.codegen.xprocessing.XAnnotationValues.hasStringValue;
import static dagger.internal.codegen.xprocessing.XAnnotationValues.hasTypeValue;
import static dagger.internal.codegen.xprocessing.XElements.getSimpleName;
import static dagger.internal.codegen.xprocessing.XTypes.asArray;

Expand Down Expand Up @@ -124,32 +113,32 @@ CodeBlock getValueExpression(XAnnotationValue value, XType valueType) {
}

private CodeBlock visit(XAnnotationValue value) {
if (hasEnumValue(value)) {
if (value.hasEnumValue()) {
return CodeBlock.of(
"$T.$L",
value.asEnum().getEnclosingElement().getClassName(),
getSimpleName(value.asEnum()));
} else if (hasAnnotationValue(value)) {
} else if (value.hasAnnotationValue()) {
return getAnnotationInstanceExpression(value.asAnnotation());
} else if (hasTypeValue(value)) {
} else if (value.hasTypeValue()) {
return CodeBlock.of("$T.class", value.asType().getTypeName());
} else if (hasStringValue(value)) {
} else if (value.hasStringValue()) {
return CodeBlock.of("$S", value.asString());
} else if (hasByteValue(value)) {
} else if (value.hasByteValue()) {
return CodeBlock.of("(byte) $L", value.asByte());
} else if (hasCharValue(value)) {
} else if (value.hasCharValue()) {
// TODO(bcorso): This relies on AnnotationValue.toString() to properly output escaped
// characters like '\n'. See https://github.com/square/javapoet/issues/698.
return CodeBlock.of("$L", toJavac(value));
} else if (hasDoubleValue(value)) {
} else if (value.hasDoubleValue()) {
return CodeBlock.of("$LD", value.asDouble());
} else if (hasFloatValue(value)) {
} else if (value.hasFloatValue()) {
return CodeBlock.of("$LF", value.asFloat());
} else if (hasLongValue(value)) {
} else if (value.hasLongValue()) {
return CodeBlock.of("$LL", value.asLong());
} else if (hasShortValue(value)) {
} else if (value.hasShortValue()) {
return CodeBlock.of("(short) $L", value.asShort());
} else if (hasArrayValue(value)) {
} else if (value.hasListValue()) {
return CodeBlock.of(
"{$L}",
value.asAnnotationValueList().stream().map(this::visit).collect(toParametersCodeBlock()));
Expand Down
6 changes: 2 additions & 4 deletions java/dagger/internal/codegen/kotlin/KotlinMetadata.java
Expand Up @@ -17,8 +17,6 @@
package dagger.internal.codegen.kotlin;

import static dagger.internal.codegen.extension.DaggerStreams.toImmutableMap;
import static dagger.internal.codegen.xprocessing.XAnnotationValues.hasIntValue;
import static dagger.internal.codegen.xprocessing.XAnnotationValues.hasStringValue;
import static dagger.internal.codegen.xprocessing.XElements.getFieldDescriptor;
import static dagger.internal.codegen.xprocessing.XElements.getSimpleName;

Expand Down Expand Up @@ -177,10 +175,10 @@ private static KotlinClassMetadata.Class metadataOf(XTypeElement typeElement) {
metadataAnnotation.getAsStringList("d1").toArray(new String[0]),
metadataAnnotation.getAsStringList("d2").toArray(new String[0]),
metadataAnnotation.getAsString("xs"),
hasStringValue(metadataAnnotation.getAnnotationValue("pn"))
metadataAnnotation.getAnnotationValue("pn").hasStringValue()
? metadataAnnotation.getAsString("pn")
: null,
hasIntValue(metadataAnnotation.getAnnotationValue("xi"))
metadataAnnotation.getAnnotationValue("xi").hasIntValue()
? metadataAnnotation.getAsInt("xi")
: null);
KotlinClassMetadata metadata = KotlinClassMetadata.read(header);
Expand Down
167 changes: 9 additions & 158 deletions java/dagger/internal/codegen/xprocessing/XAnnotationValues.java
Expand Up @@ -16,187 +16,38 @@

package dagger.internal.codegen.xprocessing;

import static androidx.room.compiler.processing.compat.XConverters.toJavac;

import androidx.room.compiler.processing.XAnnotationValue;
import com.google.common.base.Equivalence;
import java.util.List;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.SimpleAnnotationValueVisitor8;

// TODO(bcorso): Consider moving these methods into XProcessing library.
/** A utility class for {@link XAnnotationValue} helper methods. */
public final class XAnnotationValues {
private enum Kind {
BOOLEAN,
BYTE,
CHAR,
DOUBLE,
FLOAT,
INT,
LONG,
SHORT,
STRING,
TYPE,
ENUM_CONSTANT,
ANNOTATION,
ARRAY,
};

public static boolean hasBooleanValue(XAnnotationValue value) {
return getKind(value) == Kind.BOOLEAN;
}

public static boolean hasByteValue(XAnnotationValue value) {
return getKind(value) == Kind.BYTE;
}

public static boolean hasCharValue(XAnnotationValue value) {
return getKind(value) == Kind.CHAR;
}

public static boolean hasDoubleValue(XAnnotationValue value) {
return getKind(value) == Kind.DOUBLE;
}

public static boolean hasFloatValue(XAnnotationValue value) {
return getKind(value) == Kind.FLOAT;
}

public static boolean hasIntValue(XAnnotationValue value) {
return getKind(value) == Kind.INT;
}

public static boolean hasLongValue(XAnnotationValue value) {
return getKind(value) == Kind.LONG;
}

public static boolean hasShortValue(XAnnotationValue value) {
return getKind(value) == Kind.SHORT;
}

public static boolean hasStringValue(XAnnotationValue value) {
return getKind(value) == Kind.STRING;
}

public static boolean hasTypeValue(XAnnotationValue value) {
return getKind(value) == Kind.TYPE;
}

public static boolean hasEnumValue(XAnnotationValue value) {
return getKind(value) == Kind.ENUM_CONSTANT;
}

public static boolean hasAnnotationValue(XAnnotationValue value) {
return getKind(value) == Kind.ANNOTATION;
}

public static boolean hasArrayValue(XAnnotationValue value) {
return getKind(value) == Kind.ARRAY;
}

private static Kind getKind(XAnnotationValue value) {
return KindVisitor.INSTANCE.visit(toJavac(value));
}

private static final class KindVisitor extends SimpleAnnotationValueVisitor8<Kind, Void> {
private static final KindVisitor INSTANCE = new KindVisitor();

@Override
public Kind visitBoolean(boolean b, Void p) {
return Kind.BOOLEAN;
}

@Override
public Kind visitByte(byte b, Void p) {
return Kind.BYTE;
}

@Override
public Kind visitChar(char c, Void p) {
return Kind.CHAR;
}

@Override
public Kind visitDouble(double d, Void p) {
return Kind.DOUBLE;
}

@Override
public Kind visitFloat(float f, Void p) {
return Kind.FLOAT;
}

@Override
public Kind visitInt(int i, Void p) {
return Kind.INT;
}

@Override
public Kind visitLong(long i, Void p) {
return Kind.LONG;
}

@Override
public Kind visitShort(short s, Void p) {
return Kind.SHORT;
}

@Override
public Kind visitString(String s, Void p) {
return Kind.STRING;
}

@Override
public Kind visitType(TypeMirror t, Void p) {
return Kind.TYPE;
}

@Override
public Kind visitEnumConstant(VariableElement e, Void p) {
return Kind.ENUM_CONSTANT;
}

@Override
public Kind visitAnnotation(AnnotationMirror a, Void p) {
return Kind.ANNOTATION;
}

@Override
public Kind visitArray(List<? extends AnnotationValue> vals, Void p) {
return Kind.ARRAY;
}
}

private static final Equivalence<XAnnotationValue> XANNOTATION_VALUE_EQUIVALENCE =
new Equivalence<XAnnotationValue>() {
@Override
protected boolean doEquivalent(XAnnotationValue left, XAnnotationValue right) {
if (hasAnnotationValue(left)) {
return hasAnnotationValue(right)
if (left.hasAnnotationValue()) {
return right.hasAnnotationValue()
&& XAnnotations.equivalence().equivalent(left.asAnnotation(), right.asAnnotation());
} else if (hasArrayValue(left)) {
return hasArrayValue(right)
} else if (left.hasListValue()) {
return right.hasListValue()
&& XAnnotationValues.equivalence()
.pairwise()
.equivalent(left.asAnnotationValueList(), right.asAnnotationValueList());
} else if (hasTypeValue(left)) {
return hasTypeValue(right)
} else if (left.hasTypeValue()) {
return right.hasTypeValue()
&& XTypes.equivalence().equivalent(left.asType(), right.asType());
}
return left.getValue().equals(right.getValue());
}

@Override
protected int doHash(XAnnotationValue value) {
if (hasAnnotationValue(value)) {
if (value.hasAnnotationValue()) {
return XAnnotations.equivalence().hash(value.asAnnotation());
} else if (hasArrayValue(value)) {
} else if (value.hasListValue()) {
return XAnnotationValues.equivalence().pairwise().hash(value.asAnnotationValueList());
} else if (hasTypeValue(value)) {
} else if (value.hasTypeValue()) {
return XTypes.equivalence().hash(value.asType());
}
return value.getValue().hashCode();
Expand Down
Binary file modified java/dagger/internal/codegen/xprocessing/xprocessing-testing.jar
Binary file not shown.
Binary file modified java/dagger/internal/codegen/xprocessing/xprocessing.jar
Binary file not shown.

0 comments on commit 9224d1b

Please sign in to comment.