use of spoon.reflect.cu.CompilationUnit 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());
}
use of spoon.reflect.cu.CompilationUnit in project spoon by INRIA.
the class PositionBuilder method buildPosition.
SourcePosition buildPosition(int sourceStart, int sourceEnd) {
CompilationUnit cu = this.jdtTreeBuilder.getContextBuilder().compilationUnitSpoon;
final int[] lineSeparatorPositions = this.jdtTreeBuilder.getContextBuilder().compilationunitdeclaration.compilationResult.lineSeparatorPositions;
return this.jdtTreeBuilder.getFactory().Core().createSourcePosition(cu, sourceStart, sourceEnd, lineSeparatorPositions);
}
use of spoon.reflect.cu.CompilationUnit in project spoon by INRIA.
the class PositionBuilder method buildPositionCtElement.
SourcePosition buildPositionCtElement(CtElement e, ASTNode node) {
CoreFactory cf = this.jdtTreeBuilder.getFactory().Core();
CompilationUnit cu = this.jdtTreeBuilder.getFactory().CompilationUnit().getOrCreate(new String(this.jdtTreeBuilder.getContextBuilder().compilationunitdeclaration.getFileName()));
CompilationResult cr = this.jdtTreeBuilder.getContextBuilder().compilationunitdeclaration.compilationResult;
int[] lineSeparatorPositions = cr.lineSeparatorPositions;
char[] contents = cr.compilationUnit.getContents();
int sourceStart = node.sourceStart;
int sourceEnd = node.sourceEnd;
if ((node instanceof Annotation)) {
Annotation ann = (Annotation) node;
int declEnd = ann.declarationSourceEnd;
if (declEnd > 0) {
sourceEnd = declEnd;
}
} else if ((node instanceof Expression)) {
Expression expression = (Expression) node;
int statementEnd = expression.statementEnd;
if (statementEnd > 0) {
sourceEnd = statementEnd;
}
}
if (node instanceof AbstractVariableDeclaration) {
AbstractVariableDeclaration variableDeclaration = (AbstractVariableDeclaration) node;
int modifiersSourceStart = variableDeclaration.modifiersSourceStart;
int declarationSourceStart = variableDeclaration.declarationSourceStart;
int declarationSourceEnd = variableDeclaration.declarationSourceEnd;
int declarationEnd = variableDeclaration.declarationEnd;
Annotation[] annotations = variableDeclaration.annotations;
if (annotations != null && annotations.length > 0) {
if (annotations[0].sourceStart() == sourceStart) {
modifiersSourceStart = annotations[annotations.length - 1].sourceEnd() + 2;
}
}
if (modifiersSourceStart == 0) {
modifiersSourceStart = declarationSourceStart;
}
int modifiersSourceEnd;
if (variableDeclaration.type != null) {
modifiersSourceEnd = variableDeclaration.type.sourceStart() - 2;
} else {
// variable that has no type such as TypeParameter
modifiersSourceEnd = declarationSourceStart - 1;
}
// when no modifier
if (modifiersSourceStart > modifiersSourceEnd) {
modifiersSourceEnd = modifiersSourceStart - 1;
}
return cf.createDeclarationSourcePosition(cu, sourceStart, sourceEnd, modifiersSourceStart, modifiersSourceEnd, declarationSourceStart, declarationSourceEnd, lineSeparatorPositions);
} else if (node instanceof TypeDeclaration) {
TypeDeclaration typeDeclaration = (TypeDeclaration) node;
int declarationSourceStart = typeDeclaration.declarationSourceStart;
int declarationSourceEnd = typeDeclaration.declarationSourceEnd;
int modifiersSourceStart = typeDeclaration.modifiersSourceStart;
int bodyStart = typeDeclaration.bodyStart;
int bodyEnd = typeDeclaration.bodyEnd;
Annotation[] annotations = typeDeclaration.annotations;
if (annotations != null && annotations.length > 0) {
if (annotations[0].sourceStart() == declarationSourceStart) {
modifiersSourceStart = findNextNonWhitespace(contents, annotations[annotations.length - 1].declarationSourceEnd + 1);
}
}
if (modifiersSourceStart == 0) {
modifiersSourceStart = declarationSourceStart;
}
// look for start of first keyword before the type keyword e.g. "class". `sourceStart` points at first char of type name
int modifiersSourceEnd = findPrevNonWhitespace(contents, findPrevWhitespace(contents, findPrevNonWhitespace(contents, sourceStart - 1)));
if (modifiersSourceEnd < modifiersSourceStart) {
// there is no modifier
modifiersSourceEnd = modifiersSourceStart - 1;
}
return cf.createBodyHolderSourcePosition(cu, sourceStart, sourceEnd, modifiersSourceStart, modifiersSourceEnd, declarationSourceStart, declarationSourceEnd, bodyStart - 1, bodyEnd, lineSeparatorPositions);
} else if (node instanceof AbstractMethodDeclaration) {
AbstractMethodDeclaration methodDeclaration = (AbstractMethodDeclaration) node;
int bodyStart = methodDeclaration.bodyStart;
int bodyEnd = methodDeclaration.bodyEnd;
int declarationSourceStart = methodDeclaration.declarationSourceStart;
int declarationSourceEnd = methodDeclaration.declarationSourceEnd;
int modifiersSourceStart = methodDeclaration.modifiersSourceStart;
if (modifiersSourceStart == 0) {
modifiersSourceStart = declarationSourceStart;
}
if (node instanceof AnnotationMethodDeclaration && bodyStart == bodyEnd) {
// The ";" at the end of annotation method declaration is not part of body
// let it behave same like in abstract MethodDeclaration
bodyEnd--;
}
Javadoc javadoc = methodDeclaration.javadoc;
if (javadoc != null && javadoc.sourceEnd() > declarationSourceStart) {
modifiersSourceStart = javadoc.sourceEnd() + 1;
}
Annotation[] annotations = methodDeclaration.annotations;
if (annotations != null && annotations.length > 0) {
if (annotations[0].sourceStart() == declarationSourceStart) {
modifiersSourceStart = annotations[annotations.length - 1].sourceEnd() + 2;
}
}
int modifiersSourceEnd = sourceStart - 1;
if (methodDeclaration instanceof MethodDeclaration && ((MethodDeclaration) methodDeclaration).returnType != null) {
modifiersSourceEnd = ((MethodDeclaration) methodDeclaration).returnType.sourceStart() - 2;
}
TypeParameter[] typeParameters = methodDeclaration.typeParameters();
if (typeParameters != null && typeParameters.length > 0) {
modifiersSourceEnd = typeParameters[0].declarationSourceStart - 3;
}
if (getModifiers(methodDeclaration.modifiers, false, true).isEmpty()) {
modifiersSourceStart = modifiersSourceEnd + 1;
}
sourceEnd = sourceStart + methodDeclaration.selector.length - 1;
if (e instanceof CtStatementList) {
return cf.createSourcePosition(cu, bodyStart - 1, bodyEnd + 1, lineSeparatorPositions);
} else {
if (bodyStart == 0) {
return SourcePosition.NOPOSITION;
} else {
if (bodyStart < bodyEnd) {
// include brackets if they are there
if (contents[bodyStart - 1] == '{') {
bodyStart--;
if (contents[bodyEnd + 1] == '}') {
bodyEnd++;
} else {
throw new SpoonException("Missing body end in\n" + new String(contents, sourceStart, sourceEnd - sourceStart));
}
}
}
return cf.createBodyHolderSourcePosition(cu, sourceStart, sourceEnd, modifiersSourceStart, modifiersSourceEnd, declarationSourceStart, declarationSourceEnd, bodyStart, bodyEnd, lineSeparatorPositions);
}
}
}
return cf.createSourcePosition(cu, sourceStart, sourceEnd, lineSeparatorPositions);
}
use of spoon.reflect.cu.CompilationUnit in project spoon by INRIA.
the class SubstitutionVisitor method getGeneratedByComment.
private static String getGeneratedByComment(CtElement ele) {
SourcePosition pos = ele.getPosition();
if (pos != null) {
CompilationUnit cu = pos.getCompilationUnit();
if (cu != null) {
CtType<?> mainType = cu.getMainType();
if (mainType != null) {
StringBuilder result = new StringBuilder();
result.append("Generated by ");
result.append(mainType.getQualifiedName());
appendInnerTypedElements(result, mainType, ele);
result.append('(');
result.append(mainType.getSimpleName());
result.append(".java:");
result.append(pos.getLine());
result.append(')');
return result.toString();
}
}
}
return null;
}
use of spoon.reflect.cu.CompilationUnit in project spoon by INRIA.
the class ImportTest method testImportStarredPackageWithNonVisibleClass.
@Test
public void testImportStarredPackageWithNonVisibleClass() throws IOException {
// contract: when importing starred import, it should import the starred import
final Launcher launcher = new Launcher();
launcher.getEnvironment().setAutoImports(true);
launcher.getEnvironment().setShouldCompile(true);
launcher.addInputResource("./src/test/java/spoon/test/imports/testclasses/internal/");
launcher.addInputResource("./src/test/java/spoon/test/imports/testclasses/DumbClassUsingInternal.java");
launcher.run();
File f = new File("./src/test/java/spoon/test/imports/testclasses/DumbClassUsingInternal.java");
CompilationUnit cu = launcher.getFactory().CompilationUnit().getMap().get(f.getCanonicalPath());
assertNotNull(cu);
assertEquals(1, cu.getImports().size());
assertEquals(CtImportKind.ALL_TYPES, cu.getImports().iterator().next().getImportKind());
}
Aggregations