Refactor ReflectJavaConstructor.getValueParameters()

See https://youtrack.jetbrains.com/issue/KT-6886 for more information
This commit is contained in:
Alexander Udalov
2015-03-02 15:26:27 +03:00
parent 1054f004aa
commit e95d22dcfe
5 changed files with 69 additions and 13 deletions
@@ -0,0 +1,15 @@
package test;
public enum EnumConstructorParameter {
INSTANCE("instance");
public @interface Anno {
String value();
}
EnumConstructorParameter(@Anno("string") String s) {
}
EnumConstructorParameter(int x) {
}
}
@@ -0,0 +1,25 @@
package test
public final enum class EnumConstructorParameter : kotlin.Enum<test.EnumConstructorParameter!> {
public enum entry INSTANCE : test.EnumConstructorParameter {
private constructor INSTANCE()
public final override /*1*/ /*fake_override*/ fun compareTo(/*0*/ other: test.EnumConstructorParameter!): kotlin.Int
public final override /*1*/ /*fake_override*/ fun name(): kotlin.String
public final override /*1*/ /*fake_override*/ fun ordinal(): kotlin.Int
}
private constructor EnumConstructorParameter(/*0*/ p0: kotlin.Int)
private constructor EnumConstructorParameter(/*0*/ test.EnumConstructorParameter.Anno(value = "string": kotlin.String) p0: kotlin.String!)
public final override /*1*/ /*fake_override*/ fun compareTo(/*0*/ other: test.EnumConstructorParameter!): kotlin.Int
public final override /*1*/ /*fake_override*/ fun name(): kotlin.String
public final override /*1*/ /*fake_override*/ fun ordinal(): kotlin.Int
public final annotation class Anno : kotlin.Annotation {
public constructor Anno(/*0*/ value: kotlin.String)
public abstract fun value(): kotlin.String
}
// Static members
public final /*synthesized*/ fun valueOf(/*0*/ value: kotlin.String): test.EnumConstructorParameter
public final /*synthesized*/ fun values(): kotlin.Array<test.EnumConstructorParameter>
}
@@ -415,6 +415,12 @@ public class LoadJavaTestGenerated extends AbstractLoadJavaTest {
doTestCompiledJava(fileName);
}
@TestMetadata("EnumConstructorParameter.java")
public void testEnumConstructorParameter() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/loadJava/compiledJava/annotations/EnumConstructorParameter.java");
doTestCompiledJava(fileName);
}
@TestMetadata("EnumInParam.java")
public void testEnumInParam() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/loadJava/compiledJava/annotations/EnumInParam.java");
@@ -3457,6 +3457,12 @@ public class JvmRuntimeDescriptorLoaderTestGenerated extends AbstractJvmRuntimeD
doTest(fileName);
}
@TestMetadata("EnumConstructorParameter.java")
public void testEnumConstructorParameter() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/loadJava/compiledJava/annotations/EnumConstructorParameter.java");
doTest(fileName);
}
@TestMetadata("EnumInParam.java")
public void testEnumInParam() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/loadJava/compiledJava/annotations/EnumInParam.java");
@@ -21,24 +21,28 @@ import org.jetbrains.kotlin.load.java.structure.JavaTypeParameter
import org.jetbrains.kotlin.load.java.structure.JavaValueParameter
import java.lang.reflect.Constructor
import java.lang.reflect.Modifier
import java.util.Arrays
public class ReflectJavaConstructor(override val member: Constructor<*>) : ReflectJavaMember(), JavaConstructor {
override fun getValueParameters(): List<JavaValueParameter> =
getValueParameters(
dropSynthetic(member.getGenericParameterTypes()),
dropSynthetic(member.getParameterAnnotations()),
member.isVarArgs()
)
// TODO: test local/anonymous classes
override fun getValueParameters(): List<JavaValueParameter> {
val types = member.getGenericParameterTypes()
if (types.isEmpty()) return emptyList()
// Constructors of inner classes have one additional synthetic parameter
// TODO: test this code with annotations on constructor parameters of enums and inner classes
private inline fun <reified T> dropSynthetic(array: Array<T>): Array<T> {
val klass = member.getDeclaringClass()
return if (klass.getDeclaringClass() != null && !Modifier.isStatic(klass.getModifiers())) {
Arrays.copyOfRange(array, 1, array.size())
val realTypes = when {
klass.getDeclaringClass() != null && !Modifier.isStatic(klass.getModifiers()) -> types.copyOfRange(1, types.size())
else -> types
}
else array
val annotations = member.getParameterAnnotations()
val realAnnotations = when {
annotations.size() < realTypes.size() -> throw IllegalStateException("Illegal generic signature: $member")
annotations.size() > realTypes.size() -> annotations.copyOfRange(annotations.size() - realTypes.size(), annotations.size())
else -> annotations
}
return getValueParameters(realTypes, realAnnotations, member.isVarArgs())
}
override fun getTypeParameters(): List<JavaTypeParameter> {