reading annotations from bytecode
(without fields yet)
This commit is contained in:
+51
-9
@@ -34,6 +34,7 @@ import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
|
|||||||
import org.jetbrains.jet.lang.resolve.BindingContext;
|
import org.jetbrains.jet.lang.resolve.BindingContext;
|
||||||
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
|
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
|
||||||
import org.jetbrains.jet.lang.resolve.OverrideResolver;
|
import org.jetbrains.jet.lang.resolve.OverrideResolver;
|
||||||
|
import org.jetbrains.jet.lang.resolve.constants.CompileTimeConstant;
|
||||||
import org.jetbrains.jet.lang.resolve.java.alt.AltClassFinder;
|
import org.jetbrains.jet.lang.resolve.java.alt.AltClassFinder;
|
||||||
import org.jetbrains.jet.lang.resolve.java.kt.JetClassAnnotation;
|
import org.jetbrains.jet.lang.resolve.java.kt.JetClassAnnotation;
|
||||||
import org.jetbrains.jet.lang.types.*;
|
import org.jetbrains.jet.lang.types.*;
|
||||||
@@ -282,7 +283,8 @@ public class JavaDescriptorResolver {
|
|||||||
DeclarationDescriptor containingDeclaration = resolveParentDescriptor(psiClass);
|
DeclarationDescriptor containingDeclaration = resolveParentDescriptor(psiClass);
|
||||||
classData.classDescriptor = new MutableClassDescriptorLite(containingDeclaration, kind);
|
classData.classDescriptor = new MutableClassDescriptorLite(containingDeclaration, kind);
|
||||||
classData.classDescriptor.setName(name);
|
classData.classDescriptor.setName(name);
|
||||||
|
classData.classDescriptor.setAnnotations(resolveAnnotations(psiClass));
|
||||||
|
|
||||||
List<JetType> supertypes = new ArrayList<JetType>();
|
List<JetType> supertypes = new ArrayList<JetType>();
|
||||||
|
|
||||||
TypeVariableResolverFromOuters outerTypeVariableByNameResolver = new TypeVariableResolverFromOuters(containingDeclaration);
|
TypeVariableResolverFromOuters outerTypeVariableByNameResolver = new TypeVariableResolverFromOuters(containingDeclaration);
|
||||||
@@ -297,10 +299,16 @@ public class JavaDescriptorResolver {
|
|||||||
classData.classDescriptor.setTypeParameterDescriptors(typeParameters);
|
classData.classDescriptor.setTypeParameterDescriptors(typeParameters);
|
||||||
classData.classDescriptor.setSupertypes(supertypes);
|
classData.classDescriptor.setSupertypes(supertypes);
|
||||||
classData.classDescriptor.setVisibility(resolveVisibilityFromPsiModifiers(psiClass));
|
classData.classDescriptor.setVisibility(resolveVisibilityFromPsiModifiers(psiClass));
|
||||||
classData.classDescriptor.setModality(Modality.convertFromFlags(
|
Modality modality;
|
||||||
psiClass.hasModifierProperty(PsiModifier.ABSTRACT) || psiClass.isInterface(),
|
if (classData.classDescriptor.getKind() == ClassKind.ANNOTATION_CLASS) {
|
||||||
!psiClass.hasModifierProperty(PsiModifier.FINAL))
|
modality = Modality.FINAL;
|
||||||
);
|
}
|
||||||
|
else {
|
||||||
|
modality = Modality.convertFromFlags(
|
||||||
|
psiClass.hasModifierProperty(PsiModifier.ABSTRACT) || psiClass.isInterface(),
|
||||||
|
!psiClass.hasModifierProperty(PsiModifier.FINAL));
|
||||||
|
}
|
||||||
|
classData.classDescriptor.setModality(modality);
|
||||||
classData.classDescriptor.createTypeConstructor();
|
classData.classDescriptor.createTypeConstructor();
|
||||||
classData.classDescriptor.setScopeForMemberLookup(new JavaClassMembersScope(classData.classDescriptor, psiClass, semanticServices, false));
|
classData.classDescriptor.setScopeForMemberLookup(new JavaClassMembersScope(classData.classDescriptor, psiClass, semanticServices, false));
|
||||||
|
|
||||||
@@ -700,8 +708,8 @@ public class JavaDescriptorResolver {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
transformSupertypeList(result, psiClass.getPsiClass().getExtendsListTypes(), new TypeVariableResolverFromTypeDescriptors(typeParameters, null));
|
transformSupertypeList(result, psiClass.getPsiClass().getExtendsListTypes(), new TypeVariableResolverFromTypeDescriptors(typeParameters, null), classDescriptor.getKind() == ClassKind.ANNOTATION_CLASS);
|
||||||
transformSupertypeList(result, psiClass.getPsiClass().getImplementsListTypes(), new TypeVariableResolverFromTypeDescriptors(typeParameters, null));
|
transformSupertypeList(result, psiClass.getPsiClass().getImplementsListTypes(), new TypeVariableResolverFromTypeDescriptors(typeParameters, null), classDescriptor.getKind() == ClassKind.ANNOTATION_CLASS);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (JetType supertype : result) {
|
for (JetType supertype : result) {
|
||||||
@@ -716,12 +724,15 @@ public class JavaDescriptorResolver {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void transformSupertypeList(List<JetType> result, PsiClassType[] extendsListTypes, TypeVariableResolver typeVariableResolver) {
|
private void transformSupertypeList(List<JetType> result, PsiClassType[] extendsListTypes, TypeVariableResolver typeVariableResolver, boolean annotation) {
|
||||||
for (PsiClassType type : extendsListTypes) {
|
for (PsiClassType type : extendsListTypes) {
|
||||||
PsiClass resolved = type.resolve();
|
PsiClass resolved = type.resolve();
|
||||||
if (resolved != null && resolved.getQualifiedName().equals(JvmStdlibNames.JET_OBJECT.getFqName())) {
|
if (resolved != null && resolved.getQualifiedName().equals(JvmStdlibNames.JET_OBJECT.getFqName())) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
if (annotation && resolved.getQualifiedName().equals("java.lang.annotation.Annotation")) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
JetType transform = semanticServices.getTypeTransformer().transformToType(type, typeVariableResolver);
|
JetType transform = semanticServices.getTypeTransformer().transformToType(type, typeVariableResolver);
|
||||||
|
|
||||||
@@ -1373,7 +1384,7 @@ public class JavaDescriptorResolver {
|
|||||||
|
|
||||||
NamedFunctionDescriptorImpl functionDescriptorImpl = new NamedFunctionDescriptorImpl(
|
NamedFunctionDescriptorImpl functionDescriptorImpl = new NamedFunctionDescriptorImpl(
|
||||||
owner,
|
owner,
|
||||||
Collections.<AnnotationDescriptor>emptyList(), // TODO
|
resolveAnnotations(method.getPsiMethod()),
|
||||||
method.getName(),
|
method.getName(),
|
||||||
CallableMemberDescriptor.Kind.DECLARATION
|
CallableMemberDescriptor.Kind.DECLARATION
|
||||||
);
|
);
|
||||||
@@ -1405,6 +1416,37 @@ public class JavaDescriptorResolver {
|
|||||||
return (FunctionDescriptorImpl) substitutedFunctionDescriptor;
|
return (FunctionDescriptorImpl) substitutedFunctionDescriptor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private List<AnnotationDescriptor> resolveAnnotations(PsiModifierListOwner owner) {
|
||||||
|
PsiAnnotation[] psiAnnotations = owner.getModifierList().getAnnotations();
|
||||||
|
List<AnnotationDescriptor> r = Lists.newArrayListWithCapacity(psiAnnotations.length);
|
||||||
|
for (PsiAnnotation psiAnnotation : psiAnnotations) {
|
||||||
|
AnnotationDescriptor annotation = resolveAnnotation(psiAnnotation);
|
||||||
|
if (annotation != null) {
|
||||||
|
r.add(annotation);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private AnnotationDescriptor resolveAnnotation(PsiAnnotation psiAnnotation) {
|
||||||
|
AnnotationDescriptor annotation = new AnnotationDescriptor();
|
||||||
|
|
||||||
|
String qname = psiAnnotation.getQualifiedName();
|
||||||
|
if (qname.startsWith("java.lang.annotation.") || qname.startsWith("jet.runtime.typeinfo.") || qname.equals(JvmAbi.JETBRAINS_NOT_NULL_ANNOTATION.getFqName())) {
|
||||||
|
// TODO
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
ClassDescriptor clazz = resolveClass(psiAnnotation.getQualifiedName());
|
||||||
|
if (clazz == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
annotation.setAnnotationType(clazz.getDefaultType());
|
||||||
|
annotation.setValueArguments(new ArrayList<CompileTimeConstant<?>>()); // TODO
|
||||||
|
return annotation;
|
||||||
|
}
|
||||||
|
|
||||||
public List<FunctionDescriptor> resolveMethods(PsiClass psiClass, ClassOrNamespaceDescriptor containingDeclaration) {
|
public List<FunctionDescriptor> resolveMethods(PsiClass psiClass, ClassOrNamespaceDescriptor containingDeclaration) {
|
||||||
ResolverScopeData scopeData = getResolverScopeData(containingDeclaration, new PsiClassWrapper(psiClass));
|
ResolverScopeData scopeData = getResolverScopeData(containingDeclaration, new PsiClassWrapper(psiClass));
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -133,7 +133,7 @@ public class LazySubstitutingClassDescriptor implements ClassDescriptor {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<AnnotationDescriptor> getAnnotations() {
|
public List<AnnotationDescriptor> getAnnotations() {
|
||||||
throw new UnsupportedOperationException(); // TODO
|
return original.getAnnotations();
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
|
|||||||
+5
-1
@@ -40,7 +40,7 @@ public class MutableClassDescriptorLite extends MutableDeclarationDescriptor imp
|
|||||||
private ConstructorDescriptor primaryConstructor;
|
private ConstructorDescriptor primaryConstructor;
|
||||||
private final Set<ConstructorDescriptor> constructors = Sets.newLinkedHashSet();
|
private final Set<ConstructorDescriptor> constructors = Sets.newLinkedHashSet();
|
||||||
|
|
||||||
private final List<AnnotationDescriptor> annotations = Lists.newArrayList();
|
private List<AnnotationDescriptor> annotations = Lists.newArrayList();
|
||||||
|
|
||||||
private Map<String, ClassDescriptor> innerClassesAndObjects = Maps.newHashMap();
|
private Map<String, ClassDescriptor> innerClassesAndObjects = Maps.newHashMap();
|
||||||
|
|
||||||
@@ -342,4 +342,8 @@ public class MutableClassDescriptorLite extends MutableDeclarationDescriptor imp
|
|||||||
public List<AnnotationDescriptor> getAnnotations() {
|
public List<AnnotationDescriptor> getAnnotations() {
|
||||||
return annotations;
|
return annotations;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setAnnotations(List<AnnotationDescriptor> annotations) {
|
||||||
|
this.annotations = annotations;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+5
@@ -17,9 +17,11 @@
|
|||||||
package org.jetbrains.jet.lang.descriptors.annotations;
|
package org.jetbrains.jet.lang.descriptors.annotations;
|
||||||
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
|
||||||
import org.jetbrains.jet.lang.resolve.constants.CompileTimeConstant;
|
import org.jetbrains.jet.lang.resolve.constants.CompileTimeConstant;
|
||||||
import org.jetbrains.jet.lang.types.JetType;
|
import org.jetbrains.jet.lang.types.JetType;
|
||||||
|
|
||||||
|
import javax.rmi.CORBA.ClassDesc;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -40,6 +42,9 @@ public class AnnotationDescriptor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setAnnotationType(@NotNull JetType annotationType) {
|
public void setAnnotationType(@NotNull JetType annotationType) {
|
||||||
|
if (!(annotationType.getConstructor().getDeclarationDescriptor() instanceof ClassDescriptor)) {
|
||||||
|
throw new IllegalStateException();
|
||||||
|
}
|
||||||
this.annotationType = annotationType;
|
this.annotationType = annotationType;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package test;
|
||||||
|
|
||||||
|
import java.lang.annotation.*;
|
||||||
|
|
||||||
|
@Retention(RetentionPolicy.CLASS)
|
||||||
|
@interface Aaa {
|
||||||
|
}
|
||||||
|
|
||||||
|
class HasAnnotatedMethod {
|
||||||
|
@Aaa
|
||||||
|
public void f() { }
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
annotation class Aaa
|
||||||
|
|
||||||
|
open class HasAnnotatedMethod() {
|
||||||
|
open Aaa fun f(): Unit { }
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
namespace test
|
||||||
|
|
||||||
|
open class test.HasAnnotatedMethod : jet.Any {
|
||||||
|
final /*constructor*/ fun <init>(): test.HasAnnotatedMethod
|
||||||
|
open test.Aaa() fun f(): jet.Tuple0
|
||||||
|
}
|
||||||
|
final annotation class test.Aaa : jet.Any {
|
||||||
|
final /*constructor*/ fun <init>(): test.Aaa
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
package test;
|
||||||
|
|
||||||
|
@interface SimpleAnnotation {
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
annotation class SimpleAnnotation
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
namespace test
|
||||||
|
|
||||||
|
final annotation class test.SimpleAnnotation : jet.Any {
|
||||||
|
final /*constructor*/ fun <init>(): test.SimpleAnnotation
|
||||||
|
}
|
||||||
@@ -33,6 +33,7 @@ import org.jetbrains.jet.lang.descriptors.PropertyDescriptor;
|
|||||||
import org.jetbrains.jet.lang.descriptors.TypeParameterDescriptor;
|
import org.jetbrains.jet.lang.descriptors.TypeParameterDescriptor;
|
||||||
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.VariableDescriptor;
|
||||||
|
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
|
||||||
import org.jetbrains.jet.lang.resolve.java.JavaDescriptorResolver;
|
import org.jetbrains.jet.lang.resolve.java.JavaDescriptorResolver;
|
||||||
import org.jetbrains.jet.lang.resolve.java.JavaNamespaceDescriptor;
|
import org.jetbrains.jet.lang.resolve.java.JavaNamespaceDescriptor;
|
||||||
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
|
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
|
||||||
@@ -200,6 +201,9 @@ class NamespaceComparator {
|
|||||||
case OBJECT:
|
case OBJECT:
|
||||||
sb.append("object");
|
sb.append("object");
|
||||||
break;
|
break;
|
||||||
|
case ANNOTATION_CLASS:
|
||||||
|
sb.append("annotation class");
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
throw new IllegalStateException("unknown class kind: " + kind);
|
throw new IllegalStateException("unknown class kind: " + kind);
|
||||||
}
|
}
|
||||||
@@ -218,6 +222,11 @@ class NamespaceComparator {
|
|||||||
public void serialize(FunctionDescriptor fun) {
|
public void serialize(FunctionDescriptor fun) {
|
||||||
serialize(fun.getModality());
|
serialize(fun.getModality());
|
||||||
sb.append(" ");
|
sb.append(" ");
|
||||||
|
|
||||||
|
if (!fun.getAnnotations().isEmpty()) {
|
||||||
|
new Serializer(sb).serializeSeparated(fun.getAnnotations(), " ");
|
||||||
|
sb.append(" ");
|
||||||
|
}
|
||||||
|
|
||||||
if (!fun.getOverriddenDescriptors().isEmpty()) {
|
if (!fun.getOverriddenDescriptors().isEmpty()) {
|
||||||
sb.append("override /*" + fun.getOverriddenDescriptors().size() + "*/ ");
|
sb.append("override /*" + fun.getOverriddenDescriptors().size() + "*/ ");
|
||||||
@@ -250,6 +259,11 @@ class NamespaceComparator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void serialize(PropertyDescriptor prop) {
|
public void serialize(PropertyDescriptor prop) {
|
||||||
|
if (!prop.getAnnotations().isEmpty()) {
|
||||||
|
new Serializer(sb).serializeSeparated(prop.getAnnotations(), " ");
|
||||||
|
sb.append(" ");
|
||||||
|
}
|
||||||
|
|
||||||
if (prop.isVar()) {
|
if (prop.isVar()) {
|
||||||
sb.append("var ");
|
sb.append("var ");
|
||||||
} else {
|
} else {
|
||||||
@@ -322,6 +336,13 @@ class NamespaceComparator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void serialize(AnnotationDescriptor annotation) {
|
||||||
|
serialize(annotation.getType());
|
||||||
|
sb.append("(");
|
||||||
|
serializeCommaSeparated(annotation.getValueArguments());
|
||||||
|
sb.append(")");
|
||||||
|
}
|
||||||
|
|
||||||
public void serializeCommaSeparated(List<?> list) {
|
public void serializeCommaSeparated(List<?> list) {
|
||||||
serializeSeparated(list, ", ");
|
serializeSeparated(list, ", ");
|
||||||
}
|
}
|
||||||
@@ -460,6 +481,10 @@ class NamespaceComparator {
|
|||||||
|
|
||||||
public void serialize(ClassDescriptor klass) {
|
public void serialize(ClassDescriptor klass) {
|
||||||
|
|
||||||
|
if (!klass.getAnnotations().isEmpty()) {
|
||||||
|
new Serializer(sb).serializeSeparated(klass.getAnnotations(), " ");
|
||||||
|
sb.append(" ");
|
||||||
|
}
|
||||||
serialize(klass.getModality());
|
serialize(klass.getModality());
|
||||||
sb.append(" ");
|
sb.append(" ");
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user