Skip to content

Commit

Permalink
fix(printer): protects each getParent invocation in DefaultJavaPretty…
Browse files Browse the repository at this point in the history
…Printer (#704)
  • Loading branch information
tdurieux authored and GerardPaligot committed Jun 16, 2016
1 parent cf1204d commit 0f6a4a9
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 18 deletions.
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 @@ -788,8 +788,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 @@ -1043,9 +1049,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 @@ -1078,19 +1092,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 @@ -1352,7 +1370,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 @@ -1713,7 +1736,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 @@ -260,6 +260,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

0 comments on commit 0f6a4a9

Please sign in to comment.