Generate @Nullable/@NotNull annotations on Kotlin declarations (for IDEA interop)

This commit is contained in:
Andrey Breslav
2013-11-08 18:39:43 +04:00
parent 4268d8aa27
commit 2873f74932
13 changed files with 405 additions and 11 deletions
@@ -19,10 +19,7 @@ package org.jetbrains.jet.codegen;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.asm4.AnnotationVisitor;
import org.jetbrains.asm4.ClassVisitor;
import org.jetbrains.asm4.FieldVisitor;
import org.jetbrains.asm4.MethodVisitor;
import org.jetbrains.asm4.*;
import org.jetbrains.jet.codegen.state.JetTypeMapper;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.descriptors.annotations.Annotated;
@@ -36,11 +33,11 @@ import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall;
import org.jetbrains.jet.lang.resolve.constants.*;
import org.jetbrains.jet.lang.resolve.constants.StringValue;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
import java.lang.annotation.RetentionPolicy;
import java.util.List;
import java.util.Map;
import java.util.*;
import static org.jetbrains.jet.lang.resolve.BindingContextUtils.descriptorToDeclaration;
@@ -73,9 +70,12 @@ public abstract class AnnotationCodegen {
}
if (modifierList == null) {
generateAdditionalAnnotations(annotated, Collections.<String>emptySet());
return;
}
Set<String> annotationDescriptorsAlreadyPresent = new HashSet<String>();
List<JetAnnotationEntry> annotationEntries = modifierList.getAnnotationEntries();
for (JetAnnotationEntry annotationEntry : annotationEntries) {
ResolvedCall<? extends CallableDescriptor> resolvedCall =
@@ -86,7 +86,29 @@ public abstract class AnnotationCodegen {
if (annotationDescriptor == null) continue; // Skipping annotations if they are not resolved. Needed for JetLightClass generation
if (isVolatile(annotationDescriptor)) continue;
genAnnotation(annotationDescriptor);
String descriptor = genAnnotation(annotationDescriptor);
if (descriptor != null) {
annotationDescriptorsAlreadyPresent.add(descriptor);
}
}
generateAdditionalAnnotations(annotated, annotationDescriptorsAlreadyPresent);
}
private void generateAdditionalAnnotations(@NotNull Annotated annotated, @NotNull Set<String> annotationDescriptorsAlreadyPresent) {
if (annotated instanceof CallableDescriptor) {
generateNullabilityAnnotation(((CallableDescriptor) annotated).getReturnType(), annotationDescriptorsAlreadyPresent);
}
}
private void generateNullabilityAnnotation(@Nullable JetType type, @NotNull Set<String> annotationDescriptorsAlreadyPresent) {
if (type == null) return;
Class<?> annotationClass = CodegenUtil.isNullableType(type) ? Nullable.class : NotNull.class;
String descriptor = Type.getType(annotationClass).getDescriptor();
if (!annotationDescriptorsAlreadyPresent.contains(descriptor)) {
visitAnnotation(descriptor, false).visitEnd();
}
}
@@ -101,18 +123,21 @@ public abstract class AnnotationCodegen {
visitor.visitEnd();
}
private void genAnnotation(AnnotationDescriptor annotationDescriptor) {
@Nullable
private String genAnnotation(AnnotationDescriptor annotationDescriptor) {
ClassifierDescriptor classifierDescriptor = annotationDescriptor.getType().getConstructor().getDeclarationDescriptor();
RetentionPolicy rp = getRetentionPolicy(classifierDescriptor, typeMapper);
if (rp == RetentionPolicy.SOURCE) {
return;
return null;
}
String internalName = typeMapper.mapType(annotationDescriptor.getType()).getDescriptor();
AnnotationVisitor annotationVisitor = visitAnnotation(internalName, rp == RetentionPolicy.RUNTIME);
String descriptor = typeMapper.mapType(annotationDescriptor.getType()).getDescriptor();
AnnotationVisitor annotationVisitor = visitAnnotation(descriptor, rp == RetentionPolicy.RUNTIME);
genAnnotationArguments(annotationDescriptor, annotationVisitor);
annotationVisitor.visitEnd();
return descriptor;
}
private void genAnnotationArguments(AnnotationDescriptor annotationDescriptor, AnnotationVisitor annotationVisitor) {
@@ -0,0 +1,74 @@
public final class Class implements jet.JetObject {
@org.jetbrains.annotations.Nullable
private final java.lang.String nullableVal = "";
@org.jetbrains.annotations.Nullable
private java.lang.String nullableVar;
@org.jetbrains.annotations.NotNull
private final java.lang.String notNullVal = "";
@org.jetbrains.annotations.NotNull
private java.lang.String notNullVar;
@org.jetbrains.annotations.NotNull
public final java.lang.String notNull(@org.jetbrains.annotations.NotNull @jet.runtime.typeinfo.JetValueParameter(name = "a") java.lang.String p) { /* compiled code */ }
@org.jetbrains.annotations.Nullable
public final java.lang.String nullable(@org.jetbrains.annotations.Nullable @jet.runtime.typeinfo.JetValueParameter(name = "a", type = "?") java.lang.String p) { /* compiled code */ }
@org.jetbrains.annotations.NotNull
public final java.lang.String notNullWithNN() { /* compiled code */ }
@org.jetbrains.annotations.Nullable
@org.jetbrains.annotations.NotNull
public final java.lang.String notNullWithN() { /* compiled code */ }
@org.jetbrains.annotations.Nullable
public final java.lang.String nullableWithN() { /* compiled code */ }
@org.jetbrains.annotations.NotNull
@org.jetbrains.annotations.Nullable
public final java.lang.String nullableWithNN() { /* compiled code */ }
@org.jetbrains.annotations.Nullable
public final java.lang.String getNullableVal() { /* compiled code */ }
@org.jetbrains.annotations.Nullable
public final java.lang.String getNullableVar() { /* compiled code */ }
@org.jetbrains.annotations.NotNull
public final void setNullableVar(@org.jetbrains.annotations.Nullable @jet.runtime.typeinfo.JetValueParameter(name = "<set-?>", type = "?") java.lang.String p) { /* compiled code */ }
@org.jetbrains.annotations.NotNull
public final java.lang.String getNotNullVal() { /* compiled code */ }
@org.jetbrains.annotations.NotNull
public final java.lang.String getNotNullVar() { /* compiled code */ }
@org.jetbrains.annotations.NotNull
public final void setNotNullVar(@org.jetbrains.annotations.NotNull @jet.runtime.typeinfo.JetValueParameter(name = "<set-?>") java.lang.String p) { /* compiled code */ }
@org.jetbrains.annotations.Nullable
@org.jetbrains.annotations.NotNull
public final java.lang.String getNotNullValWithGet() { /* compiled code */ }
@org.jetbrains.annotations.Nullable
@org.jetbrains.annotations.NotNull
public final java.lang.String getNotNullVarWithGetSet() { /* compiled code */ }
@org.jetbrains.annotations.Nullable
@org.jetbrains.annotations.NotNull
public final void setNotNullVarWithGetSet(@org.jetbrains.annotations.NotNull @jet.runtime.typeinfo.JetValueParameter(name = "v") java.lang.String p) { /* compiled code */ }
@org.jetbrains.annotations.NotNull
@org.jetbrains.annotations.Nullable
public final java.lang.String getNullableValWithGet() { /* compiled code */ }
@org.jetbrains.annotations.NotNull
@org.jetbrains.annotations.Nullable
public final java.lang.String getNullableVarWithGetSet() { /* compiled code */ }
@org.jetbrains.annotations.NotNull
public final void setNullableVarWithGetSet(@org.jetbrains.annotations.Nullable @jet.runtime.typeinfo.JetValueParameter(name = "v", type = "?") java.lang.String p) { /* compiled code */ }
@org.jetbrains.annotations.NotNull
public Class() { /* compiled code */ }
}
@@ -0,0 +1,32 @@
import org.jetbrains.annotations.NotNull
import org.jetbrains.annotations.Nullable
class Class {
fun notNull(a: String): String = ""
fun nullable(a: String?): String? = ""
NotNull fun notNullWithNN(): String = ""
Nullable fun notNullWithN(): String = ""
Nullable fun nullableWithN(): String? = ""
NotNull fun nullableWithNN(): String? = ""
val nullableVal: String? = ""
var nullableVar: String? = ""
val notNullVal: String = ""
var notNullVar: String = ""
val notNullValWithGet: String
[Nullable] get() = ""
var notNullVarWithGetSet: String
[Nullable] get() = ""
[Nullable] set(v) {}
val nullableValWithGet: String?
[NotNull] get() = ""
var nullableVarWithGetSet: String?
[NotNull] get() = ""
[NotNull] set(v) {}
}
@@ -0,0 +1,4 @@
public final class ClassWithConstructor implements jet.JetObject {
@org.jetbrains.annotations.NotNull
public ClassWithConstructor(@org.jetbrains.annotations.Nullable @jet.runtime.typeinfo.JetValueParameter(name = "nullable", type = "?") java.lang.String p, @org.jetbrains.annotations.NotNull @jet.runtime.typeinfo.JetValueParameter(name = "notNull") java.lang.String p1) { /* compiled code */ }
}
@@ -0,0 +1,4 @@
class ClassWithConstructor(
nullable: String?,
notNull: String
)
@@ -0,0 +1,15 @@
public final class ClassWithConstructorAndProperties implements jet.JetObject {
@org.jetbrains.annotations.Nullable
private final java.lang.String nullable;
@org.jetbrains.annotations.NotNull
private final java.lang.String notNull;
@org.jetbrains.annotations.Nullable
public final java.lang.String getNullable() { /* compiled code */ }
@org.jetbrains.annotations.NotNull
public final java.lang.String getNotNull() { /* compiled code */ }
@org.jetbrains.annotations.NotNull
public ClassWithConstructorAndProperties(@org.jetbrains.annotations.Nullable @jet.runtime.typeinfo.JetValueParameter(name = "nullable", type = "?") java.lang.String p, @org.jetbrains.annotations.NotNull @jet.runtime.typeinfo.JetValueParameter(name = "notNull") java.lang.String p1) { /* compiled code */ }
}
@@ -0,0 +1,4 @@
class ClassWithConstructorAndProperties(
val nullable: String?,
val notNull: String
)
@@ -0,0 +1,39 @@
public interface Trait extends jet.JetObject {
@org.jetbrains.annotations.NotNull
java.lang.String notNull(@org.jetbrains.annotations.NotNull @jet.runtime.typeinfo.JetValueParameter(name = "a") java.lang.String p);
@org.jetbrains.annotations.Nullable
java.lang.String nullable(@org.jetbrains.annotations.Nullable @jet.runtime.typeinfo.JetValueParameter(name = "a", type = "?") java.lang.String p);
@org.jetbrains.annotations.NotNull
java.lang.String notNullWithNN();
@org.jetbrains.annotations.Nullable
@org.jetbrains.annotations.NotNull
java.lang.String notNullWithN();
@org.jetbrains.annotations.Nullable
java.lang.String nullableWithN();
@org.jetbrains.annotations.NotNull
@org.jetbrains.annotations.Nullable
java.lang.String nullableWithNN();
@org.jetbrains.annotations.Nullable
java.lang.String getNullableVal();
@org.jetbrains.annotations.Nullable
java.lang.String getNullableVar();
@org.jetbrains.annotations.NotNull
void setNullableVar(@org.jetbrains.annotations.Nullable @jet.runtime.typeinfo.JetValueParameter(name = "<set-?>", type = "?") java.lang.String p);
@org.jetbrains.annotations.NotNull
java.lang.String getNotNullVal();
@org.jetbrains.annotations.NotNull
java.lang.String getNotNullVar();
@org.jetbrains.annotations.NotNull
void setNotNullVar(@org.jetbrains.annotations.NotNull @jet.runtime.typeinfo.JetValueParameter(name = "<set-?>") java.lang.String p);
}
@@ -0,0 +1,18 @@
import org.jetbrains.annotations.NotNull
import org.jetbrains.annotations.Nullable
trait Trait {
fun notNull(a: String): String
fun nullable(a: String?): String?
NotNull fun notNullWithNN(): String
Nullable fun notNullWithN(): String
Nullable fun nullableWithN(): String?
NotNull fun nullableWithNN(): String?
val nullableVal: String?
var nullableVar: String?
val notNullVal: String
var notNullVar: String
}
@@ -0,0 +1,62 @@
public final class _DefaultPackage {
@org.jetbrains.annotations.NotNull
public static final java.lang.String notNull(@org.jetbrains.annotations.NotNull @jet.runtime.typeinfo.JetValueParameter(name = "a") java.lang.String p) { /* compiled code */ }
@org.jetbrains.annotations.Nullable
public static final java.lang.String nullable(@org.jetbrains.annotations.Nullable @jet.runtime.typeinfo.JetValueParameter(name = "a", type = "?") java.lang.String p) { /* compiled code */ }
@org.jetbrains.annotations.NotNull
public static final java.lang.String notNullWithNN() { /* compiled code */ }
@org.jetbrains.annotations.Nullable
@org.jetbrains.annotations.NotNull
public static final java.lang.String notNullWithN() { /* compiled code */ }
@org.jetbrains.annotations.Nullable
public static final java.lang.String nullableWithN() { /* compiled code */ }
@org.jetbrains.annotations.NotNull
@org.jetbrains.annotations.Nullable
public static final java.lang.String nullableWithNN() { /* compiled code */ }
@org.jetbrains.annotations.Nullable
public static final java.lang.String getNullableVal() { /* compiled code */ }
@org.jetbrains.annotations.Nullable
public static final java.lang.String getNullableVar() { /* compiled code */ }
@org.jetbrains.annotations.NotNull
public static final void setNullableVar(@org.jetbrains.annotations.Nullable @jet.runtime.typeinfo.JetValueParameter(name = "<set-?>", type = "?") java.lang.String p) { /* compiled code */ }
@org.jetbrains.annotations.NotNull
public static final java.lang.String getNotNullVal() { /* compiled code */ }
@org.jetbrains.annotations.NotNull
public static final java.lang.String getNotNullVar() { /* compiled code */ }
@org.jetbrains.annotations.NotNull
public static final void setNotNullVar(@org.jetbrains.annotations.NotNull @jet.runtime.typeinfo.JetValueParameter(name = "<set-?>") java.lang.String p) { /* compiled code */ }
@org.jetbrains.annotations.Nullable
@org.jetbrains.annotations.NotNull
public static final java.lang.String getNotNullValWithGet() { /* compiled code */ }
@org.jetbrains.annotations.Nullable
@org.jetbrains.annotations.NotNull
public static final java.lang.String getNotNullVarWithGetSet() { /* compiled code */ }
@org.jetbrains.annotations.Nullable
@org.jetbrains.annotations.NotNull
public static final void setNotNullVarWithGetSet(@org.jetbrains.annotations.NotNull @jet.runtime.typeinfo.JetValueParameter(name = "v") java.lang.String p) { /* compiled code */ }
@org.jetbrains.annotations.NotNull
@org.jetbrains.annotations.Nullable
public static final java.lang.String getNullableValWithGet() { /* compiled code */ }
@org.jetbrains.annotations.NotNull
@org.jetbrains.annotations.Nullable
public static final java.lang.String getNullableVarWithGetSet() { /* compiled code */ }
@org.jetbrains.annotations.NotNull
public static final void setNullableVarWithGetSet(@org.jetbrains.annotations.Nullable @jet.runtime.typeinfo.JetValueParameter(name = "v", type = "?") java.lang.String p) { /* compiled code */ }
}
@@ -0,0 +1,30 @@
import org.jetbrains.annotations.NotNull
import org.jetbrains.annotations.Nullable
fun notNull(a: String): String = ""
fun nullable(a: String?): String? = ""
NotNull fun notNullWithNN(): String = ""
Nullable fun notNullWithN(): String = ""
Nullable fun nullableWithN(): String? = ""
NotNull fun nullableWithNN(): String? = ""
val nullableVal: String? = ""
var nullableVar: String? = ""
val notNullVal: String = ""
var notNullVar: String = ""
val notNullValWithGet: String
[Nullable] get() = ""
var notNullVarWithGetSet: String
[Nullable] get() = ""
[Nullable] set(v) {}
val nullableValWithGet: String?
[NotNull] get() = ""
var nullableVarWithGetSet: String?
[NotNull] get() = ""
[NotNull] set(v) {}
@@ -16,6 +16,7 @@
package org.jetbrains.jet.asJava;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.JetTestUtils;
import org.jetbrains.jet.cli.jvm.compiler.JetCoreEnvironment;
import org.jetbrains.jet.config.CommonConfigurationKeys;
@@ -36,9 +37,14 @@ public abstract class KotlinAsJavaTestBase extends KotlinTestWithEnvironment {
configuration.add(CommonConfigurationKeys.SOURCE_ROOTS_KEY, root.getPath());
}
extraConfiguration(configuration);
return JetCoreEnvironment.createForTests(getTestRootDisposable(), configuration);
}
protected void extraConfiguration(@NotNull CompilerConfiguration configuration) {
}
protected abstract List<File> getKotlinSourceRoots();
@Override
@@ -0,0 +1,81 @@
/*
* 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.asJava;
import com.intellij.psi.PsiClass;
import com.intellij.psi.impl.compiled.ClsElementImpl;
import com.intellij.psi.search.GlobalSearchScope;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.JetTestUtils;
import org.jetbrains.jet.cli.jvm.JVMConfigurationKeys;
import org.jetbrains.jet.config.CompilerConfiguration;
import java.io.File;
import java.util.Collections;
import java.util.List;
public class NullabilityAnnotationsTest extends KotlinAsJavaTestBase {
private final File testDir = new File("compiler/testData/asJava/nullabilityAnnotations");
@Override
protected List<File> getKotlinSourceRoots() {
return Collections.singletonList(new File(testDir, getTestName(false) + ".kt"));
}
@Override
protected void extraConfiguration(@NotNull CompilerConfiguration configuration) {
configuration.add(JVMConfigurationKeys.CLASSPATH_KEY, JetTestUtils.getAnnotationsJar());
}
public void test_DefaultPackage() throws Exception {
doTest(getTestName(false));
}
public void testClass() throws Exception {
doTest(getTestName(false));
}
public void testTrait() throws Exception {
doTest(getTestName(false));
}
public void testClassWithConstructorAndProperties() throws Exception {
doTest(getTestName(false));
}
public void testClassWithConstructor() throws Exception {
doTest(getTestName(false));
}
private void doTest(@NotNull String fqName) {
PsiClass psiClass = finder.findClass(fqName, GlobalSearchScope.allScope(getProject()));
if (!(psiClass instanceof KotlinLightClass)) {
throw new IllegalStateException("Not a light class: " + psiClass);
}
PsiClass delegate = ((KotlinLightClass) psiClass).getDelegate();
if (!(delegate instanceof ClsElementImpl)) {
throw new IllegalStateException("Not a CLS element: " + delegate);
}
StringBuilder buffer = new StringBuilder();
((ClsElementImpl) delegate).appendMirrorText(0, buffer);
String actual = buffer.toString();
JetTestUtils.assertEqualsToFile(new File(testDir, fqName + ".java"), actual);
}
}