diff --git a/src/main/java/spoon/support/compiler/jdt/ReferenceBuilder.java b/src/main/java/spoon/support/compiler/jdt/ReferenceBuilder.java index bd0a6037867..bff1df4cccb 100644 --- a/src/main/java/spoon/support/compiler/jdt/ReferenceBuilder.java +++ b/src/main/java/spoon/support/compiler/jdt/ReferenceBuilder.java @@ -454,6 +454,7 @@ CtExecutableReference getExecutableReference(MessageSend messageSend) { ref.setType(this.getTypeReference(messageSend.expectedType(), true)); if (messageSend.receiver.resolvedType == null) { // It is crisis dude! static context, we don't have much more information. + ref.setStatic(true); if (messageSend.receiver instanceof SingleNameReference) { ref.setDeclaringType(jdtTreeBuilder.getHelper().createTypeAccessNoClasspath((SingleNameReference) messageSend.receiver).getAccessedType()); } else if (messageSend.receiver instanceof QualifiedNameReference) { diff --git a/src/test/java/spoon/test/imports/ImportTest.java b/src/test/java/spoon/test/imports/ImportTest.java index 84c5882c7b4..0f2294578e5 100644 --- a/src/test/java/spoon/test/imports/ImportTest.java +++ b/src/test/java/spoon/test/imports/ImportTest.java @@ -32,6 +32,7 @@ import spoon.reflect.code.CtTypeAccess; import spoon.reflect.cu.CompilationUnit; import spoon.reflect.declaration.CtClass; +import spoon.reflect.declaration.CtConstructor; import spoon.reflect.declaration.CtElement; import spoon.reflect.declaration.CtField; import spoon.reflect.declaration.CtImport; @@ -1534,6 +1535,7 @@ public void testBug2369_autoimports() { " }" + nl + "}", launcher.getFactory().Type().get("spoon.test.imports.testclasses.JavaLongUse").toString()); } + @Test public void testImportReferenceIsFullyQualifiedAndNoGeneric() { //contract: the reference of CtImport is always fully qualified and contains no actual type arguments @@ -1556,4 +1558,26 @@ public void testImportReferenceIsFullyQualifiedAndNoGeneric() { assertTrue(typeRef.isImplicit()); assertTrue(typeRef.getPackage().isImplicit()); } + + @Test + public void testMethodChainAutoImports() { + // contract: A chain of unknown methods in noclasspath mode with enabled auto-imports should not corrupt argument + // https://github.com/INRIA/spoon/issues/2996 + Launcher launcher = new Launcher(); + launcher.getEnvironment().setNoClasspath(true); + launcher.getEnvironment().setShouldCompile(false); + launcher.getEnvironment().setAutoImports(true); + launcher.addInputResource("./src/test/resources/import-resources/fr/inria/PageButtonNoClassPath.java"); + launcher.buildModel(); + + CtClass clazz = (CtClass) launcher.getFactory().Type().get("fr.inria.PageButtonNoClassPath"); + CtConstructor ctor = clazz.getConstructors().stream().findFirst().get(); + List statements = ctor.getBody().getStatements(); + + assertEquals("super(context, attributeSet)", statements.get(0).toString()); + assertEquals("mButton = ((Button) (findViewById(R.id.page_button_button)))", statements.get(1).toString()); + assertEquals("mCurrentActiveColor = getColor(R.color.c4_active_button_color)", statements.get(2).toString()); + assertEquals("mCurrentActiveColor = getResources().getColor(R.color.c4_active_button_color)", statements.get(3).toString()); + assertEquals("mCurrentActiveColor = getData().getResources().getColor(R.color.c4_active_button_color)", statements.get(4).toString()); + } } diff --git a/src/test/resources/import-resources/fr/inria/PageButtonNoClassPath.java b/src/test/resources/import-resources/fr/inria/PageButtonNoClassPath.java new file mode 100644 index 00000000000..74b4f726095 --- /dev/null +++ b/src/test/resources/import-resources/fr/inria/PageButtonNoClassPath.java @@ -0,0 +1,25 @@ +package fr.inria; + +import android.content.Context; +import android.content.res.ColorStateList; +import android.util.AttributeSet; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.Button; +import android.widget.FrameLayout; +import android.widget.ImageView; + +import com.carconnectivity.mlmediaplayer.R; + +public class PageButtonNoClassPath extends FrameLayout { + private final Button mButton; + private int mCurrentActiveColor; + + public PageButtonNoClassPath(Context context, AttributeSet attributeSet) { + super(context, attributeSet); + mButton = (Button) findViewById(R.id.page_button_button); + mCurrentActiveColor = getColor(R.color.c4_active_button_color); + mCurrentActiveColor = getResources().getColor(R.color.c4_active_button_color); + mCurrentActiveColor = getData().getResources().getColor(R.color.c4_active_button_color); + } +}