Skip to content

Commit

Permalink
feat(position): improve the element position
Browse files Browse the repository at this point in the history
  • Loading branch information
tdurieux committed Nov 17, 2016
1 parent 204d9aa commit 2b68632
Show file tree
Hide file tree
Showing 24 changed files with 699 additions and 96 deletions.
@@ -0,0 +1,32 @@
/**
* Copyright (C) 2006-2016 INRIA and contributors
* Spoon - http://spoon.gforge.inria.fr/
*
* This software is governed by the CeCILL-C License under French law and
* abiding by the rules of distribution of free software. You can use, modify
* and/or redistribute the software under the terms of the CeCILL-C license as
* circulated by CEA, CNRS and INRIA at http://www.cecill.info.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details.
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL-C license and that you accept its terms.
*/
package spoon.reflect.cu.position;

/**
* This interface represents the position of a program element in a source file.
*/
public interface DeclarationSourcePosition extends SourcePosition {

int getModifierSourceStart();

int getModifierSourceEnd();

int getNameStart();

int getNameEnd();

}
28 changes: 28 additions & 0 deletions src/main/java/spoon/reflect/cu/position/MethodSourcePosition.java
@@ -0,0 +1,28 @@
/**
* Copyright (C) 2006-2016 INRIA and contributors
* Spoon - http://spoon.gforge.inria.fr/
*
* This software is governed by the CeCILL-C License under French law and
* abiding by the rules of distribution of free software. You can use, modify
* and/or redistribute the software under the terms of the CeCILL-C license as
* circulated by CEA, CNRS and INRIA at http://www.cecill.info.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details.
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL-C license and that you accept its terms.
*/
package spoon.reflect.cu.position;

/**
* This interface represents the position of a Method declaration in a source file.
*/
public interface MethodSourcePosition extends DeclarationSourcePosition {

int getBodyStart();

int getBodyEnd();

}
Expand Up @@ -14,16 +14,67 @@
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL-C license and that you accept its terms.
*/
package spoon.reflect.cu;

import java.io.File;
package spoon.reflect.cu.position;

import spoon.compiler.Environment;
import spoon.reflect.cu.CompilationUnit;

import java.io.File;
import java.io.Serializable;

