Search in sources :

Example 6 with SourcePosition

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());
    }
}
Also used : CtComment(spoon.reflect.code.CtComment) CtBlock(spoon.reflect.code.CtBlock) SourcePosition(spoon.reflect.cu.SourcePosition)

Example 7 with SourcePosition

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;
}
Also used : SourcePosition(spoon.reflect.cu.SourcePosition) NoSourcePosition(spoon.reflect.cu.position.NoSourcePosition) NoSourcePosition(spoon.reflect.cu.position.NoSourcePosition)

Example 8 with SourcePosition

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());
    }
}
Also used : SourcePosition(spoon.reflect.cu.SourcePosition) Factory(spoon.reflect.factory.Factory) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 9 with SourcePosition

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();
    }
}
Also used : SourcePosition(spoon.reflect.cu.SourcePosition)

Example 10 with SourcePosition

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());
}
Also used : CompilationUnit(spoon.reflect.cu.CompilationUnit) CtClass(spoon.reflect.declaration.CtClass) PartialSourcePositionImpl(spoon.support.reflect.cu.position.PartialSourcePositionImpl) SourcePosition(spoon.reflect.cu.SourcePosition) NoSourcePosition(spoon.reflect.cu.position.NoSourcePosition) Launcher(spoon.Launcher) File(java.io.File) Test(org.junit.Test)

Aggregations

SourcePosition (spoon.reflect.cu.SourcePosition)16 Test (org.junit.Test)8 BodyHolderSourcePosition (spoon.reflect.cu.position.BodyHolderSourcePosition)6 DeclarationSourcePosition (spoon.reflect.cu.position.DeclarationSourcePosition)6 Factory (spoon.reflect.factory.Factory)5 CtBlock (spoon.reflect.code.CtBlock)4 File (java.io.File)3 Launcher (spoon.Launcher)3 CtIf (spoon.reflect.code.CtIf)3 CtMethod (spoon.reflect.declaration.CtMethod)3 CtComment (spoon.reflect.code.CtComment)2 CtExpression (spoon.reflect.code.CtExpression)2 CtStatement (spoon.reflect.code.CtStatement)2 CompilationUnit (spoon.reflect.cu.CompilationUnit)2 NoSourcePosition (spoon.reflect.cu.position.NoSourcePosition)2 CtClass (spoon.reflect.declaration.CtClass)2 ParentNotInitializedException (spoon.reflect.declaration.ParentNotInitializedException)2 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)2 ArrayList (java.util.ArrayList)1 StringLiteralConcatenation (org.eclipse.jdt.internal.compiler.ast.StringLiteralConcatenation)1