Skip to content

Commit

Permalink
Additional refactor to KotlinMetadata to clean up flag usage and reor…
Browse files Browse the repository at this point in the history
…der methods.

This CL adds a general method for checking flags rather than having a custom method for each flag like isObjectClass(), isPrivate(), etc...

RELNOTES=N/A
PiperOrigin-RevId: 348648656
  • Loading branch information
bcorso authored and Dagger Team committed Dec 22, 2020
1 parent dd691c4 commit bf18e2f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 86 deletions.
125 changes: 44 additions & 81 deletions java/dagger/internal/codegen/kotlin/KotlinMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import static dagger.internal.codegen.extension.DaggerStreams.toImmutableMap;
import static dagger.internal.codegen.langmodel.DaggerElements.getAnnotationMirror;
import static dagger.internal.codegen.langmodel.DaggerElements.getFieldDescriptor;
import static kotlinx.metadata.Flag.ValueParameter.DECLARES_DEFAULT_VALUE;

import com.google.auto.value.AutoValue;
import com.google.auto.value.extension.memoized.Memoized;
Expand All @@ -46,7 +47,6 @@
import javax.lang.model.util.ElementFilter;
import kotlin.Metadata;
import kotlinx.metadata.Flag;
import kotlinx.metadata.Flag.ValueParameter;
import kotlinx.metadata.KmClassVisitor;
import kotlinx.metadata.KmConstructorVisitor;
import kotlinx.metadata.KmExtensionType;
Expand Down Expand Up @@ -85,13 +85,12 @@ ImmutableMap<String, ExecutableElement> methodDescriptors() {
.collect(toImmutableMap(DaggerElements::getMethodDescriptor, Function.identity()));
}

