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
private String genAnnotation(AnnotationDescriptor annotationDescriptor) {
private String genAnnotation(@NotNull AnnotationDescriptor annotationDescriptor) {
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) {
return null;
}
@@ -273,17 +274,21 @@ public abstract class AnnotationCodegen {
value.accept(argumentVisitor, null);
}
private static RetentionPolicy getRetentionPolicy(ClassifierDescriptor descriptor, JetTypeMapper typeMapper) {
@NotNull
private RetentionPolicy getRetentionPolicy(@NotNull Annotated descriptor) {
for (AnnotationDescriptor annotationDescriptor : descriptor.getAnnotations()) {
String internalName = typeMapper.mapType(annotationDescriptor.getType()).getInternalName();
if("java/lang/annotation/Retention".equals(internalName)) {
CompileTimeConstant<?> compileTimeConstant = annotationDescriptor.getAllValueArguments().values().iterator().next();
assert compileTimeConstant instanceof EnumValue : "Retention argument should be Enum value " + compileTimeConstant;
PropertyDescriptor propertyDescriptor = ((EnumValue) compileTimeConstant).getValue();
assert "java/lang/annotation/RetentionPolicy".equals(typeMapper.mapType(propertyDescriptor.getType()).getInternalName()) :
"Retention argument should be of type RetentionPolicy";
String propertyDescriptorName = propertyDescriptor.getName().asString();
return RetentionPolicy.valueOf(propertyDescriptorName);
if ("java/lang/annotation/Retention".equals(internalName)) {
Collection<CompileTimeConstant<?>> valueArguments = annotationDescriptor.getAllValueArguments().values();
assert valueArguments.size() == 1 : "Retention should have an argument: " + annotationDescriptor;
CompileTimeConstant<?> compileTimeConstant = valueArguments.iterator().next();
assert compileTimeConstant instanceof EnumValue : "Retention argument should be enum value: " + compileTimeConstant;
ClassDescriptor enumEntry = ((EnumValue) compileTimeConstant).getValue();
JetType classObjectType = enumEntry.getClassObjectType();
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.COMPILE_TIME_INITIALIZER;
import static org.jetbrains.jet.lang.resolve.DescriptorUtils.isEnumEntry;
import static org.jetbrains.jet.lang.types.TypeUtils.NO_EXPECTED_TYPE;
public class AnnotationResolver {
@@ -286,22 +287,24 @@ public class AnnotationResolver {
}
@Override
public CompileTimeConstant<?> visitStringTemplateExpression(
@NotNull JetStringTemplateExpression expression,
Void nothing) {
public CompileTimeConstant<?> visitStringTemplateExpression(@NotNull JetStringTemplateExpression expression, Void nothing) {
return trace.get(BindingContext.COMPILE_TIME_VALUE, expression);
}
@Override
public CompileTimeConstant<?> visitSimpleNameExpression(@NotNull JetSimpleNameExpression expression, Void data) {
ResolvedCall<? extends CallableDescriptor> resolvedCall =
trace.getBindingContext().get(BindingContext.RESOLVED_CALL, expression);
DeclarationDescriptor descriptor = trace.getBindingContext().get(BindingContext.REFERENCE_TARGET, expression);
if (descriptor != null && isEnumEntry(descriptor)) {
return new EnumValue((ClassDescriptor) descriptor);
}
ResolvedCall<?> resolvedCall = trace.getBindingContext().get(BindingContext.RESOLVED_CALL, expression);
if (resolvedCall != null) {
CallableDescriptor callableDescriptor = resolvedCall.getResultingDescriptor();
if (callableDescriptor instanceof PropertyDescriptor) {
PropertyDescriptor propertyDescriptor = (PropertyDescriptor) callableDescriptor;
if (isEnumProperty(propertyDescriptor)) {
return new EnumValue(propertyDescriptor);
return new EnumValue(((VariableDescriptorForObject) propertyDescriptor).getObjectClass());
}
if (AnnotationUtils.isPropertyAcceptableAsAnnotationParameter(propertyDescriptor)) {
return trace.getBindingContext().get(COMPILE_TIME_INITIALIZER, propertyDescriptor);
@@ -364,7 +367,7 @@ public class AnnotationResolver {
}
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();
return classifier != null &&
DescriptorUtils.isEnumClass(classifier) &&