Constructors for Java annotations

This commit is contained in:
Andrey Breslav
2012-01-26 13:43:46 +04:00
parent 7678815f17
commit 92e496c43b
2 changed files with 47 additions and 1 deletions
@@ -243,7 +243,7 @@ public class JavaDescriptorResolver {
// Example:
// class Kotlin() : Java() {}
// abstract public class Java {}
if (!psiClass.isInterface() || psiClass.isAnnotationType()) {
if (!psiClass.isInterface()) {
ConstructorDescriptorImpl constructorDescriptor = new ConstructorDescriptorImpl(
classData.classDescriptor,
Collections.<AnnotationDescriptor>emptyList(),
@@ -253,6 +253,47 @@ public class JavaDescriptorResolver {
classData.classDescriptor.addConstructor(constructorDescriptor, null);
semanticServices.getTrace().record(BindingContext.CONSTRUCTOR, psiClass, constructorDescriptor);
}
if (psiClass.isAnnotationType()) {
// A constructor for an annotation type takes all the "methods" in the @interface as parameters
ConstructorDescriptorImpl constructorDescriptor = new ConstructorDescriptorImpl(
classData.classDescriptor,
Collections.<AnnotationDescriptor>emptyList(),
false);
List<ValueParameterDescriptor> valueParameters = Lists.newArrayList();
PsiMethod[] methods = psiClass.getMethods();
for (int i = 0; i < methods.length; i++) {
PsiMethod method = methods[i];
if (method instanceof PsiAnnotationMethod) {
PsiAnnotationMethod annotationMethod = (PsiAnnotationMethod) method;
assert annotationMethod.getParameterList().getParameters().length == 0;
PsiType returnType = annotationMethod.getReturnType();
// We take the following heuristical convention:
// if the last method of the @interface is an array, we convert it into a vararg
JetType varargElementType = null;
if (i == methods.length - 1 && (returnType instanceof PsiArrayType)) {
varargElementType = semanticServices.getTypeTransformer().transformToType(((PsiArrayType) returnType).getComponentType());
}
valueParameters.add(new ValueParameterDescriptorImpl(
constructorDescriptor,
i,
Collections.<AnnotationDescriptor>emptyList(),
method.getName(),
false,
semanticServices.getTypeTransformer().transformToType(returnType),
annotationMethod.getDefaultValue() != null,
varargElementType));
}
}
constructorDescriptor.initialize(typeParameters, valueParameters, Modality.FINAL, classData.classDescriptor.getVisibility());
constructorDescriptor.setReturnType(classData.classDescriptor.getDefaultType());
classData.classDescriptor.addConstructor(constructorDescriptor, null);
semanticServices.getTrace().record(BindingContext.CONSTRUCTOR, psiClass, constructorDescriptor);
}
}
else {
for (PsiMethod psiConstructor : psiConstructors) {