Removed 'INSTANCE$' field generation

This commit is contained in:
Michael Bogdanov
2015-12-24 10:40:30 +03:00
parent aeb6486473
commit 2e73559f33
19 changed files with 10 additions and 170 deletions
@@ -33,17 +33,12 @@ public class FieldInfo {
@NotNull
public static FieldInfo createForSingleton(@NotNull ClassDescriptor classDescriptor, @NotNull JetTypeMapper typeMapper) {
return createForSingleton(classDescriptor, typeMapper, false);
}
@NotNull
public static FieldInfo createForSingleton(@NotNull ClassDescriptor classDescriptor, @NotNull JetTypeMapper typeMapper, boolean oldSingleton) {
if (!classDescriptor.getKind().isSingleton() || DescriptorUtils.isEnumEntry(classDescriptor)) {
throw new UnsupportedOperationException("Can't create singleton field for class: " + classDescriptor);
}
if (isNonCompanionObject(classDescriptor) || COMPANION_OBJECT_MAPPING.hasMappingToObject(classDescriptor)) {
return createSingletonViaInstance(classDescriptor, typeMapper, oldSingleton);
return createSingletonViaInstance(classDescriptor, typeMapper);
}
else {
ClassDescriptor ownerDescriptor = DescriptorUtils.getParentOfType(classDescriptor, ClassDescriptor.class);
@@ -56,11 +51,10 @@ public class FieldInfo {
@NotNull
public static FieldInfo createSingletonViaInstance(
@NotNull ClassDescriptor classDescriptor,
@NotNull JetTypeMapper typeMapper,
boolean oldSingleton
@NotNull JetTypeMapper typeMapper
) {
Type type = typeMapper.mapType(classDescriptor);
return new FieldInfo(type, type, oldSingleton ? JvmAbi.DEPRECATED_INSTANCE_FIELD : JvmAbi.INSTANCE_FIELD, true);
return new FieldInfo(type, type, JvmAbi.INSTANCE_FIELD, true);
}
@NotNull
@@ -849,11 +849,6 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
ACC_PUBLIC | ACC_STATIC | ACC_FINAL | (isCompanionObject ? ACC_DEPRECATED : 0),
field.name, field.type.getDescriptor(), null, null);
if (isNonCompanionObject(descriptor)) {
StackValue.Field oldField = StackValue.oldSingleton(descriptor, typeMapper);
v.newField(JvmDeclarationOriginKt.OtherOrigin(myClass), ACC_PUBLIC | ACC_STATIC | ACC_FINAL | ACC_DEPRECATED, oldField.name, oldField.type.getDescriptor(), null, null);
}
if (state.getClassBuilderMode() != ClassBuilderMode.FULL) return;
// Invoke the object constructor but ignore the result because INSTANCE$ will be initialized in the first line of <init>
InstructionAdapter v = createOrGetClInitCodegen().v;
@@ -1009,9 +1004,6 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
if (isObject(descriptor)) {
StackValue.singletonViaInstance(descriptor, typeMapper).store(StackValue.LOCAL_0, iv);
if (isNonCompanionObject(descriptor)) {
StackValue.oldSingleton(descriptor, typeMapper).store(StackValue.LOCAL_0, iv);
}
}
for (KtSuperTypeListEntry specifier : myClass.getSuperTypeListEntries()) {
@@ -597,11 +597,7 @@ public abstract class StackValue {
}
public static Field singletonViaInstance(ClassDescriptor classDescriptor, JetTypeMapper typeMapper) {
return field(FieldInfo.createSingletonViaInstance(classDescriptor, typeMapper, false), none());
}
public static Field oldSingleton(ClassDescriptor classDescriptor, JetTypeMapper typeMapper) {
return field(FieldInfo.createForSingleton(classDescriptor, typeMapper, true), none());
return field(FieldInfo.createSingletonViaInstance(classDescriptor, typeMapper), none());
}
public static StackValue operation(Type type, Function1<InstructionAdapter, Unit> lambda) {
@@ -9,10 +9,6 @@ public final class A {
@org.jetbrains.annotations.NotNull
public static java.lang.String v;
public static final pack.A INSTANCE;
/**
* @deprecated
*/
public static final pack.A INSTANCE$;
public final int getC() { /* compiled code */ }
@@ -11,8 +11,8 @@ class B {
}
/*
Two DEPRECATED are for INSTANCE$ and INSTANCE temporarily
1 DEPRECATED is for INSTANCE temporarily
3 others are for getCONST_VAL
*/
// 5 DEPRECATED
// 4 DEPRECATED
+2 -2
View File
@@ -7,5 +7,5 @@ object A {
//check that constant initializers inlined
// 0 GETSTATIC
// 2 PUTSTATIC A.INSTANCE
// 2 PUTSTATIC
// 1 PUTSTATIC A.INSTANCE
// 1 PUTSTATIC
@@ -3,5 +3,4 @@ object A {
}
// Field initialized in constant pool
// A super constructor call and INSTANCE put
// One more for INSTANCE$ put (temporarily)
// 3 ALOAD 0
// 2 ALOAD 0
@@ -51,7 +51,6 @@ public final class JvmAbi {
public static final String ANNOTATED_PROPERTY_METHOD_NAME_SUFFIX = "$annotations";
public static final String INSTANCE_FIELD = "INSTANCE";
public static final String DEPRECATED_INSTANCE_FIELD = "INSTANCE$";
public static final String DEFAULT_MODULE_NAME = "main";
public static final ClassId REFLECTION_FACTORY_IMPL = ClassId.topLevel(new FqName("kotlin.reflect.jvm.internal.ReflectionFactoryImpl"));
-8
View File
@@ -1280,14 +1280,6 @@
level="WARNING"
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.DeprecatedObjectInstanceFieldReferenceInspection"
displayName="Usage of deprecated 'INSTANCE$' field"
groupName="Kotlin"
language="JAVA"
enabledByDefault="true"
cleanupTool="true"
level="WARNING"/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.DeprecatedUsageOfStaticFieldInspection"
displayName="This field will not be generated in future versions of Kotlin. Use 'const' modifier, '@JvmField' annotation or access data through corresponding object."
groupName="Kotlin"
@@ -1,65 +0,0 @@
/*
* 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.idea.inspections
import com.intellij.codeInspection.*
import com.intellij.openapi.project.Project
import com.intellij.psi.*
import com.intellij.psi.codeStyle.JavaCodeStyleManager
import org.jetbrains.kotlin.asJava.KtLightClass
import org.jetbrains.kotlin.load.java.JvmAbi
import org.jetbrains.kotlin.psi.KtObjectDeclaration
public class DeprecatedObjectInstanceFieldReferenceInspection : LocalInspectionTool(), CleanupLocalInspectionTool {
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean, session: LocalInspectionToolSession): PsiElementVisitor {
return object : JavaElementVisitor() {
override fun visitReferenceExpression(expression: PsiReferenceExpression) {
val resolvedTo = expression.reference?.resolve()
if (!(resolvedTo is PsiField && resolvedTo.name == JvmAbi.DEPRECATED_INSTANCE_FIELD)) return
val containingClass = resolvedTo.containingClass
if (containingClass !is KtLightClass || containingClass.getOrigin() !is KtObjectDeclaration) return
holder.registerProblem(
expression, "Use of deprecated '${JvmAbi.DEPRECATED_INSTANCE_FIELD}' field",
ProblemHighlightType.LIKE_DEPRECATED,
DeprecatedObjectInstanceFieldReferenceFix()
)
}
}
}
}
public class DeprecatedObjectInstanceFieldReferenceFix : LocalQuickFix {
override fun getName(): String = "Replace with reference to '${JvmAbi.INSTANCE_FIELD}' field"
override fun getFamilyName(): String = name
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val reference = descriptor.psiElement.reference ?: return
val deprecatedField = reference.resolve() as? PsiField ?: return
val lightClassForObject = deprecatedField.containingClass as? KtLightClass ?: return
val correctField = lightClassForObject.findFieldByName(JvmAbi.INSTANCE_FIELD, false) ?: return
val newReference = reference.bindToElement(correctField)
val codeStyleManager = JavaCodeStyleManager.getInstance(project)
codeStyleManager.shortenClassReferences(newReference)
val javaFile = newReference.containingFile as? PsiJavaFile ?: return
codeStyleManager.removeRedundantImports(javaFile)
}
}
@@ -1 +0,0 @@
org.jetbrains.kotlin.idea.inspections.DeprecatedObjectInstanceFieldReferenceInspection
@@ -1,8 +0,0 @@
// "Replace with reference to 'INSTANCE' field" "true"
import a.A;
class B {
void bar() {
A.INSTANCE.getName();
}
}
@@ -1,8 +0,0 @@
// "Replace with reference to 'INSTANCE' field" "true"
import a.A;
class B {
void bar() {
A.IN<caret>STANCE$.getName();
}
}
@@ -1,4 +0,0 @@
package a
object A {
}
@@ -1,8 +0,0 @@
// "Replace with reference to 'INSTANCE' field" "true"
import a.A;
class B {
void bar() {
A.INSTANCE.getName();
}
}
@@ -1,8 +0,0 @@
// "Replace with reference to 'INSTANCE' field" "true"
import static a.A.INSTANCE;
class B {
void bar() {
IN<caret>STANCE$.getName();
}
}
@@ -1,5 +0,0 @@
package a
object A {
val name: String = ""
}
+1 -1
View File
@@ -1,6 +1,6 @@
public class ObjectInstance {
public static void foo() {
k.KotlinObject.INSTANCE<caret>$.f();
k.KotlinObject.INSTANC<caret>E.f();
}
}
@@ -1234,27 +1234,6 @@ public class QuickFixMultiFileTestGenerated extends AbstractQuickFixMultiFileTes
}
}
@TestMetadata("idea/testData/quickfix/migration/deprecatedObjectInstanceField")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class DeprecatedObjectInstanceField extends AbstractQuickFixMultiFileTest {
public void testAllFilesPresentInDeprecatedObjectInstanceField() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/migration/deprecatedObjectInstanceField"), Pattern.compile("^(\\w+)\\.((before\\.Main\\.\\w+)|(test))$"), true);
}
@TestMetadata("basic.before.Main.java")
public void testBasic() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/migration/deprecatedObjectInstanceField/basic.before.Main.java");
doTestWithExtraFile(fileName);
}
@TestMetadata("staticImport.before.Main.java")
public void testStaticImport() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/migration/deprecatedObjectInstanceField/staticImport.before.Main.java");
doTestWithExtraFile(fileName);
}
}
@TestMetadata("idea/testData/quickfix/migration/deprecatedStaticField")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)