private static String getPropertyNameFromField(VariableElement field) {
String name = field.getSimpleName().toString();
if (name.endsWith(DELEGATED_PROPERTY_NAME_SUFFIX)) {
return name.substring(0, name.length() - DELEGATED_PROPERTY_NAME_SUFFIX.length());
} else {
return name;
}
/** Returns true if any constructor of the defined a default parameter. */
@Memoized
boolean containsConstructorWithDefaultParam() {
return classMetadata().constructors().stream()
.flatMap(constructor -> constructor.parameters().stream())
.anyMatch(parameter -> parameter.flags(DECLARES_DEFAULT_VALUE));
}

/** Gets the synthetic method for annotations of a given field element. */
Expand Down Expand Up @@ -120,42 +119,6 @@ boolean isMissingSyntheticAnnotationMethod(VariableElement fieldElement) {
.orElse(false);
}

boolean isObjectClass() {
return Flag.Class.IS_OBJECT.invoke(classMetadata().flags());
}

/** Returns true if the type element of this metadata is a Kotlin companion object. */
boolean isCompanionObjectClass() {
return Flag.Class.IS_COMPANION_OBJECT.invoke(classMetadata().flags());
}

/**
* Returns the name of the companion object enclosed by the type element of this metadata. If the
* type element this metadata belongs to does not have a companion object, then this method
* returns an empty optional.
*/
Optional<String> getCompanionObjectName() {
return classMetadata().companionObjectName();
}

boolean isPrivate() {
return Flag.IS_PRIVATE.invoke(classMetadata().flags());
}

/** Gets the getter method of a given field element corresponding to a property. */
Optional<ExecutableElement> getPropertyGetter(VariableElement fieldElement) {
return elementFieldGetterMethodMap.computeIfAbsent(
fieldElement, this::getPropertyGetterUncached);
}

/** Returns true if any constructor of the defined a default parameter. */
@Memoized
boolean containsConstructorWithDefaultParam() {
return classMetadata().constructors().stream()
.flatMap(constructor -> constructor.parameters().stream())
.anyMatch(parameter -> ValueParameter.DECLARES_DEFAULT_VALUE.invoke(parameter.flags()));
}

private Optional<MethodForAnnotations> getAnnotationMethod(VariableElement fieldElement) {
return elementFieldAnnotationMethodMap.computeIfAbsent(
fieldElement, this::getAnnotationMethodUncached);
Expand All @@ -173,6 +136,12 @@ private Optional<MethodForAnnotations> getAnnotationMethodUncached(VariableEleme
.orElse(MethodForAnnotations.MISSING));
}

/** Gets the getter method of a given field element corresponding to a property. */
Optional<ExecutableElement> getPropertyGetter(VariableElement fieldElement) {
return elementFieldGetterMethodMap.computeIfAbsent(
fieldElement, this::getPropertyGetterUncached);
}

private Optional<ExecutableElement> getPropertyGetterUncached(VariableElement fieldElement) {
return findProperty(fieldElement)
.getterSignature()
Expand All @@ -192,6 +161,15 @@ private PropertyMetadata findProperty(VariableElement field) {
}
}

private static String getPropertyNameFromField(VariableElement field) {
String name = field.getSimpleName().toString();
if (name.endsWith(DELEGATED_PROPERTY_NAME_SUFFIX)) {
return name.substring(0, name.length() - DELEGATED_PROPERTY_NAME_SUFFIX.length());
} else {
return name;
}
}

/** Parse Kotlin class metadata from a given type element * */
static KotlinMetadata from(TypeElement typeElement) {
return new AutoValue_KotlinMetadata(
Expand Down Expand Up @@ -307,11 +285,7 @@ public void visitEnd() {
}

@AutoValue
abstract static class ClassMetadata {
abstract int flags();

abstract String name();

abstract static class ClassMetadata extends BaseMetadata {
abstract Optional<String> companionObjectName();

abstract ImmutableSet<FunctionMetadata> constructors();
Expand All @@ -323,11 +297,7 @@ static Builder builder() {
}

@AutoValue.Builder
abstract static class Builder {
abstract Builder flags(int flags);

abstract Builder name(String name);

abstract static class Builder implements BaseMetadata.Builder<Builder> {
abstract Builder companionObjectName(String companionObjectName);

abstract ImmutableSet.Builder<FunctionMetadata> constructorsBuilder();
Expand All @@ -351,23 +321,15 @@ Builder addProperty(PropertyMetadata property) {
}

@AutoValue
abstract static class FunctionMetadata {
abstract int flags();

abstract String name();

abstract static class FunctionMetadata extends BaseMetadata {
abstract ImmutableList<ValueParameterMetadata> parameters();

static Builder builder(int flags, String name) {
return new AutoValue_KotlinMetadata_FunctionMetadata.Builder().flags(flags).name(name);
}

@AutoValue.Builder
abstract static class Builder {
abstract Builder flags(int flags);

abstract Builder name(String name);

abstract static class Builder implements BaseMetadata.Builder<Builder> {
abstract ImmutableList.Builder<ValueParameterMetadata> parametersBuilder();

Builder addParameter(ValueParameterMetadata parameter) {
Expand All @@ -380,17 +342,7 @@ Builder addParameter(ValueParameterMetadata parameter) {
}

@AutoValue
abstract static class PropertyMetadata {
/**
* Returns the Kotlin metadata flags for this property.
*
* <p>Use {@link Flag.Property} to apply the right mask and obtain a specific value.
*/
abstract int flags();

/** Returns the simple name of this property. */
abstract String name();

abstract static class PropertyMetadata extends BaseMetadata {
/** Returns the JVM field descriptor of the backing field of this property. */
abstract Optional<String> fieldSignature();

Expand All @@ -404,11 +356,7 @@ static Builder builder(int flags, String name) {
}

@AutoValue.Builder
interface Builder {
Builder flags(int flags);

Builder name(String name);

interface Builder extends BaseMetadata.Builder<Builder> {
Builder fieldSignature(Optional<String> signature);

Builder getterSignature(Optional<String> signature);
Expand All @@ -420,14 +368,29 @@ interface Builder {
}

@AutoValue
abstract static class ValueParameterMetadata {
abstract static class ValueParameterMetadata extends BaseMetadata {
private static ValueParameterMetadata create(int flags, String name) {
return new AutoValue_KotlinMetadata_ValueParameterMetadata(flags, name);
}
}

abstract static class BaseMetadata {
/** Returns the Kotlin metadata flags for this property. */
abstract int flags();

/** returns {@code true} if the given flag (e.g. {@link Flag.IS_PRIVATE}) applies. */
boolean flags(Flag flag) {
return flag.invoke(flags());
}

/** Returns the simple name of this property. */
abstract String name();

interface Builder<BuilderT> {
BuilderT flags(int flags);

BuilderT name(String name);
}
}

@AutoValue
Expand Down
17 changes: 12 additions & 5 deletions java/dagger/internal/codegen/kotlin/KotlinMetadataUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
import static com.google.auto.common.AnnotationMirrors.getAnnotatedAnnotations;
import static com.google.auto.common.MoreElements.isAnnotationPresent;
import static dagger.internal.codegen.langmodel.DaggerElements.closestEnclosingTypeElement;
import static kotlinx.metadata.Flag.Class.IS_COMPANION_OBJECT;
import static kotlinx.metadata.Flag.Class.IS_OBJECT;
import static kotlinx.metadata.Flag.IS_PRIVATE;

import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -79,25 +82,28 @@ public boolean isMissingSyntheticPropertyForAnnotations(VariableElement fieldEle

/** Returns {@code true} if this type element is a Kotlin Object. */
public boolean isObjectClass(TypeElement typeElement) {
return hasMetadata(typeElement) && metadataFactory.create(typeElement).isObjectClass();
return hasMetadata(typeElement)
&& metadataFactory.create(typeElement).classMetadata().flags(IS_OBJECT);
}

/* Returns {@code true} if this type element is a Kotlin Companion Object. */
public boolean isCompanionObjectClass(TypeElement typeElement) {
return hasMetadata(typeElement) && metadataFactory.create(typeElement).isCompanionObjectClass();
return hasMetadata(typeElement)
&& metadataFactory.create(typeElement).classMetadata().flags(IS_COMPANION_OBJECT);
}

/* Returns {@code true} if this type element has a Kotlin Companion Object. */
public boolean hasEnclosedCompanionObject(TypeElement typeElement) {
return hasMetadata(typeElement)
&& metadataFactory.create(typeElement).getCompanionObjectName().isPresent();
&& metadataFactory.create(typeElement).classMetadata().companionObjectName().isPresent();
}

/* Returns the Companion Object element enclosed by the given type element. */
public TypeElement getEnclosedCompanionObject(TypeElement typeElement) {
return metadataFactory
.create(typeElement)
.getCompanionObjectName()
.classMetadata()
.companionObjectName()
.map(
companionObjectName ->
ElementFilter.typesIn(typeElement.getEnclosedElements()).stream()
Expand All @@ -112,7 +118,8 @@ public TypeElement getEnclosedCompanionObject(TypeElement typeElement) {
* source.
*/
public boolean isVisibilityPrivate(TypeElement typeElement) {
return hasMetadata(typeElement) && metadataFactory.create(typeElement).isPrivate();
return hasMetadata(typeElement)
&& metadataFactory.create(typeElement).classMetadata().flags(IS_PRIVATE);
}

public Optional<ExecutableElement> getPropertyGetter(VariableElement fieldElement) {
Expand Down

0 comments on commit bf18e2f

Please sign in to comment.