Skip to content

Commit

Permalink
fix:avoid crash in hasImplicitAncestor (#3247)
Browse files Browse the repository at this point in the history
  • Loading branch information
monperrus authored Feb 17, 2020
1 parent 0e5dfe4 commit 098cb9b
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,6 @@ private static void handleImplicit(PackageBinding packageBinding, char[][] token
* (e.g. spoon.test.imports.testclasses.internal.ChildClass.InnerClassProtected)
* In such rare case we cannot detect and set implicitness
*/
typeRef.getFactory().getEnvironment().debugMessage("Compiler's type path: " + qualifiedNameReference + " doesn't matches with Spoon qualified type name: " + originTypeRef);
return;
//throw new SpoonException("Unexpected type reference simple name: \"" + token + "\" expected: \"" + typeRef.getSimpleName() + "\"");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ private void pushContext(SourceFragmentPrinter listContext) {


private static boolean hasImplicitAncestor(CtElement el) {
if (el == null) {
if (el == null || !el.isParentInitialized()) {
return false;
}
if (el == el.getFactory().getModel().getRootPackage()) {
Expand Down
1 change: 1 addition & 0 deletions src/test/java/spoon/processing/ProcessingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.junit.Test;
import spoon.Launcher;
import spoon.compiler.Environment;
import spoon.reflect.code.CtBlock;
import spoon.reflect.declaration.CtType;
import spoon.support.sniper.SniperJavaPrettyPrinter;
import spoon.test.processing.processors.MyProcessor;
Expand Down
22 changes: 22 additions & 0 deletions src/test/java/spoon/test/parent/ParentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import org.junit.Ignore;
import org.junit.Test;
import spoon.Launcher;
import spoon.compiler.Environment;
import spoon.compiler.SpoonResourceHelper;
import spoon.support.sniper.SniperJavaPrettyPrinter;
import spoon.test.intercession.IntercessionScanner;
import spoon.reflect.code.BinaryOperatorKind;
import spoon.reflect.code.CtAssignment;
Expand Down Expand Up @@ -57,6 +59,9 @@
import spoon.support.UnsettableProperty;
import spoon.test.replace.testclasses.Tacos;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.List;
Expand Down Expand Up @@ -410,4 +415,21 @@ public boolean matches(CtInvocation<?> element) {
}.scan(launcher.getModel().getRootPackage());
}


@Test
public void testParentNotInitializedException() throws IOException {
// contract: does not crash on hasImplicitParent
final Launcher l = new Launcher();
Environment e = l.getEnvironment();

e.setNoClasspath(true);
e.setAutoImports(true);
e.setPrettyPrinterCreator(() -> new SniperJavaPrettyPrinter(l.getEnvironment()));

Path path = Files.createTempDirectory("emptydir");
l.addInputResource("src/test/resources/compilation4/A.java");
l.setSourceOutputDirectory(path.toFile());
l.run();
}

}
69 changes: 69 additions & 0 deletions src/test/resources/compilation4/A.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package me.ccrama.redditslide;
/**
* Created by ccrama on 9/19/2015.
*/
public class A extends android.os.AsyncTask<net.dean.jraw.models.PublicContribution, java.lang.Void, java.lang.Void> {
private final net.dean.jraw.models.VoteDirection direction;

private android.view.View v;

private android.content.Context c;

public A(java.lang.Boolean b, android.view.View v, android.content.Context c) {
direction = (b) ? net.dean.jraw.models.VoteDirection.UPVOTE : net.dean.jraw.models.VoteDirection.DOWNVOTE;
this.v = v;
this.c = c;
me.ccrama.redditslide.Reddit.setDefaultErrorHandler(c);
}

public A(android.view.View v, android.content.Context c) {
direction = net.dean.jraw.models.VoteDirection.NO_VOTE;
this.v = v;
this.c = c;
}

@java.lang.Override
protected java.lang.Void doInBackground(net.dean.jraw.models.PublicContribution... sub) {
if (me.ccrama.redditslide.Authentication.isLoggedIn) {
try {
new net.dean.jraw.managers.AccountManager(me.ccrama.redditslide.Authentication.reddit).vote(sub[0], direction);
} catch (net.dean.jraw.ApiException | java.lang.RuntimeException e) {
((android.app.Activity) (c)).runOnUiThread(new java.lang.Runnable() {
public void run() {
try {
if (((v != null) && (c != null)) && (v.getContext() != null)) {
android.support.design.widget.Snackbar s = android.support.design.widget.Snackbar.make(v, me.ccrama.redditslide.R.string.vote_err, android.support.design.widget.Snackbar.LENGTH_SHORT);
android.view.View view = s.getView();
android.widget.TextView tv = ((android.widget.TextView) (view.findViewById(android.support.design.R.id.snackbar_text)));
tv.setTextColor(android.graphics.Color.WHITE);
s.show();
}
} catch (java.lang.Exception ignored) {
}
c = null;
v = null;
}
});
e.printStackTrace();
}
} else {
((android.app.Activity) (c)).runOnUiThread(new java.lang.Runnable() {
public void run() {
try {
if (((v != null) && (c != null)) && (v.getContext() != null)) {
android.support.design.widget.Snackbar s = android.support.design.widget.Snackbar.make(v, me.ccrama.redditslide.R.string.vote_err_login, android.support.design.widget.Snackbar.LENGTH_SHORT);
android.view.View view = s.getView();
android.widget.TextView tv = ((android.widget.TextView) (view.findViewById(android.support.design.R.id.snackbar_text)));
tv.setTextColor(android.graphics.Color.WHITE);
s.show();
}
} catch (java.lang.Exception ignored) {
}
c = null;
v = null;
}
});
}
return null;
}
}

0 comments on commit 098cb9b

Please sign in to comment.