EnumValue now has a ClassDescriptor for its enum entry

This commit is contained in:
Alexander Udalov
2013-11-13 20:45:08 +04:00
parent 83ef095093
commit a5d6d6719c
6 changed files with 45 additions and 50 deletions
@@ -143,9 +143,10 @@ public abstract class AnnotationCodegen {
} }
@Nullable @Nullable
private String genAnnotation(AnnotationDescriptor annotationDescriptor) { private String genAnnotation(@NotNull AnnotationDescriptor annotationDescriptor) {
ClassifierDescriptor classifierDescriptor = annotationDescriptor.getType().getConstructor().getDeclarationDescriptor(); ClassifierDescriptor classifierDescriptor = annotationDescriptor.getType().getConstructor().getDeclarationDescriptor();
RetentionPolicy rp = getRetentionPolicy(classifierDescriptor, typeMapper); assert classifierDescriptor != null : "Annotation descriptor has no class: " + annotationDescriptor;
RetentionPolicy rp = getRetentionPolicy(classifierDescriptor);
if (rp == RetentionPolicy.SOURCE) { if (rp == RetentionPolicy.SOURCE) {
return null; return null;
} }
@@ -273,17 +274,21 @@ public abstract class AnnotationCodegen {
value.accept(argumentVisitor, null); value.accept(argumentVisitor, null);
} }
private static RetentionPolicy getRetentionPolicy(ClassifierDescriptor descriptor, JetTypeMapper typeMapper) { @NotNull
private RetentionPolicy getRetentionPolicy(@NotNull Annotated descriptor) {
for (AnnotationDescriptor annotationDescriptor : descriptor.getAnnotations()) { for (AnnotationDescriptor annotationDescriptor : descriptor.getAnnotations()) {
String internalName = typeMapper.mapType(annotationDescriptor.getType()).getInternalName(); String internalName = typeMapper.mapType(annotationDescriptor.getType()).getInternalName();
if("java/lang/annotation/Retention".equals(internalName)) { if ("java/lang/annotation/Retention".equals(internalName)) {
CompileTimeConstant<?> compileTimeConstant = annotationDescriptor.getAllValueArguments().values().iterator().next(); Collection<CompileTimeConstant<?>> valueArguments = annotationDescriptor.getAllValueArguments().values();
assert compileTimeConstant instanceof EnumValue : "Retention argument should be Enum value " + compileTimeConstant; assert valueArguments.size() == 1 : "Retention should have an argument: " + annotationDescriptor;
PropertyDescriptor propertyDescriptor = ((EnumValue) compileTimeConstant).getValue(); CompileTimeConstant<?> compileTimeConstant = valueArguments.iterator().next();
assert "java/lang/annotation/RetentionPolicy".equals(typeMapper.mapType(propertyDescriptor.getType()).getInternalName()) : assert compileTimeConstant instanceof EnumValue : "Retention argument should be enum value: " + compileTimeConstant;
"Retention argument should be of type RetentionPolicy"; ClassDescriptor enumEntry = ((EnumValue) compileTimeConstant).getValue();
String propertyDescriptorName = propertyDescriptor.getName().asString(); JetType classObjectType = enumEntry.getClassObjectType();
return RetentionPolicy.valueOf(propertyDescriptorName); assert classObjectType != null : "Enum entry should have a class object: " + enumEntry;
assert "java/lang/annotation/RetentionPolicy".equals(typeMapper.mapType(classObjectType).getInternalName()) :
"Retention argument should be of type RetentionPolicy: " + enumEntry;
return RetentionPolicy.valueOf(enumEntry.getName().asString());
} }
} }
@@ -47,6 +47,7 @@ import java.util.Map;
import static org.jetbrains.jet.lang.resolve.BindingContext.ANNOTATION_DESCRIPTOR_TO_PSI_ELEMENT; import static org.jetbrains.jet.lang.resolve.BindingContext.ANNOTATION_DESCRIPTOR_TO_PSI_ELEMENT;
import static org.jetbrains.jet.lang.resolve.BindingContext.COMPILE_TIME_INITIALIZER; import static org.jetbrains.jet.lang.resolve.BindingContext.COMPILE_TIME_INITIALIZER;
import static org.jetbrains.jet.lang.resolve.DescriptorUtils.isEnumEntry;
import static org.jetbrains.jet.lang.types.TypeUtils.NO_EXPECTED_TYPE; import static org.jetbrains.jet.lang.types.TypeUtils.NO_EXPECTED_TYPE;
public class AnnotationResolver { public class AnnotationResolver {
@@ -286,22 +287,24 @@ public class AnnotationResolver {
} }
@Override @Override
public CompileTimeConstant<?> visitStringTemplateExpression( public CompileTimeConstant<?> visitStringTemplateExpression(@NotNull JetStringTemplateExpression expression, Void nothing) {
@NotNull JetStringTemplateExpression expression,
Void nothing) {
return trace.get(BindingContext.COMPILE_TIME_VALUE, expression); return trace.get(BindingContext.COMPILE_TIME_VALUE, expression);
} }
@Override @Override
public CompileTimeConstant<?> visitSimpleNameExpression(@NotNull JetSimpleNameExpression expression, Void data) { public CompileTimeConstant<?> visitSimpleNameExpression(@NotNull JetSimpleNameExpression expression, Void data) {
ResolvedCall<? extends CallableDescriptor> resolvedCall = DeclarationDescriptor descriptor = trace.getBindingContext().get(BindingContext.REFERENCE_TARGET, expression);
trace.getBindingContext().get(BindingContext.RESOLVED_CALL, expression); if (descriptor != null && isEnumEntry(descriptor)) {
return new EnumValue((ClassDescriptor) descriptor);
}
ResolvedCall<?> resolvedCall = trace.getBindingContext().get(BindingContext.RESOLVED_CALL, expression);
if (resolvedCall != null) { if (resolvedCall != null) {
CallableDescriptor callableDescriptor = resolvedCall.getResultingDescriptor(); CallableDescriptor callableDescriptor = resolvedCall.getResultingDescriptor();
if (callableDescriptor instanceof PropertyDescriptor) { if (callableDescriptor instanceof PropertyDescriptor) {
PropertyDescriptor propertyDescriptor = (PropertyDescriptor) callableDescriptor; PropertyDescriptor propertyDescriptor = (PropertyDescriptor) callableDescriptor;
if (isEnumProperty(propertyDescriptor)) { if (isEnumProperty(propertyDescriptor)) {
return new EnumValue(propertyDescriptor); return new EnumValue(((VariableDescriptorForObject) propertyDescriptor).getObjectClass());
} }
if (AnnotationUtils.isPropertyAcceptableAsAnnotationParameter(propertyDescriptor)) { if (AnnotationUtils.isPropertyAcceptableAsAnnotationParameter(propertyDescriptor)) {
return trace.getBindingContext().get(COMPILE_TIME_INITIALIZER, propertyDescriptor); return trace.getBindingContext().get(COMPILE_TIME_INITIALIZER, propertyDescriptor);
@@ -364,7 +367,7 @@ public class AnnotationResolver {
} }
private static boolean isEnumProperty(@NotNull PropertyDescriptor descriptor) { private static boolean isEnumProperty(@NotNull PropertyDescriptor descriptor) {
// TODO: doesn't return true anymore // TODO: this is true only for enum entries loaded from Java, refactor JDR to use classes with class objects instead
ClassifierDescriptor classifier = descriptor.getType().getConstructor().getDeclarationDescriptor(); ClassifierDescriptor classifier = descriptor.getType().getConstructor().getDeclarationDescriptor();
return classifier != null && return classifier != null &&
DescriptorUtils.isEnumClass(classifier) && DescriptorUtils.isEnumClass(classifier) &&
@@ -19,9 +19,8 @@ package org.jetbrains.jet.lang.resolve.java.resolver;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.ClassDescriptor; import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
import org.jetbrains.jet.lang.descriptors.PropertyDescriptor; import org.jetbrains.jet.lang.descriptors.ClassifierDescriptor;
import org.jetbrains.jet.lang.descriptors.ValueParameterDescriptor; import org.jetbrains.jet.lang.descriptors.ValueParameterDescriptor;
import org.jetbrains.jet.lang.descriptors.VariableDescriptor;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor; import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
import org.jetbrains.jet.lang.resolve.constants.*; import org.jetbrains.jet.lang.resolve.constants.*;
import org.jetbrains.jet.lang.resolve.constants.StringValue; import org.jetbrains.jet.lang.resolve.constants.StringValue;
@@ -141,13 +140,10 @@ public final class JavaAnnotationArgumentResolver {
ClassDescriptor enumClass = classResolver.resolveClass(fqName, INCLUDE_KOTLIN_SOURCES, taskList); ClassDescriptor enumClass = classResolver.resolveClass(fqName, INCLUDE_KOTLIN_SOURCES, taskList);
if (enumClass == null) return null; if (enumClass == null) return null;
for (VariableDescriptor variableDescriptor : getEnumEntriesScope(enumClass).getProperties(field.getName())) { ClassifierDescriptor classifier = getEnumEntriesScope(enumClass).getClassifier(field.getName());
if (variableDescriptor.getReceiverParameter() == null) { if (!(classifier instanceof ClassDescriptor)) return null;
return new EnumValue((PropertyDescriptor) variableDescriptor);
}
}
return null; return new EnumValue((ClassDescriptor) classifier);
} }
@Nullable @Nullable
@@ -44,8 +44,7 @@ import javax.inject.Inject;
import java.io.IOException; import java.io.IOException;
import java.util.*; import java.util.*;
import static org.jetbrains.jet.lang.resolve.DescriptorUtils.isClassObject; import static org.jetbrains.jet.lang.resolve.DescriptorUtils.*;
import static org.jetbrains.jet.lang.resolve.DescriptorUtils.isTrait;
import static org.jetbrains.jet.lang.resolve.java.DescriptorSearchRule.IGNORE_KOTLIN_SOURCES; import static org.jetbrains.jet.lang.resolve.java.DescriptorSearchRule.IGNORE_KOTLIN_SOURCES;
import static org.jetbrains.jet.lang.resolve.kotlin.DeserializedResolverUtils.kotlinFqNameToJavaFqName; import static org.jetbrains.jet.lang.resolve.kotlin.DeserializedResolverUtils.kotlinFqNameToJavaFqName;
import static org.jetbrains.jet.lang.resolve.kotlin.DeserializedResolverUtils.naiveKotlinFqName; import static org.jetbrains.jet.lang.resolve.kotlin.DeserializedResolverUtils.naiveKotlinFqName;
@@ -185,15 +184,9 @@ public class AnnotationDescriptorDeserializer implements AnnotationDeserializer
private CompileTimeConstant<?> enumEntryValue(@NotNull JvmClassName enumClassName, @NotNull Name name) { private CompileTimeConstant<?> enumEntryValue(@NotNull JvmClassName enumClassName, @NotNull Name name) {
ClassDescriptor enumClass = resolveClass(enumClassName); ClassDescriptor enumClass = resolveClass(enumClassName);
if (enumClass.getKind() == ClassKind.ENUM_CLASS) { if (enumClass.getKind() == ClassKind.ENUM_CLASS) {
ClassDescriptor classObject = enumClass.getClassObjectDescriptor(); ClassifierDescriptor classifier = getEnumEntriesScope(enumClass).getClassifier(name);
if (classObject != null) { if (classifier instanceof ClassDescriptor) {
Collection<VariableDescriptor> properties = classObject.getDefaultType().getMemberScope().getProperties(name); return new EnumValue((ClassDescriptor) classifier);
if (properties.size() == 1) {
VariableDescriptor property = properties.iterator().next();
if (property instanceof PropertyDescriptor) {
return new EnumValue((PropertyDescriptor) property);
}
}
} }
} }
return ErrorValue.create("Unresolved enum entry: " + enumClassName.getInternalName() + "." + name); return ErrorValue.create("Unresolved enum entry: " + enumClassName.getInternalName() + "." + name);
@@ -17,29 +17,30 @@
package org.jetbrains.jet.lang.resolve.constants; package org.jetbrains.jet.lang.resolve.constants;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.PropertyDescriptor; import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationArgumentVisitor; import org.jetbrains.jet.lang.descriptors.annotations.AnnotationArgumentVisitor;
import org.jetbrains.jet.lang.types.JetType; import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns; import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
public class EnumValue implements CompileTimeConstant<PropertyDescriptor> { public class EnumValue implements CompileTimeConstant<ClassDescriptor> {
private final ClassDescriptor value;
private final PropertyDescriptor value;
public EnumValue(@NotNull PropertyDescriptor value) { public EnumValue(@NotNull ClassDescriptor value) {
this.value = value; this.value = value;
} }
@Override @Override
@NotNull @NotNull
public PropertyDescriptor getValue() { public ClassDescriptor getValue() {
return value; return value;
} }
@NotNull @NotNull
@Override @Override
public JetType getType(@NotNull KotlinBuiltIns kotlinBuiltIns) { public JetType getType(@NotNull KotlinBuiltIns kotlinBuiltIns) {
return value.getType(); JetType type = value.getClassObjectType();
assert type != null : "Enum entry should have a class object: " + value;
return type;
} }
@Override @Override
@@ -49,7 +50,7 @@ public class EnumValue implements CompileTimeConstant<PropertyDescriptor> {
@Override @Override
public String toString() { public String toString() {
return value.getType() + "." + value.getName(); return getType(KotlinBuiltIns.getInstance()) + "." + value.getName();
} }
@Override @Override
@@ -57,9 +58,7 @@ public class EnumValue implements CompileTimeConstant<PropertyDescriptor> {
if (this == o) return true; if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false; if (o == null || getClass() != o.getClass()) return false;
EnumValue enumValue = (EnumValue) o; return value.equals(((EnumValue) o).value);
return value.equals(enumValue.value);
} }
@Override @Override
@@ -20,7 +20,6 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.CallableDescriptor; import org.jetbrains.jet.lang.descriptors.CallableDescriptor;
import org.jetbrains.jet.lang.descriptors.ClassDescriptor; import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
import org.jetbrains.jet.lang.descriptors.PropertyDescriptor;
import org.jetbrains.jet.lang.descriptors.ValueParameterDescriptor; import org.jetbrains.jet.lang.descriptors.ValueParameterDescriptor;
import org.jetbrains.jet.lang.descriptors.annotations.Annotated; import org.jetbrains.jet.lang.descriptors.annotations.Annotated;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor; import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
@@ -63,7 +62,7 @@ public class InlineUtil {
} }
else { else {
assert argument instanceof EnumValue : "Inline annotation parameter should be inline entry but was: " + argument + "!"; assert argument instanceof EnumValue : "Inline annotation parameter should be inline entry but was: " + argument + "!";
PropertyDescriptor value = ((EnumValue)argument).getValue(); ClassDescriptor value = ((EnumValue) argument).getValue();
String name = value.getName().asString(); String name = value.getName().asString();
return name.equals(InlineStrategy.IN_PLACE.name()) ? InlineStrategy.IN_PLACE : InlineStrategy.AS_FUNCTION; return name.equals(InlineStrategy.IN_PLACE.name()) ? InlineStrategy.IN_PLACE : InlineStrategy.AS_FUNCTION;
} }