use of spoon.reflect.cu.SourcePosition in project spoon by INRIA.
the class JDTCommentBuilder method buildComment.
/**
* Inserts the comment at the position positions in the AST
* @param positions the position of the comment
*/
private void buildComment(int[] positions) {
int start = positions[0];
int end = -positions[1];
CtComment comment;
// Javadoc comments have negative end position
if (end <= 0) {
comment = factory.Core().createJavaDoc();
end = -end;
} else {
comment = factory.Core().createComment();
comment.setCommentType(CtComment.CommentType.BLOCK);
// the inline comments have negative start
if (start < 0) {
comment.setCommentType(CtComment.CommentType.INLINE);
start = -start;
}
}
/**
* Comment, which contains only javadoc tags never set content.
* So set content now, to avoid unexpected null content.
*/
comment.setContent("");
String commentContent = getCommentContent(start, end);
int[] lineSeparatorPositions = declarationUnit.compilationResult.lineSeparatorPositions;
SourcePosition sourcePosition = factory.Core().createSourcePosition(spoonUnit, start, end, lineSeparatorPositions);
// create the Spoon comment element
comment = parseTags(comment, commentContent);
comment.setPosition(sourcePosition);
insertCommentInAST(comment);
}
use of spoon.reflect.cu.SourcePosition in project spoon by INRIA.
the class ParentExiter method visitCtBinaryOperator.
@Override
public <T> void visitCtBinaryOperator(CtBinaryOperator<T> operator) {
if (child instanceof CtExpression) {
if (operator.getLeftHandOperand() == null) {
operator.setLeftHandOperand((CtExpression<?>) child);
return;
} else if (operator.getRightHandOperand() == null) {
operator.setRightHandOperand((CtExpression<?>) child);
return;
} else if (jdtTreeBuilder.getContextBuilder().stack.peek().node instanceof StringLiteralConcatenation) {
CtBinaryOperator<?> op = operator.getFactory().Core().createBinaryOperator();
op.setKind(BinaryOperatorKind.PLUS);
op.setLeftHandOperand(operator.getRightHandOperand());
op.setRightHandOperand((CtExpression<?>) child);
operator.setRightHandOperand(op);
int[] lineSeparatorPositions = this.jdtTreeBuilder.getContextBuilder().compilationunitdeclaration.compilationResult.lineSeparatorPositions;
SourcePosition leftPosition = op.getLeftHandOperand().getPosition();
SourcePosition rightPosition = op.getRightHandOperand().getPosition();
op.setPosition(op.getFactory().createSourcePosition(leftPosition.getCompilationUnit(), leftPosition.getSourceStart(), rightPosition.getSourceEnd(), lineSeparatorPositions));
return;
}
}
super.visitCtBinaryOperator(operator);
}
use of spoon.reflect.cu.SourcePosition in project spoon by INRIA.
the class StandardEnvironment method report.
@Override
public void report(Processor<?> processor, Level level, CtElement element, String message) {
StringBuffer buffer = new StringBuffer();
prefix(buffer, level);
// Adding message
buffer.append(message);
// Add sourceposition (javac format)
try {
CtType<?> type = (element instanceof CtType) ? (CtType<?>) element : element.getParent(CtType.class);
SourcePosition sp = element.getPosition();
if (sp == null) {
buffer.append(" (Unknown Source)");
} else {
buffer.append(" at " + type.getQualifiedName() + ".");
CtExecutable<?> exe = (element instanceof CtExecutable) ? (CtExecutable<?>) element : element.getParent(CtExecutable.class);
if (exe != null) {
buffer.append(exe.getSimpleName());
}
buffer.append("(" + sp.getFile().getName() + ":" + sp.getLine() + ")");
}
} catch (ParentNotInitializedException e) {
buffer.append(" (invalid parent)");
}
print(buffer.toString(), level);
}
use of spoon.reflect.cu.SourcePosition in project spoon by INRIA.
the class PositionTest method testPositionMethod.
@Test
public void testPositionMethod() throws Exception {
final Factory build = build(FooMethod.class);
final CtClass<FooMethod> foo = build.Class().get(FooMethod.class);
String classContent = getClassContent(foo);
CtMethod<?> method1 = foo.getMethodsByName("m").get(0);
BodyHolderSourcePosition position1 = (BodyHolderSourcePosition) method1.getPosition();
assertEquals(5, position1.getLine());
assertEquals(7, position1.getEndLine());
assertEquals(69, position1.getSourceStart());
assertEquals(114, position1.getSourceEnd());
assertEquals("public static void m(int parm1) {\n" + "\t\treturn;\n" + "\t}", contentAtPosition(classContent, position1));
assertEquals("m", contentAtPosition(classContent, position1.getNameStart(), position1.getNameEnd()));
assertEquals("public static", contentAtPosition(classContent, position1.getModifierSourceStart(), position1.getModifierSourceEnd()));
// contract: body contains starting and ending brackets {}
assertEquals("{\n" + "\t\treturn;\n" + "\t}", contentAtPosition(classContent, position1.getBodyStart(), position1.getBodyEnd()));
DeclarationSourcePosition positionParam1 = (DeclarationSourcePosition) method1.getParameters().get(0).getPosition();
assertEquals(5, positionParam1.getLine());
assertEquals(5, positionParam1.getEndLine());
assertEquals(90, positionParam1.getSourceStart());
assertEquals(98, positionParam1.getSourceEnd());
assertEquals("int parm1", contentAtPosition(classContent, positionParam1));
assertEquals("parm1", contentAtPosition(classContent, positionParam1.getNameStart(), positionParam1.getNameEnd()));
assertEquals("", contentAtPosition(classContent, positionParam1.getModifierSourceStart(), positionParam1.getModifierSourceEnd()));
CtMethod method2 = foo.getMethodsByName("mWithDoc").get(0);
BodyHolderSourcePosition position2 = (BodyHolderSourcePosition) method2.getPosition();
assertEquals(13, position2.getLine());
assertEquals(15, position2.getEndLine());
assertEquals("/**\n" + "\t * Mathod with javadoc\n" + "\t * @param parm1 the parameter\n" + "\t */\n" + "\tint mWithDoc(int parm1) {\n" + "\t\treturn parm1;\n" + "\t}", contentAtPosition(classContent, position2));
assertEquals("mWithDoc", contentAtPosition(classContent, position2.getNameStart(), position2.getNameEnd()));
assertEquals("", contentAtPosition(classContent, position2.getModifierSourceStart(), position2.getModifierSourceEnd()));
CtConstructor<FooMethod> constructor = foo.getConstructor(build.Type().integerPrimitiveType());
SourcePosition position3 = constructor.getPosition();
contentAtPosition(classContent, position3);
CtMethod mWithLine = foo.getMethod("mWithLine", build.Type().integerPrimitiveType());
SourcePosition position4 = mWithLine.getPosition();
contentAtPosition(classContent, position4);
}
use of spoon.reflect.cu.SourcePosition in project spoon by INRIA.
the class PositionTest method getPositionOfImplicitBlock.
@Test
public void getPositionOfImplicitBlock() {
// contract: position of implicit block in if (cond) [implicit block] else [implicit block] should be the source position of implicit block content.
Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/position/testclasses/ImplicitBlock.java");
launcher.buildModel();
CtIf ifElement = launcher.getModel().getElements(new TypeFilter<CtIf>(CtIf.class)).get(0);
CtStatement thenStatement = ifElement.getThenStatement();
assertTrue(thenStatement instanceof CtBlock);
CtBlock thenBlock = (CtBlock) thenStatement;
SourcePosition positionThen = thenBlock.getPosition();
CtStatement returnStatement = thenBlock.getStatement(0);
assertEquals(returnStatement.getPosition(), positionThen);
assertEquals("ImplicitBlock.java", positionThen.getFile().getName());
assertEquals(7, positionThen.getLine());
CtStatement elseStatement = ifElement.getElseStatement();
assertTrue(elseStatement instanceof CtBlock);
CtBlock elseBlock = (CtBlock) elseStatement;
SourcePosition positionElse = elseBlock.getPosition();
CtStatement otherReturnStatement = elseBlock.getStatement(0);
assertEquals(otherReturnStatement.getPosition(), positionElse);
assertEquals("ImplicitBlock.java", positionThen.getFile().getName());
assertEquals(8, positionElse.getLine());
assertNotEquals(returnStatement, otherReturnStatement);
}
Aggregations