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

fix: more resilient wrt to errors during shadow class building #3655

Merged
merged 6 commits into from
Oct 26, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
5 changes: 3 additions & 2 deletions src/main/java/spoon/reflect/factory/TypeFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
package spoon.reflect.factory;

import spoon.Launcher;
import spoon.SpoonException;
import spoon.reflect.code.CtNewClass;
import spoon.reflect.cu.SourcePosition;
Expand All @@ -33,7 +34,6 @@
import spoon.reflect.visitor.CtScanner;
import spoon.reflect.visitor.filter.TypeFilter;
import spoon.support.DefaultCoreFactory;
import spoon.support.SpoonClassNotFoundException;
import spoon.support.StandardEnvironment;
import spoon.support.util.internal.MapUtils;
import spoon.support.visitor.ClassTypingContext;
Expand Down Expand Up @@ -563,7 +563,8 @@ public <T> CtType<T> get(Class<?> cl) {
try {
newShadowClass = new JavaReflectionTreeBuilder(getShadowFactory()).scan((Class<T>) cl);
} catch (Throwable e) {
throw new SpoonClassNotFoundException("cannot create shadow class: " + cl.getName(), e);
Launcher.LOGGER.warn("cannot create shadow class: {}", cl.getName(), e);
return null;
}
newShadowClass.setFactory(factory);
newShadowClass.accept(new CtScanner() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static spoon.testing.utils.ModelUtils.createFactory;

public class JavaReflectionTreeBuilderTest {
Expand Down Expand Up @@ -682,4 +683,14 @@ public boolean matches(CtType element) {
assertEquals(true, ctClass.isShadow());
assertEquals("foo", ctClass.getMethods().toArray(new CtMethod[0])[0].getSimpleName());
}

@Test
public void testExpectedExceptionInInitializerError() {
try {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you add a comment "contract: ..." in natural language where you describe the intention of the test? Thanks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

new JavaReflectionTreeBuilder(createFactory()).scan(spoon.support.visitor.java.testclasses.NPEInStaticInit.class);
fail();
}
catch (ExceptionInInitializerError expected) {
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package spoon.support.visitor.java.testclasses;

public class NPEInStaticInit {

private static class InnerClass {
public void doSmt(){ }
}

public static int VALUE = 1; // because of this field

static {
System.out.println("This text will be printed"); // NOTE
}

public static InnerClass someObject = null;

static {
someObject.doSmt(); // NPE !!!
}
}