Prohibit use of positioned value arguments for java annotation

This commit is contained in:
Denis Zharkov
2015-04-06 16:32:27 +03:00
parent a29c0ff9a3
commit 1324667bc9
15 changed files with 227 additions and 1 deletions
@@ -0,0 +1,45 @@
/*
* Copyright 2010-2015 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.kotlin.load.kotlin
import org.jetbrains.kotlin.descriptors.CallableDescriptor
import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.load.java.JvmAnnotationNames
import org.jetbrains.kotlin.load.java.descriptors.JavaConstructorDescriptor
import org.jetbrains.kotlin.resolve.calls.checkers.CallChecker
import org.jetbrains.kotlin.resolve.calls.context.BasicCallResolutionContext
import org.jetbrains.kotlin.resolve.calls.model.ExpressionValueArgument
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
import org.jetbrains.kotlin.resolve.jvm.diagnostics.ErrorsJvm
public class JavaAnnotationCallChecker : CallChecker {
override fun <F : CallableDescriptor?> check(resolvedCall: ResolvedCall<F>, context: BasicCallResolutionContext) {
val resultingDescriptor = resolvedCall.getResultingDescriptor().getOriginal()
if (resultingDescriptor !is JavaConstructorDescriptor ||
resultingDescriptor.getContainingDeclaration().getKind() != ClassKind.ANNOTATION_CLASS) return
resolvedCall.getValueArguments().filter {
p -> p.key.getName() != JvmAnnotationNames.DEFAULT_ANNOTATION_MEMBER_NAME &&
p.value is ExpressionValueArgument &&
!((p.value as ExpressionValueArgument).getValueArgument()?.isNamed() ?: true)
}.forEach {
context.trace.report(
ErrorsJvm.POSITIONED_VALUE_ARGUMENT_FOR_JAVA_ANNOTATION.on(
it.getValue().getArguments().first().getArgumentExpression()))
}
}
}
@@ -52,7 +52,7 @@ public object KotlinJvmCheckerProvider : AdditionalCheckerProvider(
ReifiedTypeParameterAnnotationChecker(),
NativeFunChecker(),
OverloadsAnnotationChecker()),
additionalCallCheckers = listOf(NeedSyntheticChecker()),
additionalCallCheckers = listOf(NeedSyntheticChecker(), JavaAnnotationCallChecker()),
additionalTypeCheckers = listOf(JavaNullabilityWarningsChecker())
)
@@ -56,6 +56,8 @@ public class DefaultErrorMessagesJvm implements DefaultErrorMessages.Extension {
MAP.put(ErrorsJvm.NATIVE_DECLARATION_IN_TRAIT, "Members of traits can not be native");
MAP.put(ErrorsJvm.NATIVE_DECLARATION_CANNOT_BE_INLINED, "Members of traits can not be inlined");
MAP.put(ErrorsJvm.POSITIONED_VALUE_ARGUMENT_FOR_JAVA_ANNOTATION, "Only named arguments are available for Java annotations");
MAP.put(ErrorsJvm.NO_REFLECTION_IN_CLASS_PATH, "Expression ''{0}'' uses reflection which is not found in compilation classpath. Make sure you have kotlin-reflect.jar in the classpath", Renderers.ELEMENT_TEXT);
MAP.put(ErrorsJvm.NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS,
@@ -51,6 +51,8 @@ public interface ErrorsJvm {
DiagnosticFactory0<JetDeclaration> NATIVE_DECLARATION_IN_TRAIT = DiagnosticFactory0.create(ERROR, DECLARATION_SIGNATURE);
DiagnosticFactory0<JetDeclaration> NATIVE_DECLARATION_CANNOT_BE_INLINED = DiagnosticFactory0.create(ERROR, DECLARATION_SIGNATURE);
DiagnosticFactory0<JetExpression> POSITIONED_VALUE_ARGUMENT_FOR_JAVA_ANNOTATION = DiagnosticFactory0.create(ERROR);
// TODO: make this a warning
DiagnosticFactory1<JetExpression, JetExpression> NO_REFLECTION_IN_CLASS_PATH = DiagnosticFactory1.create(ERROR);
@@ -0,0 +1,5 @@
annotation class Ann(val x: Int, val value: String, val y: Double)
Ann(value = "a", x = 1, y = 1.0) fun foo1() {}
Ann(2, "b", 2.0) fun foo2() {}
Ann(3, "c", y = 2.0) fun foo3() {}
@@ -0,0 +1,15 @@
package
Ann(value = "a": kotlin.String, x = IntegerValueType(1): IntegerValueType(1), y = 1.0.toDouble(): kotlin.Double) internal fun foo1(): kotlin.Unit
Ann(value = "b": kotlin.String, x = IntegerValueType(2): IntegerValueType(2), y = 2.0.toDouble(): kotlin.Double) internal fun foo2(): kotlin.Unit
Ann(value = "c": kotlin.String, x = IntegerValueType(3): IntegerValueType(3), y = 2.0.toDouble(): kotlin.Double) internal fun foo3(): kotlin.Unit
internal final annotation class Ann : kotlin.Annotation {
public constructor Ann(/*0*/ x: kotlin.Int, /*1*/ value: kotlin.String, /*2*/ y: kotlin.Double)
internal final val value: kotlin.String
internal final val x: kotlin.Int
internal final val y: kotlin.Double
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -0,0 +1,11 @@
// FILE: A.java
public @interface A {
int a();
double b();
boolean x();
}
// FILE: b.kt
A(<!CONSTANT_EXPECTED_TYPE_MISMATCH, POSITIONED_VALUE_ARGUMENT_FOR_JAVA_ANNOTATION!>false<!>,
<!POSITIONED_VALUE_ARGUMENT_FOR_JAVA_ANNOTATION!>1.0<!>,
<!POSITIONED_VALUE_ARGUMENT_FOR_JAVA_ANNOTATION!>false<!>, <!TOO_MANY_ARGUMENTS!>1<!>, <!TOO_MANY_ARGUMENTS!>2<!>) fun foo1() {}
@@ -0,0 +1,13 @@
package
A(a = false: kotlin.Boolean, b = 1.0.toDouble(): kotlin.Double, x = false: kotlin.Boolean) internal fun foo1(): kotlin.Unit
public final annotation class A : kotlin.Annotation {
public constructor A(/*0*/ a: kotlin.Int, /*1*/ b: kotlin.Double, /*2*/ x: kotlin.Boolean)
public abstract fun a(): kotlin.Int
public abstract fun b(): kotlin.Double
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
public abstract fun x(): kotlin.Boolean
}
@@ -0,0 +1,13 @@
// FILE: A.java
public @interface A {
int a();
double b();
boolean x();
}
// FILE: b.kt
A(<!CONSTANT_EXPECTED_TYPE_MISMATCH, POSITIONED_VALUE_ARGUMENT_FOR_JAVA_ANNOTATION!>false<!>,
<!POSITIONED_VALUE_ARGUMENT_FOR_JAVA_ANNOTATION!>1.0<!>,
<!POSITIONED_VALUE_ARGUMENT_FOR_JAVA_ANNOTATION!>false<!>) fun foo1() {}
A(<!CONSTANT_EXPECTED_TYPE_MISMATCH, POSITIONED_VALUE_ARGUMENT_FOR_JAVA_ANNOTATION!>2.0<!>, x = true, b = 2.0) fun foo2() {}
@@ -0,0 +1,14 @@
package
A(a = false: kotlin.Boolean, b = 1.0.toDouble(): kotlin.Double, x = false: kotlin.Boolean) internal fun foo1(): kotlin.Unit
A(a = 2.0.toDouble(): kotlin.Double, b = 2.0.toDouble(): kotlin.Double, x = true: kotlin.Boolean) internal fun foo2(): kotlin.Unit
public final annotation class A : kotlin.Annotation {
public constructor A(/*0*/ a: kotlin.Int, /*1*/ b: kotlin.Double, /*2*/ x: kotlin.Boolean)
public abstract fun a(): kotlin.Int
public abstract fun b(): kotlin.Double
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
public abstract fun x(): kotlin.Boolean
}
@@ -0,0 +1,17 @@
// FILE: A.java
public @interface A {
int a();
double b();
String value();
boolean x();
}
// FILE: b.kt
A("v1", <!POSITIONED_VALUE_ARGUMENT_FOR_JAVA_ANNOTATION!>1<!>,
<!POSITIONED_VALUE_ARGUMENT_FOR_JAVA_ANNOTATION!>1.0<!>,
<!POSITIONED_VALUE_ARGUMENT_FOR_JAVA_ANNOTATION!>false<!>) fun foo1() {}
A("v2", <!POSITIONED_VALUE_ARGUMENT_FOR_JAVA_ANNOTATION!>2<!>, x = true, b = 2.0) fun foo2() {}
A("v2", x = true, b = 3.0, a = 4) fun foo3() {}
A(value = "v2", x = true, b = 3.0, a = 4) fun foo4() {}
@@ -0,0 +1,17 @@
package
A(a = IntegerValueType(1): IntegerValueType(1), b = 1.0.toDouble(): kotlin.Double, value = "v1": kotlin.String, x = false: kotlin.Boolean) internal fun foo1(): kotlin.Unit
A(a = IntegerValueType(2): IntegerValueType(2), b = 2.0.toDouble(): kotlin.Double, value = "v2": kotlin.String, x = true: kotlin.Boolean) internal fun foo2(): kotlin.Unit
A(a = IntegerValueType(4): IntegerValueType(4), b = 3.0.toDouble(): kotlin.Double, value = "v2": kotlin.String, x = true: kotlin.Boolean) internal fun foo3(): kotlin.Unit
A(a = IntegerValueType(4): IntegerValueType(4), b = 3.0.toDouble(): kotlin.Double, value = "v2": kotlin.String, x = true: kotlin.Boolean) internal fun foo4(): kotlin.Unit
public final annotation class A : kotlin.Annotation {
public constructor A(/*0*/ value: kotlin.String, /*1*/ a: kotlin.Int, /*2*/ b: kotlin.Double, /*3*/ x: kotlin.Boolean)
public abstract fun a(): kotlin.Int
public abstract fun b(): kotlin.Double
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
public abstract fun value(): kotlin.String
public abstract fun x(): kotlin.Boolean
}
@@ -0,0 +1,17 @@
// FILE: A.java
public @interface A {
int a();
double b();
boolean x();
}
// FILE: b.kt
A(<!POSITIONED_VALUE_ARGUMENT_FOR_JAVA_ANNOTATION!>1<!>,
<!POSITIONED_VALUE_ARGUMENT_FOR_JAVA_ANNOTATION!>1.0<!>,
<!POSITIONED_VALUE_ARGUMENT_FOR_JAVA_ANNOTATION!>false<!>) fun foo1() {}
A(<!POSITIONED_VALUE_ARGUMENT_FOR_JAVA_ANNOTATION!>2<!>, x = true, b = 2.0) fun foo2() {}
A(x = true, b = 3.0, a = 4) fun foo3() {}
@@ -0,0 +1,15 @@
package
A(a = IntegerValueType(1): IntegerValueType(1), b = 1.0.toDouble(): kotlin.Double, x = false: kotlin.Boolean) internal fun foo1(): kotlin.Unit
A(a = IntegerValueType(2): IntegerValueType(2), b = 2.0.toDouble(): kotlin.Double, x = true: kotlin.Boolean) internal fun foo2(): kotlin.Unit
A(a = IntegerValueType(4): IntegerValueType(4), b = 3.0.toDouble(): kotlin.Double, x = true: kotlin.Boolean) internal fun foo3(): kotlin.Unit
public final annotation class A : kotlin.Annotation {
public constructor A(/*0*/ a: kotlin.Int, /*1*/ b: kotlin.Double, /*2*/ x: kotlin.Boolean)
public abstract fun a(): kotlin.Int
public abstract fun b(): kotlin.Double
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
public abstract fun x(): kotlin.Boolean
}
@@ -58,6 +58,7 @@ public class JetDiagnosticsTestWithStdLibGenerated extends AbstractJetDiagnostic
Annotations.AnnotationWithVarargParameter.class,
Annotations.JvmOverloads.class,
Annotations.PlatformStatic.class,
Annotations.ProhibitPositionedArgument.class,
})
@RunWith(JUnit3RunnerWithInners.class)
public static class Annotations extends AbstractJetDiagnosticsTestWithStdLib {
@@ -262,6 +263,45 @@ public class JetDiagnosticsTestWithStdLibGenerated extends AbstractJetDiagnostic
doTest(fileName);
}
}
@TestMetadata("compiler/testData/diagnostics/testsWithStdLib/annotations/prohibitPositionedArgument")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class ProhibitPositionedArgument extends AbstractJetDiagnosticsTestWithStdLib {
public void testAllFilesPresentInProhibitPositionedArgument() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/diagnostics/testsWithStdLib/annotations/prohibitPositionedArgument"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("kotlinAnnotation.kt")
public void testKotlinAnnotation() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithStdLib/annotations/prohibitPositionedArgument/kotlinAnnotation.kt");
doTest(fileName);
}
@TestMetadata("tooManyArgs.kt")
public void testTooManyArgs() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithStdLib/annotations/prohibitPositionedArgument/tooManyArgs.kt");
doTest(fileName);
}
@TestMetadata("typeMismatch.kt")
public void testTypeMismatch() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithStdLib/annotations/prohibitPositionedArgument/typeMismatch.kt");
doTest(fileName);
}
@TestMetadata("withValue.kt")
public void testWithValue() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithStdLib/annotations/prohibitPositionedArgument/withValue.kt");
doTest(fileName);
}
@TestMetadata("withoutValue.kt")
public void testWithoutValue() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithStdLib/annotations/prohibitPositionedArgument/withoutValue.kt");
doTest(fileName);
}
}
}
@TestMetadata("compiler/testData/diagnostics/testsWithStdLib/callableReference")