use of spoon.reflect.cu.SourcePosition in project spoon by INRIA.
the class DefaultJavaPrettyPrinter method visitCtIf.
@Override
public void visitCtIf(CtIf ifElement) {
enterCtStatement(ifElement);
printer.writeKeyword("if").writeSpace().writeSeparator("(");
scan(ifElement.getCondition());
printer.writeSeparator(")");
elementPrinterHelper.writeIfOrLoopBlock(ifElement.getThenStatement());
if (ifElement.getElseStatement() != null) {
List<CtComment> comments = elementPrinterHelper.getComments(ifElement, CommentOffset.INSIDE);
for (CtComment comment : comments) {
SourcePosition thenPosition = ifElement.getThenStatement().getPosition() == null ? ((CtBlock) ifElement.getThenStatement()).getStatement(0).getPosition() : ifElement.getThenStatement().getPosition();
if (comment.getPosition().getSourceStart() > thenPosition.getSourceEnd()) {
elementPrinterHelper.writeComment(comment);
}
}
printer.writeKeyword("else");
elementPrinterHelper.writeIfOrLoopBlock(ifElement.getElseStatement());
}
}
use of spoon.reflect.cu.SourcePosition in project spoon by INRIA.
the class VariableReferencesTest method getPosition.
private SourcePosition getPosition(CtElement e) {
SourcePosition sp = e.getPosition();
while (sp instanceof NoSourcePosition) {
e = e.getParent();
if (e == null) {
break;
}
sp = e.getPosition();
}
return sp;
}
use of spoon.reflect.cu.SourcePosition in project spoon by INRIA.
the class SourcePositionTest method equalPositionsHaveSameHashcode.
@Test
public void equalPositionsHaveSameHashcode() throws Exception {
String packageName = "spoon.test.testclasses";
String sampleClassName = "SampleClass";
String qualifiedName = packageName + "." + sampleClassName;
Filter<CtMethod<?>> methodFilter = new TypeFilter<CtMethod<?>>(CtMethod.class);
Factory aFactory = factoryFor(packageName, sampleClassName);
List<CtMethod<?>> methods = aFactory.Class().get(qualifiedName).getElements(methodFilter);
Factory newInstanceOfSameFactory = factoryFor(packageName, sampleClassName);
List<CtMethod<?>> newInstanceOfSameMethods = newInstanceOfSameFactory.Class().get(qualifiedName).getElements(methodFilter);
assertEquals(methods.size(), newInstanceOfSameMethods.size());
for (int i = 0; i < methods.size(); i += 1) {
SourcePosition aPosition = methods.get(i).getPosition();
SourcePosition newInstanceOfSamePosition = newInstanceOfSameMethods.get(i).getPosition();
assertTrue(aPosition.equals(newInstanceOfSamePosition));
assertEquals(aPosition.hashCode(), newInstanceOfSamePosition.hashCode());
}
}
use of spoon.reflect.cu.SourcePosition in project spoon by INRIA.
the class LiteralHelper method getLiteralToken.
/**
* @param literal to be converted literal
* @return source code representation of the literal
*/
public static <T> String getLiteralToken(CtLiteral<T> literal) {
if (literal.getValue() == null) {
return "null";
} else if (literal.getValue() instanceof Long) {
return literal.getValue() + "L";
} else if (literal.getValue() instanceof Float) {
return literal.getValue() + "F";
} else if (literal.getValue() instanceof Character) {
boolean mayContainsSpecialCharacter = true;
SourcePosition position = literal.getPosition();
if (position != null) {
// the size of the string in the source code, the -1 is the size of the ' or " in the source code
int stringLength = position.getSourceEnd() - position.getSourceStart() - 1;
// if the string in the source is not the same as the string in the literal, the string may contains special characters
mayContainsSpecialCharacter = stringLength != 1;
}
StringBuilder sb = new StringBuilder(10);
sb.append('\'');
appendCharLiteral(sb, (Character) literal.getValue(), mayContainsSpecialCharacter);
sb.append('\'');
return sb.toString();
} else if (literal.getValue() instanceof String) {
boolean mayContainsSpecialCharacters = true;
SourcePosition position = literal.getPosition();
if (position != null) {
// the size of the string in the source code, the -1 is the size of the ' or " in the source code
int stringLength = position.getSourceEnd() - position.getSourceStart() - 1;
// if the string in the source is not the same as the string in the literal, the string may contains special characters
mayContainsSpecialCharacters = ((String) literal.getValue()).length() != stringLength;
}
return "\"" + getStringLiteral((String) literal.getValue(), mayContainsSpecialCharacters) + "\"";
} else if (literal.getValue() instanceof Class) {
return ((Class<?>) literal.getValue()).getName();
} else {
return literal.getValue().toString();
}
}
use of spoon.reflect.cu.SourcePosition in project spoon by INRIA.
the class TestCompilationUnit method testNewlyCreatedCUWouldGetAPartialPosition.
@Test
public void testNewlyCreatedCUWouldGetAPartialPosition() throws IOException {
// contract: when a type is created, a CU can be created and added as partial position
final Launcher launcher = new Launcher();
assertTrue(launcher.getFactory().CompilationUnit().getMap().isEmpty());
CtClass myNewClass = launcher.getFactory().createClass("my.new.MyClass");
assertEquals(SourcePosition.NOPOSITION, myNewClass.getPosition());
CompilationUnit cu = launcher.getFactory().CompilationUnit().getOrCreate(myNewClass);
assertNotNull(cu);
assertSame(cu, launcher.getFactory().CompilationUnit().getOrCreate(myNewClass));
SourcePosition sourcePosition = myNewClass.getPosition();
assertTrue(sourcePosition instanceof PartialSourcePositionImpl);
assertSame(cu, sourcePosition.getCompilationUnit());
File f = new File(Launcher.OUTPUTDIR, "my/new/MyClass.java");
assertEquals(f.getCanonicalFile(), cu.getFile());
}
Aggregations