|
47 | 47 |
|
48 | 48 | import com.sun.tools.javac.file.JavacFileManager; |
49 | 49 | import com.sun.tools.javac.main.Option; |
| 50 | +import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; |
| 51 | +import com.sun.tools.javac.tree.JCTree.JCMethodDecl; |
| 52 | +import com.sun.tools.javac.tree.TreeInfo; |
| 53 | +import com.sun.tools.javac.tree.TreeMaker; |
| 54 | +import com.sun.tools.javac.tree.TreeScanner; |
50 | 55 | import com.sun.tools.javac.util.Context; |
| 56 | +import com.sun.tools.javac.util.Names; |
51 | 57 | import com.sun.tools.javac.util.Options; |
52 | 58 |
|
53 | 59 | public class JavacUtils { |
@@ -409,4 +415,25 @@ public static boolean isTest(IJavaProject project, org.eclipse.jdt.internal.comp |
409 | 415 | return true; |
410 | 416 | } |
411 | 417 | } |
| 418 | + |
| 419 | + /// Removes non-relevant content (eg other method blocks) for given focal position |
| 420 | + public static void trimUnvisibleContent(JCCompilationUnit u, int focalPoint, Context context) { |
| 421 | + TreeMaker treeMaker = TreeMaker.instance(context); |
| 422 | + u.accept(new TreeScanner() { |
| 423 | + @Override |
| 424 | + public void visitMethodDef(JCMethodDecl decl) { |
| 425 | + if (decl.getBody() != null && |
| 426 | + !decl.getBody().getStatements().isEmpty() && |
| 427 | + !(decl.getStartPosition() <= focalPoint && |
| 428 | + decl.getStartPosition() + TreeInfo.getEndPos(decl, u.endPositions) >= focalPoint)) { |
| 429 | + var throwNewRuntimeExceptionOutOfFocalPositionScope = |
| 430 | + treeMaker.Throw( |
| 431 | + treeMaker.NewClass(null, null, |
| 432 | + treeMaker.Ident(Names.instance(context).fromString(RuntimeException.class.getSimpleName())), |
| 433 | + com.sun.tools.javac.util.List.of(treeMaker.Literal("Out of focalPosition scope")), null)); //$NON-NLS-1$ |
| 434 | + decl.body.stats = com.sun.tools.javac.util.List.of(throwNewRuntimeExceptionOutOfFocalPositionScope); |
| 435 | + } |
| 436 | + } |
| 437 | + }); |
| 438 | + } |
412 | 439 | } |
0 commit comments