Skip to content

Commit

Permalink
fix(noclasspath): fix the qualified name of partial qualified referen…
Browse files Browse the repository at this point in the history
…ce (inner class) (#1201)
  • Loading branch information
tdurieux authored and surli committed Mar 3, 2017
1 parent e7acf58 commit 6433c2c
Show file tree
Hide file tree
Showing 5 changed files with 223 additions and 7 deletions.
12 changes: 8 additions & 4 deletions src/main/java/spoon/support/compiler/jdt/ReferenceBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
import spoon.reflect.declaration.CtPackage;
import spoon.reflect.declaration.CtParameter;
import spoon.reflect.declaration.ModifierKind;
import spoon.reflect.factory.PackageFactory;
import spoon.reflect.reference.CtArrayTypeReference;
import spoon.reflect.reference.CtCatchVariableReference;
import spoon.reflect.reference.CtExecutableReference;
Expand Down Expand Up @@ -532,10 +533,11 @@ <T> CtTypeReference<T> getTypeReference(TypeReference ref) {
if (res == null) {
return this.jdtTreeBuilder.getFactory().Type().createReference(CharOperation.toString(ref.getParameterizedTypeName()));
}
CtPackageReference packageReference = index >= 0
? this.jdtTreeBuilder.getFactory().Package().getOrCreate(concatSubArray(namesParameterized, index)).getReference()
: this.jdtTreeBuilder.getFactory().Package().topLevel();
inner.setPackage(packageReference);
if (inner.getPackage() == null) {
PackageFactory packageFactory = this.jdtTreeBuilder.getFactory().Package();
CtPackageReference packageReference = index >= 0 ? packageFactory.getOrCreate(concatSubArray(namesParameterized, index)).getReference() : packageFactory.topLevel();
inner.setPackage(packageReference);
}
return res;
}

Expand Down Expand Up @@ -572,6 +574,8 @@ private <T> CtTypeReference<T> getTypeReference(String name) {
} else if (Character.isUpperCase(name.charAt(0))) {
main = this.jdtTreeBuilder.getFactory().Core().createTypeReference();
main.setSimpleName(name);
final CtReference declaring = this.getDeclaringReferenceFromImports(name.toCharArray());
setPackageOrDeclaringType(main, declaring);
} else if (name.startsWith("?")) {
return (CtTypeReference) this.jdtTreeBuilder.getFactory().Core().createWildcardReference();
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/spoon/test/arrays/ArraysTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void testInitializeWithNewArray() throws Exception {
assertEquals(1, local.getDimensionExpressions().size());
assertTrue(local.getDimensionExpressions().get(0) instanceof CtInvocation);
assertTrue(local.getType() instanceof CtArrayTypeReference);
assertEquals("new Type[list.size()]", local.toString());
assertEquals("new com.example.Type[list.size()]", local.toString());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,9 @@ public void testCtNewClassInNoClasspath() throws Exception {
assertNotNull(anonymousClass);
assertNotNull(anonymousClass.getSuperclass());
assertEquals("With", anonymousClass.getSuperclass().getSimpleName());
assertEquals("Lock$With", anonymousClass.getSuperclass().getQualifiedName());
assertEquals("org.apache.lucene.store.Lock$With", anonymousClass.getSuperclass().getQualifiedName());
assertEquals("Lock", anonymousClass.getSuperclass().getDeclaringType().getSimpleName());
assertEquals("Lock.With", anonymousClass.getSuperclass().toString());
assertEquals("org.apache.lucene.store.Lock.With", anonymousClass.getSuperclass().toString());
assertEquals("1", anonymousClass.getSimpleName());
assertEquals("2", secondNewClass.getAnonymousClass().getSimpleName());
assertEquals(1, anonymousClass.getMethods().size());
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/spoon/test/type/TypeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import spoon.reflect.code.CtFieldRead;
import spoon.reflect.code.CtLambda;
import spoon.reflect.code.CtLocalVariable;
import spoon.reflect.code.CtNewClass;
import spoon.reflect.code.CtTypeAccess;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtMethod;
Expand Down Expand Up @@ -156,6 +157,22 @@ public void testTypeAccessOfArrayObjectInFullyQualifiedName() throws Exception {
canBeBuilt(target, 8, true);
}

@Test
public void test() throws Exception {
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/resources/noclasspath/TorIntegration.java");
launcher.getEnvironment().setNoClasspath(true);
launcher.buildModel();

CtType<?> ctType = launcher.getFactory().Class().getAll().get(0);
List<CtNewClass> elements = ctType.getElements(new TypeFilter<>(CtNewClass.class));
assertEquals(4, elements.size());
for (int i = 0; i < elements.size(); i++) {
CtNewClass ctNewClass = elements.get(i);
assertEquals("android.content.DialogInterface$OnClickListener", ctNewClass.getAnonymousClass().getSuperclass().getQualifiedName());
}
}

@Test
public void testIntersectionTypeReferenceInGenericsAndCasts() throws Exception {
final String target = "./target/type";
Expand Down
195 changes: 195 additions & 0 deletions src/test/resources/noclasspath/TorIntegration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
package com.duckduckgo.mobile.android.util;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.webkit.WebView;
import com.duckduckgo.mobile.android.network.DDGNetworkConstants;
import info.guardianproject.onionkit.ui.OrbotHelper;
import info.guardianproject.onionkit.web.WebkitProxy;

/**
* This class implements methods for Tor integration, such as setting and resetting proxy.
*/
public class TorIntegration {

public static final int JELLY_BEAN_MR2 = 18;
private final Activity context;
private final OrbotHelper orbotHelper;
private Dialog dialogOrbotInstall = null;
private Dialog dialogOrbotStart = null;


public TorIntegration(Activity context){
this.context = context;
orbotHelper = new OrbotHelper(this.context);
}

public boolean prepareTorSettings(){
return prepareTorSettings(isTorSettingEnabled());
}

public boolean prepareTorSettings(boolean enableTor){
if(!isTorSupported()){
return false;
}
//DDGNetworkConstants.initializeMainClient(context.getApplication(), enableTor);
if(enableTor){
DDGNetworkConstants.initializeMainClient(context.getApplication(), enableTor);
enableOrbotProxy();
requestOrbotInstallAndStart();
}
else{
resetProxy();
}
return true;
}

private void resetProxy() {
try {
WebkitProxy.resetProxy("com.duckduckgo.mobile.android.DDGApplication", DDGNetworkConstants.getWebView().getContext().getApplicationContext());
} catch (Exception e) {

e.printStackTrace();
}
}

public void resetProxy(WebView webView) {
try {
WebkitProxy.resetProxy("com.duckduckgo.mobile.android.DDGApplication", webView.getContext().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
}

private void enableOrbotProxy() {
try {
WebkitProxy.setProxy("com.duckduckgo.mobile.android.DDGApplication", DDGNetworkConstants.getWebView().getContext().getApplicationContext(), DDGNetworkConstants.getWebView(), DDGNetworkConstants.PROXY_HOST, DDGNetworkConstants.PROXY_HTTP_PORT);
} catch (Exception e) {
// what should we do here? Discuss!
e.printStackTrace();
}
}

public void enableOrbotProxy(WebView webView) {
try {
WebkitProxy.setProxy("com.duckduckgo.mobile.android.DDGApplication", webView.getContext().getApplicationContext(), webView, DDGNetworkConstants.PROXY_HOST, DDGNetworkConstants.PROXY_HTTP_PORT);
} catch (Exception e) {
// what should we do here? Discuss!
e.printStackTrace();
}
}

private void requestOrbotInstallAndStart() {
if (!orbotHelper.isOrbotInstalled()){
promptToInstall();
}
else if (!orbotHelper.isOrbotRunning()){
requestOrbotStart();
}
}

/**
* if showing this will dismiss and release all the dialogs generated by TorIntegration
*/
public void dismissDialogs(){
dismissOrbotStartDialog();
dismissOrbotPromptDialog();
}
/**
* This method will dismiss prompt dialog if visible.
*/
public void dismissOrbotPromptDialog(){
if(dialogOrbotInstall != null && dialogOrbotInstall.isShowing()){
dialogOrbotInstall.dismiss();
dialogOrbotInstall = null;
}
}

/**
* This method is same as OrbotHelper.promptToInstall except dismisses the previous dialogs and stores the reference of new one.
*/
public void promptToInstall()
{
String uriMarket = context.getString(info.guardianproject.onionkit.R.string.market_orbot);
// show dialog - install from market, f-droid or direct APK
dialogOrbotInstall = showDownloadDialog(context.getString(info.guardianproject.onionkit.R.string.install_orbot_),
context.getString(info.guardianproject.onionkit.R.string.you_must_have_orbot),
context.getString(info.guardianproject.onionkit.R.string.yes), context.getString(info.guardianproject.onionkit.R.string.no), uriMarket);
}

/**
* This method is taken from OrbotHelper it uses same resource strings but returns the dialog instead.
* @return AlertDialog instance which can be used later to dismiss when activity is destroying
*/
private AlertDialog showDownloadDialog(CharSequence stringTitle, CharSequence stringMessage, CharSequence stringButtonYes,
CharSequence stringButtonNo, final String uriString) {
AlertDialog.Builder downloadDialog = new AlertDialog.Builder(context);
downloadDialog.setTitle(stringTitle);
downloadDialog.setMessage(stringMessage);
downloadDialog.setPositiveButton(stringButtonYes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Uri uri = Uri.parse(uriString);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
context.startActivity(intent);
}
});
downloadDialog.setNegativeButton(stringButtonNo, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
return downloadDialog.show();
}
/**
* This method will dismiss prompt dialog if visible.
*/
public void dismissOrbotStartDialog(){
if(dialogOrbotStart != null && dialogOrbotStart.isShowing()){
dialogOrbotStart.dismiss();
dialogOrbotStart = null;
}
}
public void requestOrbotStart()
{
dismissOrbotStartDialog();
AlertDialog.Builder downloadDialog = new AlertDialog.Builder(context);
downloadDialog.setTitle(info.guardianproject.onionkit.R.string.start_orbot_);
downloadDialog
.setMessage(info.guardianproject.onionkit.R.string.orbot_doesn_t_appear_to_be_running_would_you_like_to_start_it_up_and_connect_to_tor_);
downloadDialog.setPositiveButton(info.guardianproject.onionkit.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
context.startActivityForResult(orbotHelper.getOrbotStartIntent(), 1);
}
});
downloadDialog.setNegativeButton(info.guardianproject.onionkit.R.string.no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
dialogOrbotStart = downloadDialog.show();
}
public boolean isTorSettingEnabled() {
return PreferencesManager.getEnableTor();
}

public boolean isOrbotRunningAccordingToSettings() {
return !isTorSettingEnabled() || isTorEnabledAndOrbotRunning();
}

private boolean isTorEnabledAndOrbotRunning(){
return isTorSettingEnabled() &&
orbotHelper.isOrbotInstalled() &&
orbotHelper.isOrbotRunning();
}

public boolean isTorSupported() {
return true; //Build.VERSION.SDK_INT <= JELLY_BEAN_MR2;
}
}

0 comments on commit 6433c2c

Please sign in to comment.