static int getStartPosition(DocTree docTree, VisitorState state) { DocSourcePositions positions = JavacTrees.instance(state.context).getSourcePositions(); CompilationUnitTree compilationUnitTree = state.getPath().getCompilationUnit(); return (int) positions.getStartPosition(compilationUnitTree, getDocCommentTree(state), docTree); }
static int getEndPosition(DocTree docTree, VisitorState state) { DocSourcePositions positions = JavacTrees.instance(state.context).getSourcePositions(); CompilationUnitTree compilationUnitTree = state.getPath().getCompilationUnit(); return (int) positions.getEndPosition(compilationUnitTree, getDocCommentTree(state), docTree); }
static SuggestedFix replace(DocTree docTree, String replacement, VisitorState state) { DocSourcePositions positions = JavacTrees.instance(state.context).getSourcePositions(); CompilationUnitTree compilationUnitTree = state.getPath().getCompilationUnit(); int startPos = getStartPosition(docTree, state); int endPos = (int) positions.getEndPosition(compilationUnitTree, getDocCommentTree(state), docTree); return SuggestedFix.replace(startPos, endPos, replacement); }
@Override public Void visitParam(ParamTree paramTree, Void unused) { if (paramTree.isTypeParameter() && paramTree.getName().getName().contentEquals(name)) { DocSourcePositions positions = JavacTrees.instance(state.context).getSourcePositions(); CompilationUnitTree compilationUnitTree = state.getPath().getCompilationUnit(); int startPos = (int) positions.getStartPosition( compilationUnitTree, docCommentTree, paramTree.getName()); int endPos = (int) positions.getEndPosition( compilationUnitTree, docCommentTree, paramTree.getName()); fixBuilder.replace(startPos, endPos, typeVarReplacement); } return super.visitParam(paramTree, null); } },
public int endPos(DCTree oldT, DCDocComment doc) { DocSourcePositions sp = JavacTrees.instance(context).getSourcePositions(); return (int) sp.getEndPosition(null, doc, oldT); }
static int getStartPosition(DocTree docTree, VisitorState state) { DocSourcePositions positions = JavacTrees.instance(state.context).getSourcePositions(); CompilationUnitTree compilationUnitTree = state.getPath().getCompilationUnit(); return (int) positions.getStartPosition(compilationUnitTree, getDocCommentTree(state), docTree); }
public static String reformat(String text, CodeStyle style, int rightMargin) { StringBuilder sb = new StringBuilder(text); try { ClassPath empty = ClassPathSupport.createClassPath(new URL[0]); ClasspathInfo cpInfo = ClasspathInfo.create(JavaPlatformManager.getDefault().getDefaultPlatform().getBootstrapLibraries(), empty, empty); JavacTaskImpl javacTask = JavacParser.createJavacTask(cpInfo, null, null, null, null, null, null, null); com.sun.tools.javac.util.Context ctx = javacTask.getContext(); JavaCompiler.instance(ctx).genEndPos = true; CompilationUnitTree tree = javacTask.parse(FileObjects.memoryFileObject("","", text)).iterator().next(); //NOI18N SourcePositions sp = JavacTrees.instance(ctx).getSourcePositions(); TokenSequence<JavaTokenId> tokens = TokenHierarchy.create(text, JavaTokenId.language()).tokenSequence(JavaTokenId.language()); for (Diff diff : Pretty.reformat(text, tokens, new TreePath(tree), sp, style, rightMargin)) { int start = diff.getStartOffset(); int end = diff.getEndOffset(); sb.delete(start, end); String t = diff.getText(); if (t != null && t.length() > 0) { sb.insert(start, t); } } } catch (IOException ioe) { } return sb.toString(); }
private void adjustSpans(Iterable<? extends Tree> original, String code) { if (tree2Tag == null) { return; //nothing to copy } java.util.List<Tree> linearized = new LinkedList<Tree>(); if (!new Linearize().scan(original, linearized) != Boolean.TRUE) { return; //nothing to copy } try { ClassPath empty = ClassPathSupport.createClassPath(new URL[0]); ClasspathInfo cpInfo = ClasspathInfo.create(JavaPlatformManager.getDefault().getDefaultPlatform().getBootstrapLibraries(), empty, empty); JavacTaskImpl javacTask = JavacParser.createJavacTask(cpInfo, null, null, null, null, null, null, null); com.sun.tools.javac.util.Context ctx = javacTask.getContext(); JavaCompiler.instance(ctx).genEndPos = true; CompilationUnitTree tree = javacTask.parse(FileObjects.memoryFileObject("", "", code)).iterator().next(); //NOI18N SourcePositions sp = JavacTrees.instance(ctx).getSourcePositions(); ClassTree clazz = (ClassTree) tree.getTypeDecls().get(0); new CopyTags(tree, sp).scan(clazz.getModifiers().getAnnotations(), linearized); } catch (IOException ex) { Exceptions.printStackTrace(ex); } }
static SuggestedFix replace(DocTree docTree, String replacement, VisitorState state) { DocSourcePositions positions = JavacTrees.instance(state.context).getSourcePositions(); CompilationUnitTree compilationUnitTree = state.getPath().getCompilationUnit(); int startPos = getStartPosition(docTree, state); int endPos = (int) positions.getEndPosition(compilationUnitTree, getDocCommentTree(state), docTree); return SuggestedFix.replace(startPos, endPos, replacement); }
cut = javacTask.parse(FileObjects.memoryFileObject("", "", text)).iterator().next(); //NOI18N parsedTree = cut; sp = JavacTrees.instance(ctx).getSourcePositions(); } else { final SourcePositions[] psp = new SourcePositions[1];
final int origStartPos = (int) jt.getSourcePositions().getStartPosition(cu, orig.getBody()); final int origEndPos = (int) jt.getSourcePositions().getEndPosition(cu, orig.getBody()); if (origStartPos < 0) { LOGGER.log(Level.WARNING, "Javac returned startpos: {0} < 0", new Object[]{origStartPos}); //NOI18N return false; final int newEndPos = (int) jt.getSourcePositions().getEndPosition(cu, block); if (newEndPos != origStartPos + newBody.length()) { return false;
@Override public Void visitParam(ParamTree paramTree, Void unused) { if (paramTree.isTypeParameter() && paramTree.getName().getName().contentEquals(name)) { DocSourcePositions positions = JavacTrees.instance(state.context).getSourcePositions(); CompilationUnitTree compilationUnitTree = state.getPath().getCompilationUnit(); int startPos = (int) positions.getStartPosition( compilationUnitTree, docCommentTree, paramTree.getName()); int endPos = (int) positions.getEndPosition( compilationUnitTree, docCommentTree, paramTree.getName()); fixBuilder.replace(startPos, endPos, typeVarReplacement); } return super.visitParam(paramTree, null); } },