Support Class<?> as annotation arguments in java

This commit is contained in:
Alexander Udalov
2013-09-06 20:49:11 +04:00
parent b8f9ef9910
commit c0a4d8d24f
13 changed files with 170 additions and 11 deletions
@@ -168,7 +168,7 @@ public class AnnotationDescriptorDeserializer implements AnnotationDeserializer
annotation.setAnnotationType(annotationClass.getDefaultType());
return new AnnotationVisitor(Opcodes.ASM4) {
// TODO: arrays, annotations
// TODO: arrays, annotations, java.lang.Class
@Override
public void visit(String name, Object value) {
CompileTimeConstant<?> argument = JavaAnnotationArgumentResolver.resolveCompileTimeConstantValue(value, null);
@@ -29,19 +29,26 @@ import org.jetbrains.jet.lang.resolve.java.structure.*;
import org.jetbrains.jet.lang.resolve.name.FqName;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.JetTypeImpl;
import org.jetbrains.jet.lang.types.TypeProjection;
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
import javax.inject.Inject;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import static org.jetbrains.jet.lang.resolve.DescriptorUtils.getEnumEntriesScope;
import static org.jetbrains.jet.lang.resolve.java.DescriptorSearchRule.IGNORE_KOTLIN_SOURCES;
import static org.jetbrains.jet.lang.resolve.java.DescriptorSearchRule.INCLUDE_KOTLIN_SOURCES;
public final class JavaAnnotationArgumentResolver {
public static final FqName JL_CLASS_FQ_NAME = new FqName("java.lang.Class");
private JavaAnnotationResolver annotationResolver;
private JavaClassResolver classResolver;
private JavaTypeTransformer typeTransformer;
@Inject
public void setAnnotationResolver(JavaAnnotationResolver annotationResolver) {
@@ -53,6 +60,11 @@ public final class JavaAnnotationArgumentResolver {
this.classResolver = classResolver;
}
@Inject
public void setTypeTransformer(JavaTypeTransformer typeTransformer) {
this.typeTransformer = typeTransformer;
}
@Nullable
public CompileTimeConstant<?> resolveAnnotationArgument(
@NotNull FqName annotationFqName,
@@ -80,6 +92,10 @@ public final class JavaAnnotationArgumentResolver {
else if (argument instanceof JavaAnnotationAsAnnotationArgument) {
return resolveFromAnnotation(((JavaAnnotationAsAnnotationArgument) argument).getAnnotation(), postponedTasks);
}
// Class<?>
else if (argument instanceof JavaClassObjectAnnotationArgument) {
return resolveFromJavaClassObjectType(((JavaClassObjectAnnotationArgument) argument).getReferencedType());
}
return null;
}
@@ -135,6 +151,25 @@ public final class JavaAnnotationArgumentResolver {
return null;
}
@Nullable
private CompileTimeConstant<?> resolveFromJavaClassObjectType(@NotNull JavaType javaType) {
JetType type = typeTransformer.transformToType(javaType, TypeVariableResolver.EMPTY);
ClassDescriptor jlClass = classResolver.resolveClass(JL_CLASS_FQ_NAME, IGNORE_KOTLIN_SOURCES);
if (jlClass == null) return null;
List<TypeProjection> arguments = Collections.singletonList(new TypeProjection(type));
JetTypeImpl javaClassType = new JetTypeImpl(
jlClass.getAnnotations(),
jlClass.getTypeConstructor(),
false,
arguments,
jlClass.getMemberScope(arguments)
);
return new JavaClassValue(javaClassType);
}
@Nullable
public static CompileTimeConstant<?> resolveCompileTimeConstantValue(@Nullable Object value, @Nullable JetType expectedType) {
if (value instanceof String) {
@@ -0,0 +1,24 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.lang.resolve.java.structure;
import org.jetbrains.annotations.NotNull;
public interface JavaClassObjectAnnotationArgument extends JavaAnnotationArgument {
@NotNull
JavaType getReferencedType();
}