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(printer): protects each getParent invocation in DefaultJavaPrettyPrinter #704

Merged
merged 1 commit into from
Jun 16, 2016
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
66 changes: 48 additions & 18 deletions src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -797,8 +797,14 @@ public <T> void visitCtClass(CtClass<T> ctClass) {
lst.addAll(ctClass.getMethods());
lst.addAll(getComments(ctClass, CommentOffset.INSIDE));

if ((ctClass.getSimpleName() == null || ctClass.getSimpleName().isEmpty()) && ctClass.getParent() != null && ctClass.getParent() instanceof CtNewClass) {
context.currentThis.push(((CtNewClass<?>) ctClass.getParent()).getType());
CtElement parent;
try {
parent = ctClass.getParent();
} catch (ParentNotInitializedException e) {
parent = null;
}
if ((ctClass.getSimpleName() == null || ctClass.getSimpleName().isEmpty()) && parent != null && parent instanceof CtNewClass) {
context.currentThis.push(((CtNewClass<?>) parent).getType());
} else {
context.currentThis.push(ctClass.getReference());
}
Expand Down Expand Up @@ -1052,9 +1058,17 @@ private <T> void printCtFieldAccess(CtFieldAccess<T> f) {
* Check if the target expression is a static final field initialized in a static anonymous block.
*/
private <T> boolean isInitializeStaticFinalField(CtExpression<T> targetExp) {
final CtElement parent = targetExp.getParent();
if (parent instanceof CtFieldWrite && targetExp.equals(((CtFieldWrite) parent).getTarget())
&& targetExp.getParent(CtAnonymousExecutable.class) != null
final CtElement parent;
final CtAnonymousExecutable anonymousParent;
try {
parent = targetExp.getParent();
anonymousParent = targetExp.getParent(CtAnonymousExecutable.class);
} catch (ParentNotInitializedException e) {
return false;
}
if (parent instanceof CtFieldWrite
&& targetExp.equals(((CtFieldWrite) parent).getTarget())
&& anonymousParent != null
&& ((CtFieldWrite) parent).getVariable() != null
&& ((CtFieldWrite) parent).getVariable().getModifiers().contains(ModifierKind.STATIC)) {
return true;
Expand Down Expand Up @@ -1087,19 +1101,23 @@ public <T> void visitCtThisAccess(CtThisAccess<T> thisAccess) {
* Check if the this access expression is a target of a private final field in a constructor.
*/
private <T> boolean tryToInitializeFinalFieldInConstructor(CtThisAccess<T> thisAccess) {
final CtElement parent = thisAccess.getParent();
if (!(parent instanceof CtFieldWrite) || !thisAccess.equals(((CtFieldWrite) parent).getTarget()) || thisAccess.getParent(CtConstructor.class) == null) {
return false;
}
final CtFieldReference variable = ((CtFieldWrite) parent).getVariable();
if (variable == null) {
try {
final CtElement parent = thisAccess.getParent();
if (!(parent instanceof CtFieldWrite) || !thisAccess.equals(((CtFieldWrite) parent).getTarget()) || thisAccess.getParent(CtConstructor.class) == null) {
return false;
}
final CtFieldReference variable = ((CtFieldWrite) parent).getVariable();
if (variable == null) {
return false;
}
final CtField declaration = variable.getDeclaration();
if (declaration == null) {
return true;
}
return variable.getDeclaration().getModifiers().contains(ModifierKind.FINAL);
} catch (ParentNotInitializedException e) {
return false;
}
final CtField declaration = variable.getDeclaration();
if (declaration == null) {
return true;
}
return variable.getDeclaration().getModifiers().contains(ModifierKind.FINAL);
}

@Override
Expand Down Expand Up @@ -1361,7 +1379,12 @@ public <T> void visitCtInvocation(CtInvocation<T> invocation) {
if (invocation.getExecutable().isConstructor()) {
// It's a constructor (super or this)
writeActualTypeArguments(invocation.getExecutable());
CtType<?> parentType = invocation.getParent(CtType.class);
CtType<?> parentType;
try {
parentType = invocation.getParent(CtType.class);
} catch (ParentNotInitializedException e) {
parentType = null;
}
if (parentType != null && parentType.getQualifiedName() != null && parentType.getQualifiedName().equals(invocation.getExecutable().getDeclaringType().getQualifiedName())) {
write("this");
} else {
Expand Down Expand Up @@ -1722,7 +1745,14 @@ public void visitCtNamedElement(CtNamedElement e) {
public <T> void visitCtNewArray(CtNewArray<T> newArray) {
enterCtExpression(newArray);

if ((newArray.getParent(CtAnnotationType.class) == null) && (newArray.getParent(CtAnnotation.class) == null)) {
boolean isNotInAnnotation;
try {
isNotInAnnotation = (newArray.getParent(CtAnnotationType.class) == null) && (newArray.getParent(CtAnnotation.class) == null);
} catch (ParentNotInitializedException e) {
isNotInAnnotation = true;
}

if (isNotInAnnotation) {
CtTypeReference<?> ref = newArray.getType();

if (ref != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ public String toString() {
printer.computeImports(this);
printer.scan(this);
} catch (ParentNotInitializedException ignore) {
LOGGER.error(ERROR_MESSAGE_TO_STRING, ignore);
errorMessage = ERROR_MESSAGE_TO_STRING;
}
return printer.toString() + errorMessage;
Expand Down