本文整理汇总了Java中com.sun.source.tree.LineMap类的典型用法代码示例。如果您正苦于以下问题:Java LineMap类的具体用法?Java LineMap怎么用?Java LineMap使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
LineMap类属于com.sun.source.tree包,在下文中一共展示了LineMap类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: findLineOffset
import com.sun.source.tree.LineMap; //导入依赖的package包/类
/** return the offset of the first non-whitespace character on the line,
or -1 when the line does not exist
*/
private static int findLineOffset(LineMap lineMap, CharSequence text, int lineNumber) {
int offset;
try {
offset = (int) lineMap.getStartPosition(lineNumber);
int offset2 = (int) lineMap.getStartPosition(lineNumber + 1);
CharSequence lineStr = text.subSequence(offset, offset2);
for (int i = 0; i < lineStr.length(); i++) {
if (!Character.isWhitespace(lineStr.charAt(i))) {
offset += i;
break;
}
}
} catch (IndexOutOfBoundsException ioobex) {
return -1;
}
return offset;
}
示例2: run
import com.sun.source.tree.LineMap; //导入依赖的package包/类
public boolean run(DocletEnvironment root) {
DocTrees trees = root.getDocTrees();
SourcePositions sourcePositions = trees.getSourcePositions();
for (TypeElement klass : ElementFilter.typesIn(root.getIncludedElements())) {
for (ExecutableElement method : getMethods(klass)) {
if (method.getSimpleName().toString().equals("tabbedMethod")) {
TreePath path = trees.getPath(method);
CompilationUnitTree cu = path.getCompilationUnit();
long pos = sourcePositions.getStartPosition(cu, path.getLeaf());
LineMap lineMap = cu.getLineMap();
long columnNumber = lineMap.getColumnNumber(pos);
if (columnNumber == 9) {
System.out.println(columnNumber + ": OK!");
return true;
} else {
System.err.println(columnNumber + ": wrong tab expansion");
return false;
}
}
}
}
return false;
}
示例3: run
import com.sun.source.tree.LineMap; //导入依赖的package包/类
public boolean run(DocletEnvironment root) {
DocTrees trees = root.getDocTrees();
SourcePositions sourcePositions = trees.getSourcePositions();
for (TypeElement klass : root.getIncludedClasses()) {
for (ExecutableElement method : getMethods(klass)) {
if (method.getSimpleName().toString().equals("tabbedMethod")) {
TreePath path = trees.getPath(method);
CompilationUnitTree cu = path.getCompilationUnit();
long pos = sourcePositions.getStartPosition(cu, path.getLeaf());
LineMap lineMap = cu.getLineMap();
long columnNumber = lineMap.getColumnNumber(pos);
if (columnNumber == 9) {
System.out.println(columnNumber + ": OK!");
return true;
} else {
System.err.println(columnNumber + ": wrong tab expansion");
return false;
}
}
}
}
return false;
}
示例4: analyzeUnit
import com.sun.source.tree.LineMap; //导入依赖的package包/类
private static Source analyzeUnit(CompilationUnitTree cut, Set<File> errorFiles)
throws IOException {
LineMap lineMap = cut.getLineMap();
URI uri = cut.getSourceFile().toUri();
File file = new File(uri.normalize());
String path = file.getCanonicalPath();
Source source = new Source(path, lineMap);
if (errorFiles.contains(file)) {
source.hasCompileError = true;
}
SourceContext context = new SourceContext(source);
analyzeCompilationUnitTree(context, cut);
source.resetLineRange();
return source;
}
示例5: matchTry
import com.sun.source.tree.LineMap; //导入依赖的package包/类
@Override
public Description matchTry (TryTree tree, VisitorState state) {
List<? extends CatchTree> catchList = tree.getCatches();
if (catchList == null || catchList.size() == 0) {
// TODO: this try block does not have a catch, we should further check the
// finally block!
return Description.NO_MATCH;
}
CatchTree lastCatch = catchList.get(tree.getCatches().size() - 1);
if (overcatch(lastCatch, state)) {
if (abortInCatch(lastCatch, state)) {
LineMap lineMap = state.getPath().getCompilationUnit().getLineMap();
/* System.out.println("****** warning starts **************");
System.out.println("WARNING: abort in overcatch: "
+ state.getPath().getCompilationUnit().getSourceFile().getName()
+ ":" + lineMap.getLineNumber(TreeInfo.getStartPos((JCTree) lastCatch)));
System.out.println(state.getPath().getLeaf());
System.out.println("****** warning ends **************");
System.out.println(); */
return describeMatch(lastCatch, NO_FIX);
}
}
return Description.NO_MATCH;
}
示例6: matchTry
import com.sun.source.tree.LineMap; //导入依赖的package包/类
@Override
public Description matchTry (TryTree tree, VisitorState state) {
if (badEmptyCatchBlock(tree, state)) {
// If it has finally block, assume it's OK
BlockTree bt = tree.getFinallyBlock();
if (bt == null || bt.getStatements().size() == 0) {
CatchTree lastCatch = tree.getCatches().get(tree.getCatches().size() - 1);
LineMap lineMap = state.getPath().getCompilationUnit().getLineMap();
/* System.out.println("****** warning starts **************");
System.out.println("WARNING: empty catch: "
+ state.getPath().getCompilationUnit().getSourceFile().getName()
// + ":" + state.getEndPosition((JCTree) tree)
+ ":" + lineMap.getLineNumber(TreeInfo.getStartPos((JCTree) lastCatch)));
System.out.println(state.getPath().getLeaf());
System.out.println();
System.out.println("****** warning ends **************"); */
return describeMatch(lastCatch, NO_FIX);
}
}
return Description.NO_MATCH;
}
示例7: getLineNumber
import com.sun.source.tree.LineMap; //导入依赖的package包/类
public long getLineNumber(Element e) {
TreePath path = getTreePath(e);
if (path == null) { // maybe null if synthesized
TypeElement encl = getEnclosingTypeElement(e);
path = getTreePath(encl);
}
CompilationUnitTree cu = path.getCompilationUnit();
LineMap lineMap = cu.getLineMap();
DocSourcePositions spos = docTrees.getSourcePositions();
long pos = spos.getStartPosition(cu, path.getLeaf());
return lineMap.getLineNumber(pos);
}
示例8: testStatement
import com.sun.source.tree.LineMap; //导入依赖的package包/类
private boolean testStatement(StringWriter writer, SourcePositions sp, String text, CompilationUnitTree cut, Tree statement) {
if (statement == null) {
return true;
}
int start = (int) sp.getStartPosition(cut, statement);
int end = (int) sp.getEndPosition(cut, statement);
char ch = text.charAt(end - 1);
SourceCodeAnalysis.Completeness expected = COMPLETE;
LineMap lineMap = cut.getLineMap();
int row = (int) lineMap.getLineNumber(start);
int column = (int) lineMap.getColumnNumber(start);
switch (ch) {
case ',':
case ';':
expected = (statement instanceof ExpressionStatementTree)
? COMPLETE
: COMPLETE_WITH_SEMI;
--end;
break;
case '}':
break;
default:
writer.write(String.format("Unexpected end: row %d, column %d: '%c' -- %s\n",
row, column, ch, text.substring(start, end)));
return true;
}
String unit = text.substring(start, end);
SourceCodeAnalysis.CompletionInfo ci = getAnalysis().analyzeCompletion(unit);
if (ci.completeness() != expected) {
if (expected == COMPLETE_WITH_SEMI && (ci.completeness() == CONSIDERED_INCOMPLETE || ci.completeness() == EMPTY)) {
writer.write(String.format("Empty statement: row %d, column %d: -- %s\n",
start, end, unit));
} else {
writer.write(String.format("Expected %s got %s: '%s' row %d, column %d: -- %s\n",
expected, ci.completeness(), unit, row, column, unit));
return false;
}
}
return true;
}
示例9: renderSource
import com.sun.source.tree.LineMap; //导入依赖的package包/类
public String renderSource(TreePath path, List<? extends Tree> trees, String source) {
CompilationUnitTree unit = path.getCompilationUnit();
int from = (int) docTrees.getSourcePositions().getStartPosition(unit, trees.get(0));
int to = (int) docTrees.getSourcePositions().getEndPosition(unit, trees.get(trees.size() - 1));
// Correct boundaries
while (from > 1 && source.charAt(from - 1) != '\n') {
from--;
}
while (to < source.length() && source.charAt(to) != '\n') {
to++;
}
String block = source.substring(from, to);
// Determine margin
int blockMargin = Integer.MAX_VALUE;
LineMap lineMap = unit.getLineMap();
for (Tree statement : trees) {
int statementStart = (int) docTrees.getSourcePositions().getStartPosition(unit, statement);
int lineStart = statementStart;
while (lineMap.getLineNumber(statementStart) == lineMap.getLineNumber(lineStart - 1)) {
lineStart--;
}
blockMargin = Math.min(blockMargin, statementStart - lineStart);
}
// Crop the fragment
StringBuilder fragment = new StringBuilder();
for (Iterator<String> sc = new Scanner(block).useDelimiter("\n"); sc.hasNext(); ) {
String line = sc.next();
int margin = Math.min(blockMargin, line.length());
line = line.substring(margin);
fragment.append(line);
if (sc.hasNext()) {
fragment.append('\n');
}
}
return fragment.toString();
}
示例10: checkMembers
import com.sun.source.tree.LineMap; //导入依赖的package包/类
@TriggerTreeKind(Kind.CLASS)
public static ErrorDescription checkMembers(final HintContext context) {
for (Diagnostic<?> d : context.getInfo().getDiagnostics()) {
if (Hacks.isSyntaxError(d)) {
return null;
}
}
Source source = context.getInfo().getSnapshot().getSource();
try {
ModificationResult result = ModificationResult.runModificationTask(Collections.singleton(source), new UserTask() {
@Override
public void run(ResultIterator resultIterator) throws Exception {
WorkingCopy copy = WorkingCopy.get(resultIterator.getParserResult());
copy.toPhase(Phase.RESOLVED);
doOrganizeMembers(copy, context.getPath());
}
});
List<? extends Difference> diffs = result.getDifferences(source.getFileObject());
if (diffs != null && !diffs.isEmpty() && !checkGuarded(context.getInfo().getDocument(), diffs)) {
Fix fix = new OrganizeMembersFix(context.getInfo(), context.getPath()).toEditorFix();
SourcePositions sp = context.getInfo().getTrees().getSourcePositions();
int offset = diffs.get(0).getStartPosition().getOffset();
LineMap lm = context.getInfo().getCompilationUnit().getLineMap();
long lno = lm.getLineNumber(offset);
if (lno >= 1) {
offset = (int)lm.getStartPosition(lno);
}
CompilationUnitTree cut = context.getPath().getCompilationUnit();
ClassTree clazz = (ClassTree) context.getPath().getLeaf();
for (Tree member : clazz.getMembers()) {
if (context.getInfo().getTreeUtilities().isSynthetic(new TreePath(context.getPath(), member))) continue;
if (sp.getStartPosition(cut, member) >= offset) {
return ErrorDescriptionFactory.forTree(context, member, NbBundle.getMessage(OrganizeMembers.class, "MSG_OragnizeMembers"), fix); //NOI18N
}
}
return ErrorDescriptionFactory.forTree(context, clazz, NbBundle.getMessage(OrganizeMembers.class, "MSG_OragnizeMembers"), fix); //NOI18N
}
} catch (Exception ex) {
Exceptions.printStackTrace(ex);
}
return null;
}
示例11: fillSwitch
import com.sun.source.tree.LineMap; //导入依赖的package包/类
@Hint(displayName = "#DN_org.netbeans.modules.java.hints.suggestions.Tiny.fillSwitch", description = "#DESC_org.netbeans.modules.java.hints.suggestions.Tiny.fillSwitch", category="suggestions", hintKind=Kind.ACTION, severity=Severity.HINT, customizerProvider=CustomizerProviderImpl.class)
@TriggerPattern(value="switch ($expression) { case $cases$; }",
[email protected](variable="$expression", type="java.lang.Enum"))
public static ErrorDescription fillSwitch(HintContext ctx) {
int caret = ctx.getCaretLocation();
SwitchTree st = (SwitchTree) ctx.getPath().getLeaf();
int switchStart = (int) ctx.getInfo().getTrees().getSourcePositions().getStartPosition(ctx.getPath().getCompilationUnit(), st);
LineMap lm = ctx.getPath().getCompilationUnit().getLineMap();
if (lm.getLineNumber(caret) != lm.getLineNumber(switchStart)) return null;
TreePath expression = ctx.getVariables().get("$expression");
Element possibleEnumElement = ctx.getInfo().getTypes().asElement(ctx.getInfo().getTrees().getTypeMirror(expression));
if (possibleEnumElement == null || !possibleEnumElement.getKind().isClass()) return null;
TypeElement enumType = (TypeElement) possibleEnumElement;
List<VariableElement> enumConstants = new ArrayList<VariableElement>(enumType.getEnclosedElements().size());
for (Element e : enumType.getEnclosedElements()) {
if (e.getKind() == ElementKind.ENUM_CONSTANT) {
enumConstants.add((VariableElement) e);
}
}
boolean hasDefault = false;
for (TreePath casePath : ctx.getMultiVariables().get("$cases$")) {
CaseTree ct = (CaseTree) casePath.getLeaf();
if (ct.getExpression() == null) {
hasDefault = true;
} else {
enumConstants.remove(ctx.getInfo().getTrees().getElement(new TreePath(casePath, ct.getExpression())));
}
}
boolean generateDefault = ctx.getPreferences().getBoolean(KEY_DEFAULT_ENABLED, DEF_DEFAULT_ENABLED);
if (enumConstants.isEmpty() && (hasDefault || !generateDefault)) return null;
List<String> names = new ArrayList<String>(enumConstants.size());
for (VariableElement constant : enumConstants) {
names.add(constant.getSimpleName().toString());
}
String defaultTemplate = generateDefault ? ctx.getPreferences().get(KEY_DEFAULT_SNIPPET, DEF_DEFAULT_SNIPPET) : null;
String errMessage = enumConstants.isEmpty() ? "ERR_Tiny.fillSwitchDefault" : !hasDefault && generateDefault ? "ERR_Tiny.fillSwitchCasesAndDefault" : "ERR_Tiny.fillSwitchCases";
String fixMessage = enumConstants.isEmpty() ? "FIX_Tiny.fillSwitchDefault" : !hasDefault && generateDefault ? "FIX_Tiny.fillSwitchCasesAndDefault" : "FIX_Tiny.fillSwitchCases";
return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), NbBundle.getMessage(Tiny.class, errMessage), new AddSwitchCasesImpl(ctx.getInfo(), ctx.getPath(), fixMessage, names, defaultTemplate).toEditorFix());
}
示例12: testStatement
import com.sun.source.tree.LineMap; //导入依赖的package包/类
private boolean testStatement(StringWriter writer, SourcePositions sp, String text, CompilationUnitTree cut, Tree statement) {
if (statement == null) {
return true;
}
int start = (int) sp.getStartPosition(cut, statement);
int end = (int) sp.getEndPosition(cut, statement);
char ch = text.charAt(end - 1);
SourceCodeAnalysis.Completeness expected = COMPLETE;
LineMap lineMap = cut.getLineMap();
int row = (int) lineMap.getLineNumber(start);
int column = (int) lineMap.getColumnNumber(start);
switch (ch) {
case ',':
case ';':
expected = (statement instanceof ExpressionStatementTree)
? COMPLETE
: COMPLETE_WITH_SEMI;
--end;
break;
case '}':
break;
default:
writer.write(String.format("Unexpected end: row %d, column %d: '%c' -- %s\n",
row, column, ch, text.substring(start, end)));
return true;
}
String unit = text.substring(start, end);
SourceCodeAnalysis.CompletionInfo ci = getAnalysis().analyzeCompletion(unit);
if (ci.completeness != expected) {
if (expected == COMPLETE_WITH_SEMI && (ci.completeness == CONSIDERED_INCOMPLETE || ci.completeness == EMPTY)) {
writer.write(String.format("Empty statement: row %d, column %d: -- %s\n",
start, end, unit));
} else {
String oops = unit.substring(max(0, ci.unitEndPos - 10), ci.unitEndPos) + "|||" +
unit.substring(ci.unitEndPos, min(unit.length(), ci.unitEndPos + 10));
writer.write(String.format("Expected %s got %s: '%s' row %d, column %d: -- %s\n",
expected, ci.completeness, oops, row, column, unit));
return false;
}
}
return true;
}
示例13: Source
import com.sun.source.tree.LineMap; //导入依赖的package包/类
public Source(String filePath, LineMap lineMap) {
this(filePath);
this.lineMap = lineMap;
}
示例14: getLineMap
import com.sun.source.tree.LineMap; //导入依赖的package包/类
@Override
public LineMap getLineMap() {
return null;
}
示例15: line
import com.sun.source.tree.LineMap; //导入依赖的package包/类
public static long line(long position) {
LineMap lm = getState().compilationUnit.getLineMap();
return (lm != null) ? lm.getLineNumber(position) : 0;
}