Type Enhancement for Java fields and constructors supported as well
This commit is contained in:
committed by
Denis Zharkov
parent
8c78739983
commit
694af022c8
+4
-2
@@ -21,7 +21,9 @@ import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.ReceiverParameterDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.impl.ValueParameterDescriptorImpl
|
||||
import org.jetbrains.kotlin.load.java.descriptors.JavaMethodDescriptor
|
||||
import org.jetbrains.kotlin.load.java.descriptors.JavaCallableMemberDescriptor
|
||||
import org.jetbrains.kotlin.load.java.descriptors.JavaConstructorDescriptor
|
||||
import org.jetbrains.kotlin.load.java.descriptors.JavaPropertyDescriptor
|
||||
import org.jetbrains.kotlin.types.JetType
|
||||
|
||||
fun <D : CallableMemberDescriptor> enhanceSignatures(platformSignatures: Collection<D>): Collection<D> {
|
||||
@@ -46,7 +48,7 @@ fun <D : CallableMemberDescriptor> D.enhance(): D {
|
||||
|
||||
val enhancedReturnType = parts { it.getReturnType()!!.toReturnTypePart() }.enhance()
|
||||
|
||||
if (this is JavaMethodDescriptor) {
|
||||
if (this is JavaCallableMemberDescriptor) {
|
||||
@suppress("UNCHECKED_CAST")
|
||||
return this.enhance(enhancedReceiverType, enhancedValueParameters, enhancedReturnType) as D
|
||||
}
|
||||
|
||||
@@ -2,5 +2,5 @@ package test
|
||||
|
||||
public open class NotNullField {
|
||||
public constructor NotNullField()
|
||||
org.jetbrains.annotations.NotNull() public final var hi: kotlin.String!
|
||||
org.jetbrains.annotations.NotNull() public final var hi: kotlin.String
|
||||
}
|
||||
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package test;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.lang.String;
|
||||
|
||||
public class ConstructorWithAnnotations {
|
||||
public ConstructorWithAnnotations(Runnable r, @NotNull String s) {
|
||||
}
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
package test
|
||||
|
||||
public open class ConstructorWithAnnotations {
|
||||
public /*synthesized*/ constructor ConstructorWithAnnotations(/*0*/ p0: (() -> kotlin.Unit)!, /*1*/ org.jetbrains.annotations.NotNull() p1: kotlin.String)
|
||||
public constructor ConstructorWithAnnotations(/*0*/ p0: java.lang.Runnable!, /*1*/ org.jetbrains.annotations.NotNull() p1: kotlin.String)
|
||||
}
|
||||
@@ -1568,6 +1568,12 @@ public class LoadJavaTestGenerated extends AbstractLoadJavaTest {
|
||||
doTestCompiledJava(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("ConstructorWithAnnotations.java")
|
||||
public void testConstructorWithAnnotations() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("compiler/testData/loadJava/compiledJava/sam/adapters/ConstructorWithAnnotations.java");
|
||||
doTestCompiledJava(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("DeepSamLoop.java")
|
||||
public void testDeepSamLoop() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("compiler/testData/loadJava/compiledJava/sam/adapters/DeepSamLoop.java");
|
||||
|
||||
+12
@@ -16,7 +16,19 @@
|
||||
|
||||
package org.jetbrains.kotlin.load.java.descriptors;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor;
|
||||
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor;
|
||||
import org.jetbrains.kotlin.types.JetType;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface JavaCallableMemberDescriptor extends CallableMemberDescriptor {
|
||||
@NotNull
|
||||
JavaCallableMemberDescriptor enhance(
|
||||
@Nullable JetType enhancedReceiverType,
|
||||
@NotNull List<ValueParameterDescriptor> enhancedValueParameters,
|
||||
@NotNull JetType enhancedReturnType
|
||||
);
|
||||
}
|
||||
|
||||
+26
-4
@@ -18,12 +18,12 @@ package org.jetbrains.kotlin.load.java.descriptors;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor;
|
||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor;
|
||||
import org.jetbrains.kotlin.descriptors.FunctionDescriptor;
|
||||
import org.jetbrains.kotlin.descriptors.SourceElement;
|
||||
import org.jetbrains.kotlin.descriptors.*;
|
||||
import org.jetbrains.kotlin.descriptors.annotations.Annotations;
|
||||
import org.jetbrains.kotlin.descriptors.impl.ConstructorDescriptorImpl;
|
||||
import org.jetbrains.kotlin.types.JetType;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class JavaConstructorDescriptor extends ConstructorDescriptorImpl implements JavaCallableMemberDescriptor {
|
||||
private Boolean hasStableParameterNames = null;
|
||||
@@ -92,4 +92,26 @@ public class JavaConstructorDescriptor extends ConstructorDescriptorImpl impleme
|
||||
result.setHasSynthesizedParameterNames(hasSynthesizedParameterNames());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public JavaConstructorDescriptor enhance(
|
||||
@Nullable JetType enhancedReceiverType,
|
||||
@NotNull List<ValueParameterDescriptor> enhancedValueParameters,
|
||||
@NotNull JetType enhancedReturnType
|
||||
) {
|
||||
JavaConstructorDescriptor enhanced = createSubstitutedCopy(getContainingDeclaration(), getOriginal(), getKind());
|
||||
enhanced.initialize(
|
||||
enhancedReceiverType,
|
||||
getDispatchReceiverParameter(),
|
||||
getTypeParameters(),
|
||||
enhancedValueParameters,
|
||||
enhancedReturnType,
|
||||
getModality(),
|
||||
getVisibility()
|
||||
);
|
||||
|
||||
return enhanced;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+3
@@ -91,6 +91,9 @@ public class JavaMethodDescriptor extends SimpleFunctionDescriptorImpl implement
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public JavaMethodDescriptor enhance(
|
||||
@Nullable JetType enhancedReceiverType,
|
||||
|
||||
+36
-4
@@ -17,13 +17,14 @@
|
||||
package org.jetbrains.kotlin.load.java.descriptors;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor;
|
||||
import org.jetbrains.kotlin.descriptors.Modality;
|
||||
import org.jetbrains.kotlin.descriptors.SourceElement;
|
||||
import org.jetbrains.kotlin.descriptors.Visibility;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.kotlin.descriptors.*;
|
||||
import org.jetbrains.kotlin.descriptors.annotations.Annotations;
|
||||
import org.jetbrains.kotlin.descriptors.impl.PropertyDescriptorImpl;
|
||||
import org.jetbrains.kotlin.name.Name;
|
||||
import org.jetbrains.kotlin.types.JetType;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class JavaPropertyDescriptor extends PropertyDescriptorImpl implements JavaCallableMemberDescriptor {
|
||||
public JavaPropertyDescriptor(
|
||||
@@ -41,4 +42,35 @@ public class JavaPropertyDescriptor extends PropertyDescriptorImpl implements Ja
|
||||
public boolean hasSynthesizedParameterNames() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public JavaCallableMemberDescriptor enhance(
|
||||
@Nullable JetType enhancedReceiverType,
|
||||
@NotNull List<ValueParameterDescriptor> enhancedValueParameters,
|
||||
@NotNull JetType enhancedReturnType
|
||||
) {
|
||||
JavaPropertyDescriptor enhanced = new JavaPropertyDescriptor(
|
||||
getContainingDeclaration(),
|
||||
getAnnotations(),
|
||||
getVisibility(),
|
||||
isVar(),
|
||||
getName(),
|
||||
getSource()
|
||||
);
|
||||
assert getGetter() == null : "Field must not have a getter: " + this;
|
||||
assert getSetter() == null : "Field must not have a setter: " + this;
|
||||
enhanced.initialize(null, null);
|
||||
enhanced.setSetterProjectedOut(isSetterProjectedOut());
|
||||
if (compileTimeInitializer != null) {
|
||||
enhanced.setCompileTimeInitializer(compileTimeInitializer);
|
||||
}
|
||||
enhanced.setType(
|
||||
enhancedReturnType,
|
||||
getTypeParameters(), // TODO
|
||||
getDispatchReceiverParameter(),
|
||||
enhancedReceiverType
|
||||
);
|
||||
return enhanced;
|
||||
}
|
||||
}
|
||||
|
||||
+4
-1
@@ -71,7 +71,10 @@ public class LazyJavaClassMemberScope(
|
||||
result.add(descriptor)
|
||||
result.addIfNotNull(c.samConversionResolver.resolveSamAdapter(descriptor))
|
||||
}
|
||||
result ifEmpty { emptyOrSingletonList(createDefaultConstructor()) }
|
||||
|
||||
c.externalSignatureResolver.enhanceSignatures(
|
||||
result ifEmpty { emptyOrSingletonList(createDefaultConstructor()) }
|
||||
).toReadOnlyList()
|
||||
}
|
||||
|
||||
override fun computeNonDeclaredFunctions(result: MutableCollection<SimpleFunctionDescriptor>, name: Name) {
|
||||
|
||||
+4
-1
@@ -236,7 +236,10 @@ public abstract class LazyJavaMemberScope(
|
||||
|
||||
computeNonDeclaredProperties(name, properties)
|
||||
|
||||
properties.toReadOnlyList()
|
||||
if (DescriptorUtils.isAnnotationClass(containingDeclaration))
|
||||
properties.toReadOnlyList()
|
||||
else
|
||||
c.externalSignatureResolver.enhanceSignatures(properties).toReadOnlyList()
|
||||
}
|
||||
|
||||
private fun resolveProperty(field: JavaField): PropertyDescriptor {
|
||||
|
||||
+1
-1
@@ -32,7 +32,7 @@ import java.util.Set;
|
||||
|
||||
public abstract class VariableDescriptorImpl extends DeclarationDescriptorNonRootImpl implements VariableDescriptor {
|
||||
private JetType outType;
|
||||
private NullableLazyValue<CompileTimeConstant<?>> compileTimeInitializer;
|
||||
protected NullableLazyValue<CompileTimeConstant<?>> compileTimeInitializer;
|
||||
|
||||
public VariableDescriptorImpl(
|
||||
@NotNull DeclarationDescriptor containingDeclaration,
|
||||
|
||||
Reference in New Issue
Block a user