List of usage examples for org.eclipse.jdt.core.dom AST JLS2
int JLS2
To view the source code for org.eclipse.jdt.core.dom AST JLS2.
Click Source Link
From source file:ast.StructureParse.java
public void setCode(JTextPane textCode) { ASTParser parser = ASTParser.newParser(AST.JLS2); parser.setSource(textCode.getText().toCharArray()); //parser.setSource("/*abc*/".toCharArray()); parser.setKind(ASTParser.K_COMPILATION_UNIT); final CompilationUnit cu = (CompilationUnit) parser.createAST(null); _package = new DefaultMutableTreeNode("root"); codeTree.setRootVisible(false);/*from w ww . ja va 2s. c o m*/ cu.accept(new ASTVisitor() { Set names = new HashSet(); /* * Package declaration */ @Override public boolean visit(PackageDeclaration node) { _package = new DefaultMutableTreeNode(node.getName().toString()); codeTree.setRootVisible(true); System.out.println("Package : " + node.getName().toString()); return false; } /* * Class Declaration. * sub node from package */ @Override public boolean visit(TypeDeclaration node) { String type; if (node.isInterface()) type = "interface "; else type = "class "; _class = new DefaultMutableTreeNode(type + node.getName()); System.out.println(type + node.getName()); _package.add(_class); String superClass = node.getSuperclass() + ""; if (!(superClass.equals("null") || (superClass.equals("")))) { _class.add(new DefaultMutableTreeNode("extends " + superClass)); System.out.println("Extends : " + superClass); } for (Object getInterface : node.superInterfaces()) { _class.add(new DefaultMutableTreeNode("implements " + getInterface)); System.out.println("Implements : " + getInterface); } return true; } /* * method declaration * sub node from class */ @Override public boolean visit(MethodDeclaration node) { _method = new DefaultMutableTreeNode(node.getName()); _class.add(_method); System.out.println("MethodDeclaration : " + node.getName()); return true; } /* * field declaration * sub node from class */ // @Override // public boolean visit(FieldDeclaration node) // { // _field=new DefaultMutableTreeNode(node.toString()); // _class.add(_field); // Object o = node.fragments().get(0); // if(o instanceof VariableDeclarationFragment){ // _method.add(new DefaultMutableTreeNode(o.toString())); // } // return false; // } /* * Variable declaration fragment AST node type * used in field declarations * local variable declarations * sub node from method */ @Override public boolean visit(VariableDeclarationFragment node) { if (node.getParent() instanceof FieldDeclaration) { FieldDeclaration declaration = ((FieldDeclaration) node.getParent()); _class.add(new DefaultMutableTreeNode(declaration.getType().toString())); System.out.println("FieldDeclaration : " + declaration.getType().toString()); } else { _method.add(new DefaultMutableTreeNode(node.toString())); System.out.println("VariableDeclarationFragment : " + node.toString()); } return false; // do not continue to avoid usage info } @Override public boolean visit(VariableDeclarationStatement node) { _method.add(new DefaultMutableTreeNode(node.toString())); System.out.println("VariableDeclarationStatement : " + node.toString()); return false; } /* * Simple Name * A simple name is an identifier other than a keyword, boolean literal ("true", "false") or null literal ("null"). */ @Override public boolean visit(SimpleName node) { if (this.names.contains(node.getIdentifier())) { _method.add(new DefaultMutableTreeNode(node.toString())); System.out.println("SimpleNode Identifier : " + node.toString()); } return false; } /* * Alternate constructor invocation (calling constructor) statement AST node type * For JLS2: */ @Override public boolean visit(SuperConstructorInvocation node) { _constructorCall = new DefaultMutableTreeNode(node); _method.add(_constructorCall); System.out.println("SuperConstructorInvocation : " + node); return false; } /* * method call */ @Override public boolean visit(MethodInvocation node) { _methodCall = new DefaultMutableTreeNode(node); _method.add(_methodCall); System.out.println("MethodInvocation : " + node); return false; } @Override public boolean visit(SuperMethodInvocation node) { _methodCall = new DefaultMutableTreeNode(node); _method.add(_methodCall); System.out.println("SuperMethodInvocation : " + node); return false; } @Override public boolean visit(ReturnStatement node) { _method.add(new DefaultMutableTreeNode(node.toString())); System.out.println("ReturnStatement : " + node.toString()); return false; } @Override public boolean visit(IfStatement node) { String elseExist = ""; _if = new DefaultMutableTreeNode("if (" + node.getExpression() + ")"); _method.add(_if); System.out.println("if : " + node.getExpression()); enclose(node.getThenStatement().toString(), _if); elseExist = node.getElseStatement() + ""; if (!(elseExist.equals("") || elseExist.equals("null"))) { _else = new DefaultMutableTreeNode("else"); _method.add(_else); System.out.println("else : " + node.getElseStatement().toString()); enclose(node.getElseStatement().toString(), _else); } return false; } @Override public boolean visit(EnhancedForStatement node) { _for = new DefaultMutableTreeNode( "for (" + node.getParameter() + " : " + node.getExpression() + ")"); _method.add(_for); System.out.println("EnchancedFor : (" + node.getParameter() + " : " + node.getExpression() + ")"); enclose(node.getBody() + "", _for); return false; } @Override public boolean visit(ForStatement node) { /* * because initial may more than 1. */ String initial = ""; for (int i = 0; i < node.initializers().size(); i++) { initial += node.initializers().get(i); if (node.initializers().size() - 1 != i) initial += ", "; } /* * because increment may more than 1 */ String inc = ""; for (int i = 0; i < node.updaters().size(); i++) { inc += node.updaters().get(i); if (node.updaters().size() - 1 != i) inc += ", "; } _for = new DefaultMutableTreeNode( "for (" + initial + "; " + node.getExpression() + "; " + inc + ")"); _method.add(_for); System.out.println("for (" + initial + "; " + node.getExpression() + "; " + inc + ")"); enclose(node.getBody().toString(), _for); return false; } @Override public boolean visit(WhileStatement node) { _while = new DefaultMutableTreeNode("while " + node.getExpression()); _method.add(_while); System.out.println("WhileStatement : " + node.getExpression()); enclose(node.getBody().toString(), _while); return false; } @Override public boolean visit(DoStatement node) { _do = new DefaultMutableTreeNode("do"); _method.add(_do); System.out.println("Do"); enclose(node.getBody().toString(), _do); _while = new DefaultMutableTreeNode("while(" + node.getExpression() + ")"); _method.add(_while); System.out.println("WhileDo : " + node.getExpression()); return false; } @Override public boolean visit(TryStatement node) { String ada = ""; _try = new DefaultMutableTreeNode("try"); _method.add(_try); System.out.println("try"); enclose(node.getBody().toString(), _try); ada = node.getFinally() + ""; if (!(ada.equals("") || ada.equals("null"))) { _final = new DefaultMutableTreeNode("finally"); _method.add(_final); System.out.println("finall"); enclose(node.getFinally().toString(), _final); } return false; } @Override public boolean visit(CatchClause node) { _catch = new DefaultMutableTreeNode("catch (" + node.getException() + ")"); _method.add(_catch); System.out.println("catch : " + node.getException()); enclose(node.getBody().toString(), _catch); return false; } @Override public boolean visit(Assignment node) { _assignment = new DefaultMutableTreeNode(node.toString()); _method.add(_assignment); System.out.println("Assignment : " + node.toString()); return false; } @Override public boolean visit(ConstructorInvocation node) { _constructorCall = new DefaultMutableTreeNode(node.toString()); _method.add(_constructorCall); System.out.println(node.toString()); return false; } @Override public boolean visit(AnonymousClassDeclaration node) { _constructorCall = new DefaultMutableTreeNode(node.toString()); _method.add(_constructorCall); System.out.println("AnonymousClassDeclaration : " + node.toString()); return false; } @Override public boolean visit(ArrayAccess node) { _class = new DefaultMutableTreeNode(node.toString()); _method.add(_class); System.out.println("AbstrackTypeDeclaration : " + node.toString()); return false; } @Override public boolean visit(ArrayCreation node) { _array = new DefaultMutableTreeNode(node.toString()); _method.add(_array); System.out.println("ArrayCreation : " + node.toString()); return false; } @Override public boolean visit(ArrayInitializer node) { _array = new DefaultMutableTreeNode(node.toString()); System.out.println("ArrayInitialize : " + node.toString()); _method.add(_array); return false; } @Override public boolean visit(AssertStatement node) { _statement = new DefaultMutableTreeNode(node.toString()); System.out.println("AssertStatement : " + node.toString()); _method.add(_statement); return false; } @Override public boolean visit(ContinueStatement node) { _statement = new DefaultMutableTreeNode(node.toString()); System.out.println("ContinueStatement : " + node.toString()); _method.add(_statement); return false; } @Override public boolean visit(SwitchStatement node) { _switch = new DefaultMutableTreeNode("switch (" + node.getExpression() + ")"); System.out.println("switch (" + node.getExpression() + ")"); _method.add(_switch); List getStatement = node.statements(); for (Object st : getStatement) { Matcher _caseMatch = Pattern.compile("^case\\s+.+\\:").matcher(st.toString()); if (_caseMatch.find()) { _case = new DefaultMutableTreeNode(_caseMatch.group()); _switch.add(_case); } enclose(st.toString(), _case); Matcher _breakMatch = Pattern.compile("^break\\s*.*;").matcher(st.toString()); if (_breakMatch.find()) { _break = new DefaultMutableTreeNode(_breakMatch.group()); _case.add(_break); } } return false; } @Override public boolean visit(ClassInstanceCreation node) { _constructorCall = new DefaultMutableTreeNode(node.toString()); System.out.println("ClassInstanceCreation : " + node.toString()); _method.add(_constructorCall); return false; } }); // model = new DefaultTreeModel(_package); // model.reload(); // codeTree.setModel(model); codeTree.setModel(new DefaultTreeModel(_package) { public void reload(TreeNode node) { if (node != null) { fireTreeStructureChanged(this, getPathToRoot(node), null, null); } } }); codeTree.setCellRenderer(new TreeRender()); // ((DefaultTreeModel)codeTree.getModel()).reload(); for (int i = 0; i < codeTree.getRowCount(); i++) codeTree.expandRow(i); }
From source file:ast.StructureParse.java
private void enclose(String body, DefaultMutableTreeNode parentNode) { ASTParser parser = ASTParser.newParser(AST.JLS2); /*//from w w w . ja v a 2 s . c om * make parse result into OPP structure, because AST wanna be that. */ String setClass = "class fo{\nvoid foo(){ \n" + body + "\n}\n}"; parser.setSource(setClass.toCharArray()); //parser.setSource("/*abc*/".toCharArray()); parser.setKind(ASTParser.K_COMPILATION_UNIT); //ASTNode node = parser.createAST(null); parser.setResolveBindings(true); try { final CompilationUnit cu = (CompilationUnit) parser.createAST(null); cu.accept(new ASTVisitor() { Set names2 = new HashSet(); @Override public boolean visit(SimpleName node) { if (this.names2.contains(node.getIdentifier())) { parentNode.add(new DefaultMutableTreeNode(node.toString())); System.out.println("SimpleNode : " + node.toString()); } return true; } @Override public boolean visit(EnhancedForStatement node) { _for = new DefaultMutableTreeNode( "for (" + node.getParameter() + " : " + node.getExpression() + ")"); parentNode.add(_for); System.out.println("for (" + node.getParameter() + " : " + node.getExpression() + ")"); enclose(node.getBody().toString(), _for); return false; } @Override public boolean visit(ForStatement node) { /* * because initial may more than 1. */ String initial = ""; for (int i = 0; i < node.initializers().size(); i++) { initial += node.initializers().get(i); if (node.initializers().size() - 1 != i) initial += ", "; } /* * because increment may more than 1 */ String inc = ""; for (int i = 0; i < node.updaters().size(); i++) { inc += node.updaters().get(i); if (node.updaters().size() - 1 != i) inc += ", "; } _for = new DefaultMutableTreeNode( "for (" + initial + "; " + node.getExpression() + "; " + inc + ")"); parentNode.add(_for); System.out.println("for (" + initial + "; " + node.getExpression() + "; " + inc + ")"); enclose(node.getBody().toString(), _for); return false; } @Override public boolean visit(IfStatement node) { String elseExist = ""; _if = new DefaultMutableTreeNode("if (" + node.getExpression() + ")"); System.out.println("if (+" + node.getExpression() + ")"); parentNode.add(_if); enclose(node.getThenStatement().toString(), _if); elseExist = node.getElseStatement() + ""; if (!(elseExist.equals("") || (elseExist.equals("null")))) { _else = new DefaultMutableTreeNode("else"); System.out.println("else"); parentNode.add(_else); enclose(node.getElseStatement().toString(), _else); } return false; } @Override public boolean visit(VariableDeclarationFragment node) { if (node.getParent() instanceof FieldDeclaration) { FieldDeclaration declaration = ((FieldDeclaration) node.getParent()); _class.add(new DefaultMutableTreeNode(declaration.getType().toString())); } else { System.out.println("VariableDeclarationFragment : " + node.toString()); parentNode.add(new DefaultMutableTreeNode(node.toString())); } return false; // do not continue to avoid usage info } @Override public boolean visit(ReturnStatement node) { parentNode.add(new DefaultMutableTreeNode(node.toString())); System.out.println("Return : " + node.toString()); return false; } @Override public boolean visit(SuperConstructorInvocation node) { _constructorCall = new DefaultMutableTreeNode(node); parentNode.add(_constructorCall); System.out.println("SuperConstructorInvocation : " + node); return false; } @Override public boolean visit(MethodInvocation node) { _methodCall = new DefaultMutableTreeNode(node); parentNode.add(_methodCall); System.out.println("MethodInvocation : " + node); return true; } @Override public boolean visit(SuperMethodInvocation node) { _methodCall = new DefaultMutableTreeNode(node); parentNode.add(_methodCall); System.out.println("SuperMethodInvocation : " + node); return false; } @Override public boolean visit(WhileStatement node) { _while = new DefaultMutableTreeNode("while " + node.getExpression()); parentNode.add(_while); System.out.println("WhileStatement : " + node.getExpression()); enclose(node.getBody().toString(), _while); return false; } @Override public boolean visit(DoStatement node) { _do = new DefaultMutableTreeNode("do"); parentNode.add(_do); System.out.println("do"); enclose(node.getBody().toString(), _do); _while = new DefaultMutableTreeNode("while(" + node.getExpression() + ")"); parentNode.add(_while); return false; } @Override public boolean visit(TryStatement node) { String ada = ""; _try = new DefaultMutableTreeNode("try"); parentNode.add(_try); System.out.println("try"); enclose(node.getBody().toString(), _try); ada = node.getFinally() + ""; if (!(ada.equals("") || (ada.equals("null")))) { _final = new DefaultMutableTreeNode("finally"); parentNode.add(_final); System.out.println("finally"); enclose(node.getFinally().toString(), _final); } return false; } @Override public boolean visit(CatchClause node) { _catch = new DefaultMutableTreeNode("catch (" + node.getException() + ")"); parentNode.add(_catch); System.out.println("catch : " + node.getException()); enclose(node.getBody().toString(), _catch); return false; } @Override public boolean visit(Assignment node) { _assignment = new DefaultMutableTreeNode(node.toString()); parentNode.add(_assignment); System.out.println("Assignment : " + node.toString()); return false; } @Override public boolean visit(ConstructorInvocation node) { _constructorCall = new DefaultMutableTreeNode(node.toString()); parentNode.add(_constructorCall); System.out.println(node.toString()); return false; } @Override public boolean visit(AnonymousClassDeclaration node) { _constructorCall = new DefaultMutableTreeNode(node.toString()); parentNode.add(_constructorCall); System.out.println("AnonymousClassDeclaration : " + node.toString()); return false; } @Override public boolean visit(ArrayAccess node) { _class = new DefaultMutableTreeNode(node.toString()); parentNode.add(_class); System.out.println("AbstrackTypeDeclaration : " + node.toString()); return false; } @Override public boolean visit(ArrayCreation node) { _array = new DefaultMutableTreeNode(node.toString()); _method.add(_array); System.out.println("ArrayCreation : " + node.toString()); return false; } @Override public boolean visit(ArrayInitializer node) { _array = new DefaultMutableTreeNode(node.toString()); System.out.println("ArrayInitialize : " + node.toString()); parentNode.add(_array); return false; } @Override public boolean visit(AssertStatement node) { _statement = new DefaultMutableTreeNode(node.toString()); System.out.println("AssertStatement : " + node.toString()); parentNode.add(_statement); return false; } @Override public boolean visit(ContinueStatement node) { _statement = new DefaultMutableTreeNode(node.toString()); System.out.println("ContinueStatement : " + node.toString()); parentNode.add(_statement); return false; } @Override public boolean visit(SwitchStatement node) { _switch = new DefaultMutableTreeNode("switch (" + node.getExpression() + ")"); System.out.println("switch (" + node.getExpression() + ")"); parentNode.add(_switch); List getStatement = node.statements(); for (Object st : getStatement) { Matcher _caseMatch = Pattern.compile("^case\\s+.+\\:").matcher(st.toString()); if (_caseMatch.find()) { _case = new DefaultMutableTreeNode(_caseMatch.group()); _switch.add(_case); } enclose(st.toString(), _case); Matcher _breakMatch = Pattern.compile("^break\\s*.*;").matcher(st.toString()); if (_breakMatch.find()) { _break = new DefaultMutableTreeNode(_breakMatch.group()); _case.add(_break); } } return false; } @Override public boolean visit(ClassInstanceCreation node) { _constructorCall = new DefaultMutableTreeNode(node.toString()); System.out.println("ClassInstanceCreation : " + node.toString()); parentNode.add(_constructorCall); return false; } }); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:com.github.parzonka.ccms.engine.CompilationUnitSorter.java
License:Open Source License
/** * @deprecated marking deprecated as it is using deprecated code *///from w ww. j a v a 2 s . c o m @Deprecated private static void checkASTLevel(int level) { switch (level) { case AST.JLS2: case AST.JLS3: break; default: throw new IllegalArgumentException(); } }
From source file:com.github.parzonka.ccms.engine.CompilationUnitSorter.java
License:Open Source License
/** * Reorders the declarations in the given compilation unit according to JLS2 * rules. The caller is responsible for arranging in advance that the given * compilation unit is a working copy, and for saving the changes * afterwards./*from w w w .j ava 2 s . c om*/ * <p> * <b>Note:</b> Reordering the members within a type declaration might be * more than a cosmetic change and could have potentially serious * repercussions. Firstly, the order in which the fields of a type are * initialized is significant in the Java language; reordering fields and * initializers may result in compilation errors or change the execution * behavior of the code. Secondly, reordering a class's members may affect * how its instances are serialized. This operation should therefore be used * with caution and due concern for potential negative side effects. * </p> * <p> * The optional <code>positions</code> array contains a non-decreasing * ordered list of character-based source positions within the compilation * unit's source code string. Upon return from this method, the positions in * the array reflect the corresponding new locations in the modified source * code string. Note that this operation modifies the given array in place. * </p> * <p> * The <code>compare</code> method of the given comparator is passed pairs * of JLS2 AST body declarations (subclasses of <code>BodyDeclaration</code> * ) representing body declarations at the same level. The comparator is * called on body declarations of nested classes, including anonymous and * local classes, but always at the same level. Clients need to provide a * comparator implementation (there is no standard comparator). The * <code>RELATIVE_ORDER</code> property attached to these AST nodes afforts * the comparator a way to preserve the original relative order. * </p> * <p> * The body declarations passed as parameters to the comparator always carry * at least the following minimal signature information: <br> * <table border="1" width="80%" cellpadding="5"> * <tr> * <td width="20%"><code>TypeDeclaration</code></td> * <td width="50%"><code>modifiers, isInterface, name, superclass, * superInterfaces<br> * RELATIVE_ORDER property</code></td> * </tr> * <tr> * <td width="20%"><code>FieldDeclaration</code></td> * <td width="50%"><code>modifiers, type, fragments * (VariableDeclarationFragments * with name only)<br> * RELATIVE_ORDER property</code></td> * </tr> * <tr> * <td width="20%"><code>MethodDeclaration</code></td> * <td width="50%"><code>modifiers, isConstructor, returnType, name, * parameters * (SingleVariableDeclarations with name and type only), * thrownExceptions<br> * RELATIVE_ORDER property</code></td> * </tr> * <tr> * <td width="20%"><code>Initializer</code></td> * <td width="50%"><code>modifiers<br> * RELATIVE_ORDER property</code></td> * </tr> * </table> * Clients should not rely on the AST nodes being properly parented or on * having source range information. (Future releases may provide options for * requesting additional information like source positions, full ASTs, * non-recursive sorting, etc.) * </p> * * @param compilationUnit * the given compilation unit, which must be a working copy * @param positions * an array of source positions to map, or <code>null</code> if * none. If supplied, the positions must character-based source * positions within the original source code for the given * compilation unit, arranged in non-decreasing order. The array * is updated in place when this method returns to reflect the * corresponding source positions in the permuted source code * string (but not necessarily any longer in non-decreasing * order). * @param comparator * the comparator capable of ordering * <code>BodyDeclaration</code>s; this comparator is passed AST * nodes from a JLS2 AST * @param options * bitwise-or of option flags; <code>0</code> for default * behavior (reserved for future growth) * @param monitor * the progress monitor to notify, or <code>null</code> if none * @exception JavaModelException * if the compilation unit could not be sorted. Reasons * include: * <ul> * <li>The given compilation unit does not exist * (ELEMENT_DOES_NOT_EXIST)</li> * <li>The given compilation unit is not a working copy * (INVALID_ELEMENT_TYPES)</li> * <li>A <code>CoreException</code> occurred while accessing * the underlying resource * </ul> * @exception IllegalArgumentException * if the given compilation unit is null or if the given * comparator is null. * @see org.eclipse.jdt.core.dom.BodyDeclaration * @see #RELATIVE_ORDER * @deprecated Clients should port their code to use the new JLS3 AST API * and call * {@link #sort(int, ICompilationUnit, int[], Comparator, int, IProgressMonitor) * CompilationUnitSorter.sort(AST.JLS3, compilationUnit, * positions, comparator, options, monitor)} instead of using * this method. */ @Deprecated public static void sort(ICompilationUnit compilationUnit, int[] positions, Comparator comparator, int options, IProgressMonitor monitor) throws JavaModelException { sort(AST.JLS2, compilationUnit, positions, comparator, options, monitor); }
From source file:com.iw.plugins.spindle.ui.wizards.factories.ClassFactory.java
License:Mozilla Public License
/** * Uses the New Java file template from the code template page to generate a * compilation unit with the given type content. * /* w ww .j av a 2 s .com*/ * @param cu * The new created compilation unit * @param typeContent * The content of the type, including signature and type body. * @param lineDelimiter * The line delimiter to be used. * @return String Returns the result of evaluating the new file template with * the given type content. * @throws CoreException * @since 2.1 */ protected String constructCUContent(ICompilationUnit cu, String typeContent, String lineDelimiter) throws CoreException { String typeComment = getTypeComment(cu, lineDelimiter); IPackageFragment pack = (IPackageFragment) cu.getParent(); String content = CodeGeneration.getCompilationUnitContent(cu, typeComment, typeContent, lineDelimiter); if (content != null) { ASTParser parser = ASTParser.newParser(AST.JLS2); parser.setSource(content.toCharArray()); CompilationUnit unit = (CompilationUnit) parser.createAST(null); if ((pack.isDefaultPackage() || unit.getPackage() != null) && !unit.types().isEmpty()) { return content; } } StringBuffer buf = new StringBuffer(); if (!pack.isDefaultPackage()) { buf.append("package ").append(pack.getElementName()).append(';'); //$NON-NLS-1$ } buf.append(lineDelimiter).append(lineDelimiter); if (typeComment != null) { buf.append(typeComment).append(lineDelimiter); } buf.append(typeContent); return buf.toString(); }
From source file:com.iw.plugins.spindle.ui.wizards.factories.ClassFactory.java
License:Mozilla Public License
private void removeUnusedImports(ICompilationUnit cu, Set addedTypes, boolean needsSave) throws CoreException { ASTParser parser = ASTParser.newParser(AST.JLS2); parser.setSource(cu);/*from w w w . ja va2s . c om*/ parser.setResolveBindings(true); CompilationUnit root = (CompilationUnit) parser.createAST(null); IProblem[] problems = root.getProblems(); ArrayList res = new ArrayList(); for (int i = 0; i < problems.length; i++) { int id = problems[i].getID(); if (id == IProblem.UnusedImport || id == IProblem.ImportNotVisible) { // not visibles hide unused -> remove both String imp = problems[i].getArguments()[0]; res.add(imp); } } if (!res.isEmpty()) { ImportsManager imports = new ImportsManager(cu, addedTypes); for (int i = 0; i < res.size(); i++) { String curr = (String) res.get(i); imports.removeImport(curr); } imports.create(needsSave, null); } }
From source file:com.liferay.ide.project.ui.migration.CUCache.java
License:Open Source License
@SuppressWarnings("unchecked") private static CompilationUnit createCompilationUnit(String unitName, char[] javaSource) { ASTParser parser = ASTParser.newParser(AST.JLS2); Map<String, String> options = JavaCore.getOptions(); JavaCore.setComplianceOptions(JavaCore.VERSION_1_6, options); parser.setCompilerOptions(options);/*w ww.j a v a2s. c o m*/ // setUnitName for resolve bindings parser.setUnitName(unitName); String[] sources = { "" }; String[] classpath = { "" }; // setEnvironment for resolve bindings even if the args is empty parser.setEnvironment(classpath, sources, new String[] { "UTF-8" }, true); parser.setResolveBindings(true); parser.setStatementsRecovery(true); parser.setBindingsRecovery(true); parser.setSource(javaSource); parser.setIgnoreMethodBodies(false); return (CompilationUnit) parser.createAST(null); }
From source file:javadoctest.internal.DocTestExtractor.java
License:Open Source License
public List<ExtractedDocTest> extractExamples(String source) { // Fast exit for classes without <pre in their docs. A small contribution to cutting down read time if (!source.contains("<pre")) { return Collections.EMPTY_LIST; }/*ww w. j av a2s. com*/ ASTParser parser = ASTParser.newParser(AST.JLS2); parser.setResolveBindings(false); parser.setStatementsRecovery(false); parser.setBindingsRecovery(false); parser.setSource(source.toCharArray()); parser.setIgnoreMethodBodies(false); ASTNode ast = parser.createAST(null); final AtomicReference<String> className = new AtomicReference<>(); final AtomicReference<String> classPkg = new AtomicReference<>(); final List<String> imports = new LinkedList<>(); final List<ExtractedDocTest> examples = new ArrayList<>(); ASTVisitor visitor = new ASTVisitor() { @Override public boolean visit(ImportDeclaration node) { // We collect imports as well, imports.add(node.getName().toString()); return true; } @Override public boolean visit(PackageDeclaration node) { classPkg.set(node.getName().toString()); return true; } @Override public boolean visit(FieldDeclaration node) { examples.addAll(extractExamples(imports, classPkg.get(), className.get(), node.fragments().get(0).toString(), node)); return true; } @Override public boolean visit(MethodDeclaration node) { examples.addAll( extractExamples(imports, classPkg.get(), className.get(), node.getName().toString(), node)); return true; } @Override public boolean visit(TypeDeclaration node) { if (className.get() == null) { className.set(node.getName().getIdentifier().toString()); } examples.addAll(extractExamples(imports, classPkg.get(), className.get(), node.getName().toString() + " classdoc", node)); return true; } }; ast.accept(visitor); return examples; }
From source file:lang.java.jdt.internal.JdtAstToRascalAstConverter.java
License:Open Source License
private java.util.Map.Entry<IValueList, IValueList> parseExtendedModifiers(BodyDeclaration node) { if (node.getAST().apiLevel() == AST.JLS2) { return new java.util.AbstractMap.SimpleEntry<IValueList, IValueList>( parseModifiers(node.getModifiers()), new IValueList(values)); } else {//from w w w. j a va 2 s. c o m return parseExtendedModifiers(node.modifiers()); } }
From source file:lang.java.jdt.internal.JdtAstToRascalAstConverter.java
License:Open Source License
public boolean visit(ClassInstanceCreation node) { IValue expression = node.getExpression() == null ? null : visitChild(node.getExpression()); IValue type = null;/*from www . j av a 2 s. c o m*/ IValueList genericTypes = new IValueList(values); if (node.getAST().apiLevel() == AST.JLS2) { type = visitChild(node.getName()); } else { type = visitChild(node.getType()); if (!node.typeArguments().isEmpty()) { for (Iterator it = node.typeArguments().iterator(); it.hasNext();) { Type t = (Type) it.next(); genericTypes.add(visitChild(t)); } } } IValueList arguments = new IValueList(values); for (Iterator it = node.arguments().iterator(); it.hasNext();) { Expression e = (Expression) it.next(); arguments.add(visitChild(e)); } IValue anonymousClassDeclaration = node.getAnonymousClassDeclaration() == null ? null : visitChild(node.getAnonymousClassDeclaration()); ownValue = constructRascalNode(node, optional(expression), type, genericTypes.asList(), arguments.asList(), optional(anonymousClassDeclaration)); return false; }