visitAnnotation() could return null for light classes

This commit is contained in:
Andrey Breslav
2013-11-11 12:56:13 +04:00
parent 2873f74932
commit f3901d2844
6 changed files with 73 additions and 10 deletions
@@ -0,0 +1,18 @@
<root>
<item name='org.jetbrains.asm4.ClassVisitor org.jetbrains.asm4.AnnotationVisitor visitAnnotation(java.lang.String, boolean)'>
<annotation name='org.jetbrains.annotations.Nullable'/>
</item>
<item name='org.jetbrains.asm4.FieldVisitor org.jetbrains.asm4.AnnotationVisitor visitAnnotation(java.lang.String, boolean)'>
<annotation name='org.jetbrains.annotations.Nullable'/>
</item>
<item name='org.jetbrains.asm4.MethodVisitor org.jetbrains.asm4.AnnotationVisitor visitAnnotation(java.lang.String, boolean)'>
<annotation name='org.jetbrains.annotations.Nullable'/>
</item>
<item name='org.jetbrains.asm4.MethodVisitor org.jetbrains.asm4.AnnotationVisitor visitAnnotationDefault()'>
<annotation name='org.jetbrains.annotations.Nullable'/>
</item>
<item
name='org.jetbrains.asm4.MethodVisitor org.jetbrains.asm4.AnnotationVisitor visitParameterAnnotation(int, java.lang.String, boolean)'>
<annotation name='org.jetbrains.annotations.Nullable'/>
</item>
</root>
@@ -42,6 +42,8 @@ import java.util.*;
import static org.jetbrains.jet.lang.resolve.BindingContextUtils.descriptorToDeclaration;
public abstract class AnnotationCodegen {
private static final AnnotationVisitor NO_ANNOTATION_VISITOR = new AnnotationVisitor(Opcodes.ASM4) {};
private final JetTypeMapper typeMapper;
private final BindingContext bindingContext;
@@ -271,50 +273,61 @@ public abstract class AnnotationCodegen {
return RetentionPolicy.CLASS;
}
@NotNull
abstract AnnotationVisitor visitAnnotation(String descr, boolean visible);
public static AnnotationCodegen forClass(final ClassVisitor cv, JetTypeMapper mapper) {
return new AnnotationCodegen(mapper) {
@NotNull
@Override
AnnotationVisitor visitAnnotation(String descr, boolean visible) {
return cv.visitAnnotation(descr, visible);
return safe(cv.visitAnnotation(descr, visible));
}
};
}
public static AnnotationCodegen forMethod(final MethodVisitor mv, JetTypeMapper mapper) {
return new AnnotationCodegen(mapper) {
@NotNull
@Override
AnnotationVisitor visitAnnotation(String descr, boolean visible) {
return mv.visitAnnotation(descr, visible);
return safe(mv.visitAnnotation(descr, visible));
}
};
}
public static AnnotationCodegen forField(final FieldVisitor fv, JetTypeMapper mapper) {
return new AnnotationCodegen(mapper) {
@NotNull
@Override
AnnotationVisitor visitAnnotation(String descr, boolean visible) {
return fv.visitAnnotation(descr, visible);
return safe(fv.visitAnnotation(descr, visible));
}
};
}
public static AnnotationCodegen forParameter(final int parameter, final MethodVisitor mv, JetTypeMapper mapper) {
return new AnnotationCodegen(mapper) {
@NotNull
@Override
AnnotationVisitor visitAnnotation(String descr, boolean visible) {
return mv.visitParameterAnnotation(parameter, descr, visible);
return safe(mv.visitParameterAnnotation(parameter, descr, visible));
}
};
}
public static AnnotationCodegen forAnnotationDefaultValue(final MethodVisitor mv, JetTypeMapper mapper) {
return new AnnotationCodegen(mapper) {
@NotNull
@Override
AnnotationVisitor visitAnnotation(String descr, boolean visible) {
return mv.visitAnnotationDefault();
return safe(mv.visitAnnotationDefault());
}
};
}
@NotNull
private static AnnotationVisitor safe(@Nullable AnnotationVisitor av) {
return av == null ? NO_ANNOTATION_VISITOR : av;
}
}
@@ -203,11 +203,13 @@ public class FunctionCodegen extends ParentCodegenAwareImpl {
AnnotationVisitor av =
mv.visitParameterAnnotation(i, asmDescByFqNameWithoutInnerClasses(fqNameByClass(JetValueParameter.class)), true);
av.visit("name", name);
if (nullableType) {
av.visit("type", "?");
if (av != null) {
av.visit("name", name);
if (nullableType) {
av.visit("type", "?");
}
av.visitEnd();
}
av.visitEnd();
}
}
@@ -219,7 +221,9 @@ public class FunctionCodegen extends ParentCodegenAwareImpl {
// see MethodWriter.visitParameterAnnotation()
AnnotationVisitor av = mv.visitParameterAnnotation(i, "Ljava/lang/Synthetic;", true);
av.visitEnd();
if (av != null) {
av.visitEnd();
}
}
@Nullable
@@ -0,0 +1,15 @@
public final class Synthetic implements jet.JetObject {
@org.jetbrains.annotations.NotNull
private final void foo() { /* compiled code */ }
@org.jetbrains.annotations.NotNull
public Synthetic() { /* compiled code */ }
public final class Inner implements jet.JetObject {
@org.jetbrains.annotations.NotNull
public final void test() { /* compiled code */ }
@org.jetbrains.annotations.NotNull
public Inner() { /* compiled code */ }
}
}
@@ -0,0 +1,9 @@
class Synthetic {
inner class Inner {
fun test() {
foo()
}
}
private fun foo() {}
}
@@ -61,6 +61,10 @@ public class NullabilityAnnotationsTest extends KotlinAsJavaTestBase {
doTest(getTestName(false));
}
public void testSynthetic() throws Exception {
doTest(getTestName(false));
}
private void doTest(@NotNull String fqName) {
PsiClass psiClass = finder.findClass(fqName, GlobalSearchScope.allScope(getProject()));
if (!(psiClass instanceof KotlinLightClass)) {