Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible fix for auto imports (issue #2996) #3019

Merged
merged 1 commit into from
Jun 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}