Skip to content

Commit

Permalink
feature: SourcePosition checks validity and displays origin source code
Browse files Browse the repository at this point in the history
  • Loading branch information
pvojtechovsky committed Apr 5, 2018
1 parent 4bddeb6 commit 4f8ce04
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public BodyHolderSourcePositionImpl(
modifierSourceStart, modifierSourceEnd,
declarationSourceStart, declarationSourceEnd,
lineSeparatorPositions);
checkArgsAreAscending(declarationSourceStart, modifierSourceStart, modifierSourceEnd + 1, sourceStart, sourceEnd + 1, bodyStart, bodyEnd + 1, declarationSourceEnd + 1);
this.bodyStart = bodyStart;
this.bodyEnd = bodyEnd;
}
Expand All @@ -58,4 +59,16 @@ public int getBodyStart() {
public int getBodyEnd() {
return bodyEnd;
}

/**
* @return origin source code of body
*/
public String getBodySourceFragment() {
return getFragment(getBodyStart(), getBodyEnd());
}

protected String getSourceInfo() {
return super.getSourceInfo()
+ "\nbody = " + getBodySourceFragment();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public DeclarationSourcePositionImpl(CompilationUnit compilationUnit, int source
super(compilationUnit,
sourceStart, sourceEnd,
lineSeparatorPositions);
checkArgsAreAscending(declarationSourceStart, modifierSourceStart, modifierSourceEnd + 1, sourceStart, sourceEnd + 1, declarationSourceEnd + 1);
this.modifierSourceStart = modifierSourceStart;
this.declarationSourceStart = declarationSourceStart;
this.declarationSourceEnd = declarationSourceEnd;
Expand Down Expand Up @@ -86,4 +87,25 @@ public int getModifierSourceEnd() {
public int getEndLine() {
return searchLineNumber(declarationSourceEnd);
}

/**
* @return origin source code of modifiers
*/
public String getModifierSourceFragment() {
return getFragment(getModifierSourceStart(), getModifierSourceEnd());
}

/**
* @return origin source code of `name`
*/
public String getNameSourceFragment() {
return getFragment(getNameStart(), getNameEnd());
}

protected String getSourceInfo() {
return super.getSourceInfo()
+ "\nmodifier = " + getModifierSourceFragment()
+ "\nname = " + getNameSourceFragment();
}

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

import spoon.SpoonException;
import spoon.reflect.cu.CompilationUnit;
import spoon.reflect.cu.SourcePosition;

Expand Down Expand Up @@ -91,10 +92,10 @@ private int searchColumnNumber(int position) {
}

/** The position of the first byte of this element (incl. documentation and modifiers) */
private int sourceStart = -1;
private final int sourceStart;

/** The position of the last byte of this element */
private int sourceEnd = -1;
private final int sourceEnd;

/** The line number of the start of the element, if appropriate (eg the method name).
* Computed lazily by {@link #getLine()}
Expand All @@ -112,6 +113,7 @@ private int searchColumnNumber(int position) {

public SourcePositionImpl(CompilationUnit compilationUnit, int sourceStart, int sourceEnd, int[] lineSeparatorPositions) {
super();
checkArgsAreAscending(sourceStart, sourceEnd + 1);
this.compilationUnit = compilationUnit;
if (compilationUnit != null) {
this.file = compilationUnit.getFile();
Expand Down Expand Up @@ -165,7 +167,8 @@ public String toString() {
return "(unknown file)";
}
int ln = getLine();
return (ln >= 1) ? "(" + getFile().getAbsolutePath().replace('\\', '/').replace("C:/", "/") + ":" + ln + ")" : getFile().getAbsolutePath().replace('\\', '/').replace("C:/", "/");
return ((ln >= 1) ? "(" + getFile().getAbsolutePath().replace('\\', '/').replace("C:/", "/") + ":" + ln + ")" : getFile().getAbsolutePath().replace('\\', '/').replace("C:/", "/"))
+ "\n" + getSourceInfo();
}

@Override
Expand Down Expand Up @@ -193,4 +196,35 @@ public CompilationUnit getCompilationUnit() {
return compilationUnit;
}

protected String getFragment(int start, int end) {
return "|" + start + ";" + end + "|" + getCompilationUnit().getOriginalSourceCode().substring(start, end + 1) + "|";
}

/**
* @return source code of this {@link SourcePosition}
*/
public String getSourceFragment() {
return getFragment(getSourceStart(), getSourceEnd());
}

protected String getSourceInfo() {
return getSourceFragment();
}

/**
* fails when `values` are not sorted ascending
* It is used to check whether start/end values of SourcePosition are consistent
*/
protected static void checkArgsAreAscending(int...values) {
int last = -1;
for (int value : values) {
if (value < 0) {
throw new SpoonException("SourcePosition value must not be negative");
}
if (last > value) {
throw new SpoonException("SourcePosition values must be ascending or equal");
}
last = value;
}
}
}

0 comments on commit 4f8ce04

Please sign in to comment.