use of spoon.reflect.declaration.ParentNotInitializedException in project spoon by INRIA.
the class SetParentTest method testContract.
@Test
public void testContract() throws Throwable {
Object o = factory.Core().create((Class<? extends CtElement>) toTest.getActualClass());
CtMethod<?> setter = factory.Type().get(CtElement.class).getMethodsByName("setParent").get(0);
Object argument = createCompatibleObject(setter.getParameters().get(0).getType());
if (!(argument instanceof CtElement)) {
// is a primitive type or a list
throw new AssertionError("impossible, setParent always takes an element");
}
// we create a fresh object
CtElement receiver = ((CtElement) o).clone();
if ("CtClass".equals(toTest.getSimpleName()) || "CtInterface".equals(toTest.getSimpleName()) || "CtEnum".equals(toTest.getSimpleName()) || "CtAnnotationType".equals(toTest.getSimpleName()) || "CtPackage".equals(toTest.getSimpleName())) {
// contract: root package is the parent for those classes
assertTrue(receiver.getParent() instanceof CtModelImpl.CtRootPackage);
} else if ("CtModule".equals(toTest.getSimpleName())) {
// contract: module parent is necessarily the unnamedmodule
assertTrue(receiver.getParent() instanceof ModuleFactory.CtUnnamedModule);
} else {
// contract: there is no parent before
try {
receiver.getParent().hashCode();
fail(receiver.getParent().getClass().getSimpleName());
} catch (ParentNotInitializedException normal) {
}
}
Method actualMethod = setter.getReference().getActualMethod();
CtElement argumentClone = ((CtElement) argument).clone();
actualMethod.invoke(receiver, new Object[] { argument });
// contract: the parent has not been changed by a call to setParent on an elemnt
assertTrue(argument.equals(argumentClone));
assertFalse(argument == argumentClone);
}
use of spoon.reflect.declaration.ParentNotInitializedException in project spoon by INRIA.
the class DefaultJavaPrettyPrinter method visitCtNewArray.
@Override
@SuppressWarnings("rawtypes")
public <T> void visitCtNewArray(CtNewArray<T> newArray) {
enterCtExpression(newArray);
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) {
printer.writeKeyword("new").writeSpace();
}
try (Writable _context = context.modify().skipArray(true)) {
scan(ref);
}
for (int i = 0; ref instanceof CtArrayTypeReference; i++) {
printer.writeSeparator("[");
if (newArray.getDimensionExpressions().size() > i) {
CtExpression<Integer> e = newArray.getDimensionExpressions().get(i);
scan(e);
}
printer.writeSeparator("]");
ref = ((CtArrayTypeReference) ref).getComponentType();
}
}
if (newArray.getDimensionExpressions().size() == 0) {
try (ListPrinter lp = elementPrinterHelper.createListPrinter(false, "{", true, false, ",", true, true, "}")) {
for (CtExpression e : newArray.getElements()) {
lp.printSeparatorIfAppropriate();
scan(e);
}
elementPrinterHelper.writeComment(newArray, CommentOffset.INSIDE);
}
}
elementPrinterHelper.writeComment(newArray, CommentOffset.AFTER);
exitCtExpression(newArray);
}
use of spoon.reflect.declaration.ParentNotInitializedException in project spoon by INRIA.
the class JDTCommentBuilder method insertCommentInAST.
/**
* Inserts the comment into the AST.
* @param comment the comment to insert
*/
private void insertCommentInAST(final CtComment comment) {
CtElement commentParent = findCommentParent(comment);
if (commentParent == null) {
File file = spoonUnit.getFile();
if (file != null && file.getName().equals(DefaultJavaPrettyPrinter.JAVA_PACKAGE_DECLARATION)) {
spoonUnit.getDeclaredPackage().addComment(comment);
} else if (file != null && file.getName().equals(DefaultJavaPrettyPrinter.JAVA_MODULE_DECLARATION)) {
spoonUnit.getDeclaredModule().addComment(comment);
} else {
comment.setCommentType(CtComment.CommentType.FILE);
addCommentToNear(comment, new ArrayList<CtElement>(spoonUnit.getDeclaredTypes()));
}
return;
}
// visitor that inserts the comment in the element
CtInheritanceScanner insertionVisitor = new CtInheritanceScanner() {
private boolean isScanned = false;
@Override
public void scan(CtElement e) {
if (e == null) {
return;
}
// Do not visit the AST, only the first element
if (!isScanned) {
isScanned = true;
if (e.getPosition().getSourceStart() == comment.getPosition().getSourceStart()) {
e.addComment(comment);
return;
}
super.scan(e);
}
}
@Override
public <R> void visitCtStatementList(CtStatementList e) {
addCommentToNear(comment, new ArrayList<CtElement>(e.getStatements()));
try {
comment.getParent();
} catch (ParentNotInitializedException ex) {
e.addStatement(comment);
}
}
@Override
public <T> void visitCtMethod(CtMethod<T> e) {
e.addComment(comment);
}
@Override
public <T> void visitCtConstructor(CtConstructor<T> e) {
e.addComment(comment);
}
@Override
public <T> void visitCtConditional(CtConditional<T> e) {
List<CtElement> elements = new ArrayList<>();
elements.add(e.getElseExpression());
elements.add(e.getThenExpression());
elements.add(e.getCondition());
addCommentToNear(comment, elements);
}
@Override
public <T> void visitCtBinaryOperator(CtBinaryOperator<T> e) {
List<CtElement> elements = new ArrayList<>();
elements.add(e.getLeftHandOperand());
elements.add(e.getRightHandOperand());
addCommentToNear(comment, elements);
}
@Override
public <T> void visitCtClass(CtClass<T> e) {
if (comment.getPosition().getLine() <= e.getPosition().getLine()) {
e.addComment(comment);
return;
}
final List<CtElement> elements = new ArrayList<>();
for (CtTypeMember typeMember : e.getTypeMembers()) {
if (typeMember instanceof CtField || typeMember instanceof CtMethod || typeMember instanceof CtConstructor) {
elements.add(typeMember);
}
}
addCommentToNear(comment, elements);
try {
comment.getParent();
} catch (ParentNotInitializedException ex) {
e.addComment(comment);
}
}
@Override
public <T> void visitCtInterface(CtInterface<T> e) {
final List<CtElement> elements = new ArrayList<>();
for (CtTypeMember typeMember : e.getTypeMembers()) {
if (typeMember instanceof CtField || typeMember instanceof CtMethod) {
elements.add(typeMember);
}
}
addCommentToNear(comment, elements);
try {
comment.getParent();
} catch (ParentNotInitializedException ex) {
e.addComment(comment);
}
}
@Override
public <T> void visitCtField(CtField<T> e) {
e.addComment(comment);
}
@Override
public <E> void visitCtSwitch(CtSwitch<E> e) {
List<CtCase<? super E>> cases = e.getCases();
CtCase previous = null;
for (int i = 0; i < cases.size(); i++) {
CtCase<? super E> ctCase = cases.get(i);
if (previous == null) {
if (comment.getPosition().getSourceStart() < ctCase.getPosition().getSourceStart() && e.getPosition().getSourceStart() < comment.getPosition().getSourceStart()) {
ctCase.addComment(comment);
return;
}
} else {
if (previous.getPosition().getSourceEnd() < comment.getPosition().getSourceStart() && ctCase.getPosition().getSourceStart() > comment.getPosition().getSourceStart()) {
addCommentToNear(comment, new ArrayList<CtElement>(previous.getStatements()));
try {
comment.getParent();
} catch (ParentNotInitializedException ex) {
previous.addStatement(comment);
}
return;
}
}
previous = ctCase;
}
if (previous.getPosition().getSourceEnd() < comment.getPosition().getSourceStart()) {
addCommentToNear(comment, new ArrayList<CtElement>(previous.getStatements()));
try {
comment.getParent();
} catch (ParentNotInitializedException ex) {
previous.addStatement(comment);
}
return;
}
try {
comment.getParent();
} catch (ParentNotInitializedException ex) {
e.addComment(comment);
}
}
@Override
public void visitCtIf(CtIf e) {
if (!(e.getThenStatement() instanceof CtBlock)) {
if (comment.getPosition().getSourceEnd() <= e.getThenStatement().getPosition().getSourceStart()) {
e.getThenStatement().addComment(comment);
return;
}
}
if (e.getElseStatement() != null) {
SourcePosition thenPosition = e.getThenStatement().getPosition() == null ? ((CtBlock) e.getThenStatement()).getStatement(0).getPosition() : e.getThenStatement().getPosition();
SourcePosition elsePosition = e.getElseStatement().getPosition() == null ? ((CtBlock) e.getElseStatement()).getStatement(0).getPosition() : e.getElseStatement().getPosition();
if (comment.getPosition().getSourceStart() > thenPosition.getSourceEnd() && comment.getPosition().getSourceEnd() < elsePosition.getSourceStart()) {
e.getElseStatement().addComment(comment);
}
}
try {
comment.getParent();
} catch (ParentNotInitializedException ex) {
e.addComment(comment);
}
}
@Override
public void scanCtStatement(CtStatement s) {
if (!(s instanceof CtStatementList || s instanceof CtSwitch)) {
s.addComment(comment);
}
}
@Override
public void visitCtAnonymousExecutable(CtAnonymousExecutable e) {
e.addComment(comment);
}
@Override
public <T> void visitCtNewArray(CtNewArray<T> e) {
addCommentToNear(comment, new ArrayList<CtElement>(e.getElements()));
try {
comment.getParent();
} catch (ParentNotInitializedException ex) {
e.addComment(comment);
}
}
@Override
public <T> void visitCtParameter(CtParameter<T> e) {
e.addComment(comment);
}
@Override
public void visitCtCatch(CtCatch e) {
if (comment.getPosition().getLine() <= e.getPosition().getLine()) {
e.addComment(comment);
return;
}
}
@Override
public void visitCtModule(CtModule module) {
addCommentToNear(comment, new ArrayList<>(module.getModuleDirectives()));
}
};
insertionVisitor.scan(commentParent);
try {
comment.getParent();
} catch (ParentNotInitializedException e) {
LOGGER.error(comment + " is not added into the AST", e);
}
}
use of spoon.reflect.declaration.ParentNotInitializedException in project spoon by INRIA.
the class CtElementImpl method toString.
@Override
public String toString() {
DefaultJavaPrettyPrinter printer = new DefaultJavaPrettyPrinter(getFactory().getEnvironment());
String errorMessage = "";
try {
// we do not want to compute imports of a CtImport as it may change the print of a reference
if (!(this instanceof CtImport)) {
printer.computeImports(this);
}
printer.scan(this);
} catch (ParentNotInitializedException ignore) {
LOGGER.error(ERROR_MESSAGE_TO_STRING, ignore);
errorMessage = ERROR_MESSAGE_TO_STRING;
}
// removing them from the toString() representation
return printer.toString().replaceFirst("^\\s+", "") + errorMessage;
}
use of spoon.reflect.declaration.ParentNotInitializedException in project spoon by INRIA.
the class CtStatementImpl method insertBefore.
/**
* inserts all statements of `statementsToBeInserted` just before `target`
*/
public static void insertBefore(CtStatement target, CtStatementList statementsToBeInserted) throws ParentNotInitializedException {
CtElement targetParent = target.getParent();
if (targetParent instanceof CtExecutable) {
throw new SpoonException("cannot insert in this context (use insertEnd?)");
}
try {
if (target.getParent(CtConstructor.class) != null) {
if (target instanceof CtInvocation && ((CtInvocation<?>) target).getExecutable().getSimpleName().startsWith(CtExecutableReference.CONSTRUCTOR_NAME)) {
throw new SpoonException("cannot insert a statement before a super or this invocation.");
}
}
} catch (ParentNotInitializedException ignore) {
// no parent set somewhere
}
new InsertVisitor(target, statementsToBeInserted, InsertType.BEFORE).scan(targetParent);
}
Aggregations