Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(position): CompoundSourcePosition extends SourcePosition #2115

Merged
merged 2 commits into from
Jun 28, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Copyright (C) 2006-2018 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;

import spoon.reflect.cu.SourcePosition;

/**
* This interface represents the position of a program element in a source file.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The javadoc here is exactly the same that for DeclarationSourcePosition. Could you edit one of those Javadoc to explicit the differences between the interfaces?

*/
public interface CompoundSourcePosition extends SourcePosition {

int getNameStart();

int getNameEnd();

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,13 @@
*/
package spoon.reflect.cu.position;

import spoon.reflect.cu.SourcePosition;

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

int getModifierSourceStart();

int getModifierSourceEnd();

int getNameStart();

int getNameEnd();

}
10 changes: 10 additions & 0 deletions src/main/java/spoon/reflect/factory/CoreFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
import spoon.reflect.cu.CompilationUnit;
import spoon.reflect.cu.SourcePosition;
import spoon.reflect.cu.position.BodyHolderSourcePosition;
import spoon.reflect.cu.position.CompoundSourcePosition;
import spoon.reflect.cu.position.DeclarationSourcePosition;
import spoon.reflect.declaration.CtAnnotation;
import spoon.reflect.declaration.CtAnnotationMethod;
Expand Down Expand Up @@ -403,6 +404,15 @@ SourcePosition createSourcePosition(
/** Creates a source position that points to the given compilation unit */
SourcePosition createPartialSourcePosition(CompilationUnit compilationUnit);

/**
* Creates a compound source position.
*/
CompoundSourcePosition createCompoundSourcePosition(
CompilationUnit compilationUnit,
int startSource, int end,
int declarationStart, int declarationEnd,
int[] lineSeparatorPositions);

/**
* Creates a declaration source position.
*/
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/spoon/support/DefaultCoreFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
import spoon.reflect.cu.CompilationUnit;
import spoon.reflect.cu.SourcePosition;
import spoon.reflect.cu.position.BodyHolderSourcePosition;
import spoon.reflect.cu.position.CompoundSourcePosition;
import spoon.reflect.cu.position.DeclarationSourcePosition;
import spoon.reflect.declaration.CtAnnotation;
import spoon.reflect.declaration.CtAnnotationMethod;
Expand Down Expand Up @@ -154,6 +155,7 @@
import spoon.support.reflect.code.CtWhileImpl;
import spoon.support.reflect.cu.CompilationUnitImpl;
import spoon.support.reflect.cu.position.BodyHolderSourcePositionImpl;
import spoon.support.reflect.cu.position.CompoundSourcePositionImpl;
import spoon.support.reflect.cu.position.DeclarationSourcePositionImpl;
import spoon.support.reflect.cu.position.SourcePositionImpl;
import spoon.support.reflect.declaration.CtAnnotationImpl;
Expand Down Expand Up @@ -697,6 +699,11 @@ public SourcePosition createPartialSourcePosition(CompilationUnit compilationUni
return ((CompilationUnitImpl) compilationUnit).getOrCreatePartialSourcePosition();
}

@Override
public CompoundSourcePosition createCompoundSourcePosition(CompilationUnit compilationUnit, int startSource, int end, int declarationStart, int declarationEnd, int[] lineSeparatorPositions) {
return new CompoundSourcePositionImpl(compilationUnit, startSource, end, declarationStart, declarationEnd, lineSeparatorPositions);
}

@Override
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);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* Copyright (C) 2006-2018 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.support.reflect.cu.position;

import spoon.reflect.cu.CompilationUnit;
import spoon.reflect.cu.position.CompoundSourcePosition;

import java.io.Serializable;

/**
* This class represents the position of a Java program element in a source
* file.
*/
public class CompoundSourcePositionImpl extends SourcePositionImpl
implements CompoundSourcePosition, Serializable {

private static final long serialVersionUID = 1L;
private int declarationSourceStart;
private int declarationSourceEnd;

public CompoundSourcePositionImpl(CompilationUnit compilationUnit, int sourceStart, int sourceEnd,
int declarationSourceStart, int declarationSourceEnd,
int[] lineSeparatorPositions) {
super(compilationUnit,
sourceStart, sourceEnd,
lineSeparatorPositions);
checkArgsAreAscending(declarationSourceStart, sourceStart, sourceEnd + 1, declarationSourceEnd + 1);
this.declarationSourceStart = declarationSourceStart;
this.declarationSourceEnd = declarationSourceEnd;
}

@Override
public int getSourceEnd() {
return declarationSourceEnd;
}

@Override
public int getSourceStart() {
return declarationSourceStart;
}

@Override
public int getNameStart() {
return super.getSourceStart();
}

@Override
public int getNameEnd() {
return super.getSourceEnd();
}

public int getEndLine() {
return searchLineNumber(declarationSourceEnd);
}

@Override
public String getSourceDetails() {
return super.getSourceDetails()
+ "\nname = " + getFragment(getNameStart(), getNameEnd());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,56 +25,32 @@
* This class represents the position of a Java program element in a source
* file.
*/
public class DeclarationSourcePositionImpl extends SourcePositionImpl
public class DeclarationSourcePositionImpl extends CompoundSourcePositionImpl
implements DeclarationSourcePosition, Serializable {

private static final long serialVersionUID = 1L;
private int modifierSourceEnd;
private int modifierSourceStart;
private int declarationSourceStart;
private int declarationSourceEnd;

public DeclarationSourcePositionImpl(CompilationUnit compilationUnit, int sourceStart, int sourceEnd,
int modifierSourceStart, int modifierSourceEnd, int declarationSourceStart, int declarationSourceEnd,
int[] lineSeparatorPositions) {
super(compilationUnit,
sourceStart, sourceEnd,
sourceStart, sourceEnd, declarationSourceStart, declarationSourceEnd,
lineSeparatorPositions);
checkArgsAreAscending(declarationSourceStart, modifierSourceStart, modifierSourceEnd + 1, sourceStart, sourceEnd + 1, declarationSourceEnd + 1);
this.modifierSourceStart = modifierSourceStart;
this.declarationSourceStart = declarationSourceStart;
this.declarationSourceEnd = declarationSourceEnd;
if (this.modifierSourceStart == 0) {
this.modifierSourceStart = declarationSourceStart;
}
this.modifierSourceEnd = modifierSourceEnd;
}

@Override
public int getSourceEnd() {
return declarationSourceEnd;
}

@Override
public int getSourceStart() {
return declarationSourceStart;
}

@Override
public int getModifierSourceStart() {
return modifierSourceStart;
}

@Override
public int getNameStart() {
return super.getSourceStart();
}

@Override
public int getNameEnd() {
return super.getSourceEnd();
}

public void setModifierSourceEnd(int modifierSourceEnd) {
this.modifierSourceEnd = modifierSourceEnd;
}
Expand All @@ -84,13 +60,9 @@ public int getModifierSourceEnd() {
return modifierSourceEnd;
}

public int getEndLine() {
return searchLineNumber(declarationSourceEnd);
}

@Override
public String getSourceDetails() {
return super.getSourceDetails()
return getFragment(getSourceStart(), getSourceEnd())
+ "\nmodifier = " + getFragment(getModifierSourceStart(), getModifierSourceEnd())
+ "\nname = " + getFragment(getNameStart(), getNameEnd());
}
Expand Down