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.DescriptorUtils;
|
||||
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.kt.JetClassAnnotation;
|
||||
import org.jetbrains.jet.lang.types.*;
|
||||
@@ -282,7 +283,8 @@ public class JavaDescriptorResolver {
|
||||
DeclarationDescriptor containingDeclaration = resolveParentDescriptor(psiClass);
|
||||
classData.classDescriptor = new MutableClassDescriptorLite(containingDeclaration, kind);
|
||||
classData.classDescriptor.setName(name);
|
||||
|
||||
classData.classDescriptor.setAnnotations(resolveAnnotations(psiClass));
|
||||
|
||||
List<JetType> supertypes = new ArrayList<JetType>();
|
||||
|
||||
TypeVariableResolverFromOuters outerTypeVariableByNameResolver = new TypeVariableResolverFromOuters(containingDeclaration);
|
||||
@@ -297,10 +299,16 @@ public class JavaDescriptorResolver {
|
||||
classData.classDescriptor.setTypeParameterDescriptors(typeParameters);
|
||||
classData.classDescriptor.setSupertypes(supertypes);
|
||||
classData.classDescriptor.setVisibility(resolveVisibilityFromPsiModifiers(psiClass));
|
||||
classData.classDescriptor.setModality(Modality.convertFromFlags(
|
||||
psiClass.hasModifierProperty(PsiModifier.ABSTRACT) || psiClass.isInterface(),
|
||||
!psiClass.hasModifierProperty(PsiModifier.FINAL))
|
||||
);
|
||||
Modality modality;
|
||||
if (classData.classDescriptor.getKind() == ClassKind.ANNOTATION_CLASS) {
|
||||
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.setScopeForMemberLookup(new JavaClassMembersScope(classData.classDescriptor, psiClass, semanticServices, false));
|
||||
|
||||
@@ -700,8 +708,8 @@ public class JavaDescriptorResolver {
|
||||
}
|
||||
});
|
||||
} else {
|
||||
transformSupertypeList(result, psiClass.getPsiClass().getExtendsListTypes(), new TypeVariableResolverFromTypeDescriptors(typeParameters, null));
|
||||
transformSupertypeList(result, psiClass.getPsiClass().getImplementsListTypes(), 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), classDescriptor.getKind() == ClassKind.ANNOTATION_CLASS);
|
||||
}
|
||||
|
||||
for (JetType supertype : result) {
|
||||
@@ -716,12 +724,15 @@ public class JavaDescriptorResolver {
|
||||
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) {
|
||||
PsiClass resolved = type.resolve();
|
||||
if (resolved != null && resolved.getQualifiedName().equals(JvmStdlibNames.JET_OBJECT.getFqName())) {
|
||||
continue;
|
||||
}
|
||||
if (annotation && resolved.getQualifiedName().equals("java.lang.annotation.Annotation")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
JetType transform = semanticServices.getTypeTransformer().transformToType(type, typeVariableResolver);
|
||||
|
||||
@@ -1373,7 +1384,7 @@ public class JavaDescriptorResolver {
|
||||
|
||||
NamedFunctionDescriptorImpl functionDescriptorImpl = new NamedFunctionDescriptorImpl(
|
||||
owner,
|
||||
Collections.<AnnotationDescriptor>emptyList(), // TODO
|
||||
resolveAnnotations(method.getPsiMethod()),
|
||||
method.getName(),
|
||||
CallableMemberDescriptor.Kind.DECLARATION
|
||||
);
|
||||
@@ -1405,6 +1416,37 @@ public class JavaDescriptorResolver {
|
||||
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) {
|
||||
ResolverScopeData scopeData = getResolverScopeData(containingDeclaration, new PsiClassWrapper(psiClass));
|
||||
|
||||
|
||||
+1
-1
@@ -133,7 +133,7 @@ public class LazySubstitutingClassDescriptor implements ClassDescriptor {
|
||||
|
||||
@Override
|
||||
public List<AnnotationDescriptor> getAnnotations() {
|
||||
throw new UnsupportedOperationException(); // TODO
|
||||
return original.getAnnotations();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
|
||||
+5
-1
@@ -40,7 +40,7 @@ public class MutableClassDescriptorLite extends MutableDeclarationDescriptor imp
|
||||
private ConstructorDescriptor primaryConstructor;
|
||||
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();
|
||||
|
||||
@@ -342,4 +342,8 @@ public class MutableClassDescriptorLite extends MutableDeclarationDescriptor imp
|
||||
public List<AnnotationDescriptor> getAnnotations() {
|
||||
return annotations;
|
||||
}
|
||||
|
||||
public void setAnnotations(List<AnnotationDescriptor> annotations) {
|
||||
this.annotations = annotations;
|
||||
}
|
||||
}
|
||||
|
||||
+5
@@ -17,9 +17,11 @@
|
||||
package org.jetbrains.jet.lang.descriptors.annotations;
|
||||
|
||||
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.types.JetType;
|
||||
|
||||
import javax.rmi.CORBA.ClassDesc;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -40,6 +42,9 @@ public class AnnotationDescriptor {
|
||||
}
|
||||
|
||||
public void setAnnotationType(@NotNull JetType annotationType) {
|
||||
if (!(annotationType.getConstructor().getDeclarationDescriptor() instanceof ClassDescriptor)) {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
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.ValueParameterDescriptor;
|
||||
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.JavaNamespaceDescriptor;
|
||||
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
|
||||
@@ -200,6 +201,9 @@ class NamespaceComparator {
|
||||
case OBJECT:
|
||||
sb.append("object");
|
||||
break;
|
||||
case ANNOTATION_CLASS:
|
||||
sb.append("annotation class");
|
||||
break;
|
||||
default:
|
||||
throw new IllegalStateException("unknown class kind: " + kind);
|
||||
}
|
||||
@@ -218,6 +222,11 @@ class NamespaceComparator {
|
||||
public void serialize(FunctionDescriptor fun) {
|
||||
serialize(fun.getModality());
|
||||
sb.append(" ");
|
||||
|
||||
if (!fun.getAnnotations().isEmpty()) {
|
||||
new Serializer(sb).serializeSeparated(fun.getAnnotations(), " ");
|
||||
sb.append(" ");
|
||||
}
|
||||
|
||||
if (!fun.getOverriddenDescriptors().isEmpty()) {
|
||||
sb.append("override /*" + fun.getOverriddenDescriptors().size() + "*/ ");
|
||||
@@ -250,6 +259,11 @@ class NamespaceComparator {
|
||||
}
|
||||
|
||||
public void serialize(PropertyDescriptor prop) {
|
||||
if (!prop.getAnnotations().isEmpty()) {
|
||||
new Serializer(sb).serializeSeparated(prop.getAnnotations(), " ");
|
||||
sb.append(" ");
|
||||
}
|
||||
|
||||
if (prop.isVar()) {
|
||||
sb.append("var ");
|
||||
} 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) {
|
||||
serializeSeparated(list, ", ");
|
||||
}
|
||||
@@ -460,6 +481,10 @@ class NamespaceComparator {
|
||||
|
||||
public void serialize(ClassDescriptor klass) {
|
||||
|
||||
if (!klass.getAnnotations().isEmpty()) {
|
||||
new Serializer(sb).serializeSeparated(klass.getAnnotations(), " ");
|
||||
sb.append(" ");
|
||||
}
|
||||
serialize(klass.getModality());
|
||||
sb.append(" ");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user