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 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
9 changes: 7 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,12 @@ 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);

newShadowClass = getShadowFactory().Core().createClass();
newShadowClass.setSimpleName(cl.getSimpleName());
newShadowClass.setShadow(true);
getShadowFactory().Package().getOrCreate(cl.getPackage().getName()).addType(newShadowClass);
}
newShadowClass.setFactory(factory);
newShadowClass.accept(new CtScanner() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ public void visitField(Field field) {
CtLiteral<Object> defaultExpression = factory.createLiteral(field.get(null));
ctField.setDefaultExpression(defaultExpression);
}
} catch (IllegalAccessException e) {
monperrus marked this conversation as resolved.
Show resolved Hide resolved
} catch (IllegalAccessException | ExceptionInInitializerError | UnsatisfiedLinkError e) {
// ignore
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -682,4 +682,31 @@ public boolean matches(CtType element) {
assertEquals(true, ctClass.isShadow());
assertEquals("foo", ctClass.getMethods().toArray(new CtMethod[0])[0].getSimpleName());
}

@Test
public void testCannotGetDefaultExpressionBecauseOfException() {
/*
* contract:
* JavaReflectionTreeBuilder can't set defaultExpression for the field (public static primitive),
* as Reflection API throws the exception ExceptionInInitializerError when attempting to get it.
* {@link JavaReflectionTreeBuilder#visitField(Filed)} ignores this exception.
*/
CtType<?> ctType = new JavaReflectionTreeBuilder(createFactory()).scan(spoon.support.visitor.java.testclasses.NPEInStaticInit.class);

CtField<?> value = ctType.getField("VALUE");
// should have gotten '1'
assertNull(value.getDefaultExpression());

/*
* contract:
* JavaReflectionTreeBuilder can't set defaultExpression for the field (public static primitive),
* as Reflection API throws the exception UnsatisfiedLinkError when attempting to get it.
* {@link JavaReflectionTreeBuilder#visitField(Filed)} ignores this exception.
*/
ctType = new JavaReflectionTreeBuilder(createFactory()).scan(spoon.support.visitor.java.testclasses.UnsatisfiedLinkErrorInStaticInit.class);

value = ctType.getField("VALUE");
// should have gotten '1'
assertNull(value.getDefaultExpression());
}
}
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 !!!
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package spoon.support.visitor.java.testclasses;

public class UnsatisfiedLinkErrorInStaticInit {

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

static {
System.load("not found path!");
}
}