/**
* This interface represents the position of a program element in a source file.
*/
public interface SourcePosition {
public interface SourcePosition extends Serializable {

SourcePosition NOPOSITION = new SourcePosition () {
private static final long serialVersionUID = 1L;

@Override
public File getFile() {
return null;
}

@Override
public CompilationUnit getCompilationUnit() {
return null;
}

@Override
public int getLine() {
return -1;
}

@Override
public int getEndLine() {
return -1;
}

@Override
public int getColumn() {
return -1;
}

@Override
public int getEndColumn() {
return -1;
}

@Override
public int getSourceEnd() {
return -1;
}

@Override
public int getSourceStart() {
return -1;
}

@Override
public String toString() {
return "(unknown file)";
}
};

/**
* Returns a string representation of this position in the form
Expand Down Expand Up @@ -79,9 +130,4 @@ public interface SourcePosition {
* Gets the index at which the position starts in the source file.
*/
int getSourceStart();

/**
* Gets the index at which the name of the element position starts in the source file.
*/
int getNameSourceStart();
}
28 changes: 28 additions & 0 deletions src/main/java/spoon/reflect/cu/position/TypeSourcePosition.java
@@ -0,0 +1,28 @@
/**
* Copyright (C) 2006-2016 INRIA and contributors
* Spoon - http://spoon.gforge.inria.fr/
*
* This software is governed by the CeCILL-C License under French law and
* abiding by the rules of distribution of free software. You can use, modify
* and/or redistribute the software under the terms of the CeCILL-C license as
* circulated by CEA, CNRS and INRIA at http://www.cecill.info.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details.
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL-C license and that you accept its terms.
*/
package spoon.reflect.cu.position;

/**
* This interface represents the position of a type declaration in a source file.
*/
public interface TypeSourcePosition extends DeclarationSourcePosition {

int getBodyStart();

int getBodyEnd();

}
2 changes: 1 addition & 1 deletion src/main/java/spoon/reflect/declaration/CtElement.java
Expand Up @@ -18,7 +18,7 @@

import spoon.processing.FactoryAccessor;
import spoon.reflect.code.CtComment;
import spoon.reflect.cu.SourcePosition;
import spoon.reflect.cu.position.SourcePosition;
import spoon.reflect.reference.CtReference;
import spoon.reflect.reference.CtTypeReference;
import spoon.reflect.visitor.CtVisitable;
Expand Down
30 changes: 28 additions & 2 deletions src/main/java/spoon/reflect/factory/CoreFactory.java
Expand Up @@ -63,7 +63,10 @@
import spoon.reflect.code.CtVariableWrite;
import spoon.reflect.code.CtWhile;
import spoon.reflect.cu.CompilationUnit;
import spoon.reflect.cu.SourcePosition;
import spoon.reflect.cu.position.DeclarationSourcePosition;
import spoon.reflect.cu.position.MethodSourcePosition;
import spoon.reflect.cu.position.SourcePosition;
import spoon.reflect.cu.position.TypeSourcePosition;
import spoon.reflect.declaration.CtAnnotation;
import spoon.reflect.declaration.CtAnnotationMethod;
import spoon.reflect.declaration.CtAnnotationType;
Expand Down Expand Up @@ -376,7 +379,30 @@ public interface CoreFactory {
*/
SourcePosition createSourcePosition(
CompilationUnit compilationUnit,
int startDeclaration, int startSource, int end, int[] lineSeparatorPositions);
int startSource, int end, int[] lineSeparatorPositions);

DeclarationSourcePosition createDeclarationSourcePosition(
CompilationUnit compilationUnit,
int startSource, int end,
int modifierStart, int modifierEnd,
int declarationStart, int declarationEnd,
int[] lineSeparatorPositions);


MethodSourcePosition createMethodSourcePosition(
CompilationUnit compilationUnit,
int startSource, int end,
int modifierStart, int modifierEnd,
int declarationStart, int declarationEnd,
int bodyStart, int bodyEnd, int[] lineSeparatorPositions);

TypeSourcePosition createTypeSourcePosition(
CompilationUnit compilationUnit,
int startSource, int end,
int modifierStart, int modifierEnd,
int declarationStart, int declarationEnd,
int bodyStart, int bodyEnd,
int[] lineSeparatorPositions);

/**
* Creates a statement list.
Expand Down
Expand Up @@ -69,7 +69,7 @@
import spoon.reflect.code.CtVariableWrite;
import spoon.reflect.code.CtWhile;
import spoon.reflect.cu.CompilationUnit;
import spoon.reflect.cu.SourcePosition;
import spoon.reflect.cu.position.SourcePosition;
import spoon.reflect.declaration.CtAnnotation;
import spoon.reflect.declaration.CtAnnotationMethod;
import spoon.reflect.declaration.CtAnnotationType;
Expand Down
31 changes: 27 additions & 4 deletions src/main/java/spoon/support/DefaultCoreFactory.java
Expand Up @@ -63,7 +63,10 @@
import spoon.reflect.code.CtVariableWrite;
import spoon.reflect.code.CtWhile;
import spoon.reflect.cu.CompilationUnit;
import spoon.reflect.cu.SourcePosition;
import spoon.reflect.cu.position.DeclarationSourcePosition;
import spoon.reflect.cu.position.MethodSourcePosition;
import spoon.reflect.cu.position.SourcePosition;
import spoon.reflect.cu.position.TypeSourcePosition;
import spoon.reflect.declaration.CtAnnotation;
import spoon.reflect.declaration.CtAnnotationMethod;
import spoon.reflect.declaration.CtAnnotationType;
Expand Down Expand Up @@ -140,7 +143,10 @@
import spoon.support.reflect.code.CtVariableWriteImpl;
import spoon.support.reflect.code.CtWhileImpl;
import spoon.support.reflect.cu.CompilationUnitImpl;
import spoon.support.reflect.cu.SourcePositionImpl;
import spoon.support.reflect.cu.position.DeclarationSourcePositionImpl;
import spoon.support.reflect.cu.position.MethodSourcePositionImpl;
import spoon.support.reflect.cu.position.SourcePositionImpl;
import spoon.support.reflect.cu.position.TypeSourcePositionImpl;
import spoon.support.reflect.declaration.CtAnnotationImpl;
import spoon.support.reflect.declaration.CtAnnotationMethodImpl;
import spoon.support.reflect.declaration.CtAnnotationTypeImpl;
Expand Down Expand Up @@ -637,8 +643,25 @@ public void setMainFactory(Factory mainFactory) {
this.factory = mainFactory;
}

public SourcePosition createSourcePosition(CompilationUnit compilationUnit, int startDeclaration, int startSource, int end, int[] lineSeparatorPositions) {
return new SourcePositionImpl(compilationUnit, startDeclaration, startSource, end, lineSeparatorPositions);
public SourcePosition createSourcePosition(CompilationUnit compilationUnit, int startSource, int end, int[] lineSeparatorPositions) {
return new SourcePositionImpl(compilationUnit, startSource, end, lineSeparatorPositions);
}

public DeclarationSourcePosition createDeclarationSourcePosition(CompilationUnit compilationUnit, int startSource, int end, int modifierStart, int modifierEnd, int declarationStart, int declarationEnd, int[] lineSeparatorPositions) {
return new DeclarationSourcePositionImpl(compilationUnit, startSource, end, modifierStart, modifierEnd, declarationStart, declarationEnd, lineSeparatorPositions);
}

public MethodSourcePosition createMethodSourcePosition(CompilationUnit compilationUnit, int startSource, int end, int modifierStart, int modifierEnd, int declarationStart, int declarationEnd, int bodyStart, int bodyEnd, int[] lineSeparatorPositions) {
return new MethodSourcePositionImpl(compilationUnit,
startSource, end,
modifierStart, modifierEnd,
declarationStart, declarationEnd,
bodyStart, bodyEnd,
lineSeparatorPositions);
}

public TypeSourcePosition createTypeSourcePosition(CompilationUnit compilationUnit, int startSource, int end, int modifierStart, int modifierEnd, int declarationStart, int declarationEnd, int bodyStart, int bodyEnd, int[] lineSeparatorPositions) {
return new TypeSourcePositionImpl(compilationUnit, startSource, end, modifierStart, modifierEnd, declarationStart, declarationEnd, bodyStart, bodyEnd, lineSeparatorPositions);
}

public CompilationUnit createCompilationUnit() {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/spoon/support/StandardEnvironment.java
Expand Up @@ -29,7 +29,7 @@
import spoon.processing.ProcessingManager;
import spoon.processing.Processor;
import spoon.processing.ProcessorProperties;
import spoon.reflect.cu.SourcePosition;
import spoon.reflect.cu.position.SourcePosition;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtExecutable;
import spoon.reflect.declaration.CtType;
Expand Down
Expand Up @@ -32,7 +32,7 @@
import spoon.reflect.code.CtStatementList;
import spoon.reflect.code.CtSwitch;
import spoon.reflect.cu.CompilationUnit;
import spoon.reflect.cu.SourcePosition;
import spoon.reflect.cu.position.SourcePosition;
import spoon.reflect.declaration.CtAnonymousExecutable;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtConstructor;
Expand Down Expand Up @@ -123,7 +123,7 @@ private void buildComment(int[] positions) {
String commentContent = getCommentContent(start, end);

int[] lineSeparatorPositions = declarationUnit.compilationResult.lineSeparatorPositions;
SourcePosition sourcePosition = factory.Core().createSourcePosition(spoonUnit, start, start, end, lineSeparatorPositions);
SourcePosition sourcePosition = factory.Core().createSourcePosition(spoonUnit, start, end, lineSeparatorPositions);

// create the Spoon comment element
comment.setContent(commentContent);
Expand Down
Expand Up @@ -43,7 +43,6 @@
import spoon.reflect.code.CtLambda;
import spoon.reflect.code.CtTypeAccess;
import spoon.reflect.code.CtVariableAccess;
import spoon.reflect.cu.CompilationUnit;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtField;
import spoon.reflect.declaration.CtMethod;
Expand Down Expand Up @@ -285,6 +284,8 @@ <T> CtVariableAccess<T> createVariableAccess(QualifiedNameReference qualifiedNam
va = createVariableAccess(ref, isOtherBinding && fromAssignment);
}

ref.setPosition(jdtTreeBuilder.getPositionBuilder().buildPosition(sourceStart, sourceEnd));

if (qualifiedNameReference.otherBindings != null) {
int i = 0; //positions index;
va.setPosition(ref.getPosition());
Expand All @@ -310,10 +311,8 @@ <T> CtVariableAccess<T> createVariableAccess(QualifiedNameReference qualifiedNam
CtFieldAccess<T> other = createFieldAccess(//
jdtTreeBuilder.getReferencesBuilder().<T>getVariableReference(null, qualifiedNameReference.tokens[i]), va, isOtherBinding && fromAssignment);
//set source position of va;
CompilationUnit cu = jdtTreeBuilder.getFactory().CompilationUnit().create(new String(jdtTreeBuilder.getContextBuilder().compilationunitdeclaration.getFileName()));
sourceEnd = (int) (positions[i]);
final int[] lineSeparatorPositions = jdtTreeBuilder.getContextBuilder().compilationunitdeclaration.compilationResult.lineSeparatorPositions;
va.setPosition(jdtTreeBuilder.getFactory().Core().createSourcePosition(cu, sourceStart, sourceStart, sourceEnd, lineSeparatorPositions));
va.setPosition(jdtTreeBuilder.getPositionBuilder().buildPosition(sourceStart, sourceEnd));
va = other;
}
}
Expand Down

0 comments on commit 2b68632

Please sign in to comment.