Skip to content

Commit

Permalink
fix: fix autoimports for unknown methods in noclasspath (#3019)
Browse files Browse the repository at this point in the history
  • Loading branch information
Egor18 authored and monperrus committed Jun 14, 2019
1 parent 162a32b commit 341c242
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,7 @@ <T> CtExecutableReference<T> getExecutableReference(MessageSend messageSend) {
ref.setType(this.<T>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) {
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/spoon/test/imports/ImportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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<CtStatement> 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());
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 341c242

Please sign in to comment.