Skip to content

Commit

Permalink
fix: in noClasspath, a type without import should be considered in th…
Browse files Browse the repository at this point in the history
…e same package as the current class (#1302)
  • Loading branch information
surli authored and monperrus committed May 16, 2017
1 parent de8682d commit 71554f9
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -913,7 +913,8 @@ void setPackageOrDeclaringType(CtTypeReference<?> ref, CtReference declaring) {
} else if (declaring instanceof CtTypeReference) {
ref.setDeclaringType((CtTypeReference) declaring);
} else if (declaring == null) {
ref.setPackage(jdtTreeBuilder.getFactory().Package().topLevel());
// in that case we consider the package should be the same as the current one. Fix #1293
ref.setPackage(jdtTreeBuilder.getContextBuilder().compilationUnitSpoon.getDeclaredPackage().getReference());
} else {
throw new AssertionError("unexpected declaring type: " + declaring.getClass() + " of " + declaring);
}
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/spoon/test/ctClass/CtClassTest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package spoon.test.ctClass;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static spoon.testing.utils.ModelUtils.build;
import static spoon.testing.utils.ModelUtils.buildClass;
import static spoon.testing.utils.ModelUtils.canBeBuilt;
Expand All @@ -16,6 +18,7 @@
import spoon.reflect.code.CtConstructorCall;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtConstructor;
import spoon.reflect.declaration.CtField;
import spoon.reflect.declaration.CtType;
import spoon.reflect.declaration.ModifierKind;
import spoon.reflect.factory.Factory;
Expand Down Expand Up @@ -133,6 +136,23 @@ public void testAllTypeReferencesToALocalTypeShouldNotStartWithNumber() throws E

factory.getEnvironment().setAutoImports(false);
factory.Code().createCodeSnippetStatement(aPozole.toString()).compile();
}

@Test
public void testSpoonShouldInferImplicitPackageInNoClasspath() throws Exception {
// contract: in noClasspath, when a type is used and no import is specified, then Spoon
// should infer that this type is in the same package as the current class.
final Launcher launcher2 = new Launcher();
launcher2.addInputResource("./src/test/resources/noclasspath/issue1293/com/cristal/ircica/applicationcolis/userinterface/fragments/TransporteurFragment.java");
launcher2.getEnvironment().setNoClasspath(true);
launcher2.buildModel();

final CtClass<Object> aClass2 = launcher2.getFactory().Class().get("com.cristal.ircica.applicationcolis.userinterface.fragments.TransporteurFragment");
final String type2 = aClass2.getSuperclass().getQualifiedName();

CtField field = aClass2.getField("transporteurRadioGroup");
assertThat(field.getType().getQualifiedName(), is("android.widget.RadioGroup"));

assertThat(type2, is("com.cristal.ircica.applicationcolis.userinterface.fragments.CompletableFragment"));
}
}
2 changes: 1 addition & 1 deletion src/test/java/spoon/test/filters/CUFilterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,6 @@ public void testSingleExcludeWithoutFilter() {
.getMethod("createB").getBody().getStatement(0);
final CtConstructorCall ctConstructorCall =
(CtConstructorCall)ctReturn.getReturnedExpression();
assertEquals("B", ctConstructorCall.getType().getQualifiedName());
assertEquals("spoon.test.same.B", ctConstructorCall.getType().getQualifiedName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.cristal.ircica.applicationcolis.userinterface.fragments;


import android.support.v4.app.Fragment;

/**Define a fragment able to say if all its fields are completed
* Created by Itinerance on 14/06/2016.
*/
public abstract class CompletableFragment extends Fragment {
public abstract boolean isComplete();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.cristal.ircica.applicationcolis.userinterface.fragments;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.cristal.ircica.applicationcolis.R;

/**
* Created by Itinerance on 03/06/2016.
*/

public class TransporteurFragment extends CompletableFragment{
RadioGroup transporteurRadioGroup;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.transporteur_fragment, container, false);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
transporteurRadioGroup = (RadioGroup)getView().findViewById(R.id.transporteur_radio_group);
transporteurRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId == R.id.other_radio_button){
changeUndisplayedTransporteurEditTextVisibility(View.VISIBLE);
}
else {
changeUndisplayedTransporteurEditTextVisibility(View.GONE);
}
}
});
((EditText)getView().findViewById(R.id.undisplayed_transporteur_edit_text)).setVisibility(View.GONE);
((TextView)getView().findViewById(R.id.undisplayed_transporteur_text_view)).setVisibility(View.GONE);

}

/**
*
* @return true if all the fields are completed , false otherwise
*/
public boolean isComplete() {
if(!((EditText)(getView().findViewById(R.id.no_order))).getText().toString().equals("")
&& !((EditText)getView().findViewById(R.id.no_delivery)).getText().toString().equals("")
&& ((RadioGroup)getView().findViewById(R.id.transporteur_radio_group)).getCheckedRadioButtonId()!=-1
&& !((EditText)getView().findViewById(R.id.nb_parcels)).getText().toString().equals("")){
return true;
}
else
return false;
}

public void resetFields() {
((RadioGroup)getView().findViewById(R.id.transporteur_radio_group)).check(-1);
((EditText)(getView().findViewById(R.id.no_order))).setText("");
((EditText)(getView().findViewById(R.id.no_delivery))).setText("");
((EditText)(getView().findViewById(R.id.nb_parcels))).setText("");
}

public void changeUndisplayedTransporteurEditTextVisibility(int visibility){
((EditText)getView().findViewById(R.id.undisplayed_transporteur_edit_text)).setVisibility(visibility);
((TextView)getView().findViewById(R.id.undisplayed_transporteur_text_view)).setVisibility(visibility);
}
}

0 comments on commit 71554f9

Please sign in to comment.