List of usage examples for org.eclipse.jdt.core.dom ASTVisitor ASTVisitor
public ASTVisitor(boolean visitDocTags)
From source file:com.ashigeru.eclipse.util.jdt.internal.ui.handlers.InsertAssertionHandler.java
License:Apache License
private MethodDeclaration findSelectedExecutable(CompilationUnit ast, TextSelection selection) { assert ast != null; assert selection != null; final MethodDeclaration[] results = new MethodDeclaration[1]; final int carret = selection.getOffset(); ast.accept(new ASTVisitor(false) { @Override//from ww w.j a v a 2s .c o m public boolean visit(MethodDeclaration node) { if (node.getBody() == null) { return false; } int start = node.getStartPosition(); if (start < 0) { return true; } int end = start + node.getLength(); if (start <= carret && carret <= end) { results[0] = node; } return true; } }); return results[0]; }
From source file:com.facebook.nuclide.shim.EclipseJavaElementShim.java
License:Open Source License
@Override public ISourceRange getNameRange() throws JavaModelException { if (_sourceRange != null) { return _sourceRange; }//from w ww .j av a2s . co m // We need to determine the location in the source at which this type // is declared. Compute an AST from the source file, and fine the declaring // node. ICompilationUnit unit = getCompilationUnit(); ASTParser parser = ASTParser.newParser(AST.JLS8); Map<String, String> options = JavaCore.getOptions(); JavaCore.setComplianceOptions(JavaCore.VERSION_1_8, options); parser.setCompilerOptions(options); try { parser.setSource(unit.getSource().toCharArray()); } catch (Exception ex) { return new SourceRange(0, 0); } final String targetName = fixTargetName(_refType.name()); CompilationUnit ast = (CompilationUnit) parser.createAST(null); ast.accept(new ASTVisitor(false) { public boolean visit(TypeDeclaration node) { SimpleName name = getNodeDeclName(node); if (name.getFullyQualifiedName().equals(targetName)) { _sourceRange = new SourceRange(name.getStartPosition(), name.getLength()); // No need to continue processing, we've found what we're looking for. return false; } return true; } }); if (_sourceRange != null) { return _sourceRange; } // Can't return null or the source generator will die. return new SourceRange(0, 1); }
From source file:com.google.currysrc.processors.BaseTagElementNodeScanner.java
License:Apache License
@Override protected final void visit(final Reporter reporter, Javadoc javadoc, final ASTRewrite rewrite) { javadoc.accept(new ASTVisitor(true /* visitDocTags */) { @Override/*w ww . jav a2 s . c o m*/ public boolean visit(TagElement node) { return visitTagElement(reporter, rewrite, node); } }); }
From source file:com.google.currysrc.processors.ModifyQualifiedNames.java
License:Apache License
@Override public void process(Context context, CompilationUnit cu) { final ASTRewrite rewrite = context.rewrite(); ASTVisitor visitor = new ASTVisitor(true /* visitDocTags */) { @Override// ww w.j a v a2 s . c om public boolean visit(QualifiedName node) { Name qualifier = node.getQualifier(); if (qualifier != null) { String fullyQualifiedName = qualifier.getFullyQualifiedName(); if (fullyQualifiedName.startsWith(oldPrefix)) { String newQualifierString = newPrefix + fullyQualifiedName.substring(oldPrefix.length()); Name newQualifier = node.getAST().newName(newQualifierString); rewrite.replace(qualifier, newQualifier, null /* editGroup */); } } return false; } }; cu.accept(visitor); }
From source file:com.google.currysrc.processors.ModifyStringLiterals.java
License:Apache License
@Override public void process(Context context, CompilationUnit cu) { final ASTRewrite rewrite = context.rewrite(); ASTVisitor visitor = new ASTVisitor(false /* visitDocTags */) { @Override/* ww w .ja v a 2s .c o m*/ public boolean visit(StringLiteral node) { String literalValue = node.getLiteralValue(); // TODO Replace with Pattern if (literalValue.contains(oldString)) { String newLiteralValue = literalValue.replace(oldString, newString); StringLiteral newLiteral = node.getAST().newStringLiteral(); newLiteral.setLiteralValue(newLiteralValue); rewrite.replace(node, newLiteral, null /* editGorup */); } return false; } }; cu.accept(visitor); }
From source file:com.google.gwt.eclipse.core.refactoring.regionupdater.EquivalentNodeFinder.java
License:Open Source License
/** * Finds the equivalent AST node, or null if one could not be found. *///from w w w.j ava2 s . c o m public ASTNode find() { final ASTNode foundNode[] = new ASTNode[1]; newAncestorNode.accept(new ASTVisitor(true) { @Override public void preVisit(ASTNode visitedNode) { if (foundNode[0] != null) { // Already found a result, do not search further return; } if (!treesMatch(originalNode, visitedNode, ancestorNode, newAncestorNode, matcher)) { // Keep searching return; } foundNode[0] = visitedNode; // We are done } }); return foundNode[0]; }
From source file:com.t3.doccreator.MethodDefinition.java
License:Open Source License
private void parseDoc(Javadoc javadoc) { javadoc.accept(new ASTVisitor(true) { @Override//from w w w .j a v a 2 s . co m public boolean visit(TagElement node) { List<?> f = node.fragments(); if (node.getTagName() == null) for (Object o : f) { if (o instanceof TextElement) doc += ((TextElement) o).getText(); else if (o instanceof TagElement) { TagElement te = (TagElement) o; if (te.getTagName().equals("@link")) { if (te.fragments().size() == 2) doc += te.fragments().get(1).toString(); else if (te.fragments().size() == 1) doc += te.fragments().get(0).toString().substring(1); else throw new Error(); } else throw new Error(); } else throw new Error(); } else if (f.size() >= 2 && node.getTagName().equals("@param")) try { parameters.get(((SimpleName) f.get(0)).toString()).setComment(f.subList(1, f.size()) .stream().map(Object::toString).collect(Collectors.joining())); } catch (NullPointerException npe) { System.err.println("NPE for " + getName() + " -> " + ((SimpleName) f.get(0)).toString()); } else if (node.getTagName().equals("@return")) returnComment = f.stream().map(Object::toString).collect(Collectors.joining("\n")); else if (f.size() == 2 && node.getTagName().equals("@throws")) errorComment = ((TextElement) f.get(1)).getText(); else if (node.getTagName().equals("@author")) ; else if (node.getTagName().equals("@see")) ; else throw new Error(node.toString()); return false; } }); String[] lines = doc.split("\n"); doc = ""; for (String l : lines) { l = l.trim(); if (l.startsWith("*")) l = l.substring(1); if (l.startsWith("/**")) l = l.substring(3); if (l.startsWith("**/")) l = l.substring(3); if (l.endsWith("/")) l = l.substring(0, l.length() - 1); doc += l.trim() + "\n"; } }
From source file:edu.buffalo.cse.green.test.designpattern.SingletonTest.java
License:Open Source License
public void testSingletonPattern() throws JavaModelException { IType testSingleton = createClass("MyS", "" + "public MyS() {\n" + " // default constructor\n" + "}\n" + "\n" + "public MyS(Object o) {\n" + " // secondary constructor\n" + "}\n" + "\n" + "public void someMethod(Object o) {\n" + " // non-constructor method\n" + "}\n").getType(); List constructors = new ArrayList(); List nonConstructors = new ArrayList(); for (int x = 0; x < testSingleton.getMethods().length; x++) { assertTrue("All methods should be public to begin with", Flags.isPublic(testSingleton.getMethods()[x].getFlags())); // add the method to the appropriate list if (testSingleton.getMethods()[x].isConstructor()) { constructors.add(testSingleton.getMethods()[x]); } else {//www . j a v a2s. c o m nonConstructors.add(testSingleton.getMethods()[x]); } } // create the compilation unit CompilationUnit cu = RelationshipVisitor.getCompilationUnit(testSingleton); // run the recognizer on the generator - make sure there's no singleton SingletonRecognizer recognizer = new SingletonRecognizer(); cu.accept(recognizer); assertEquals("The type shouldn't be a singleton yet", 0, recognizer.getSingletonCount()); // run the generator SingletonGenerator generator = new SingletonGenerator(testSingleton); cu.accept(generator); // run the recognizer on the generator cu.accept(recognizer); assertEquals("The type should be a singleton, but it's not", 1, recognizer.getSingletonCount()); ASTVisitor typeAsserter = new ASTVisitor(false) { public boolean visit(MethodDeclaration node) { if (node.isConstructor()) { assertTrue("A constructor (" + node.getName() + ") was not private", Flags.isPrivate(node.getModifiers())); } else { assertTrue("A method (" + node.getName() + ") was private", !Flags.isPrivate(node.getModifiers())); } return true; } }; // make sure all constructors are public and other methods are not cu.accept(typeAsserter); }
From source file:edu.buffalo.cse.green.test.designpattern.SingletonTest.java
License:Open Source License
public void testSingletonPatternWithNoConstructor() throws JavaModelException { IType testSingleton = createClass("MyS").getType(); List constructors = new ArrayList(); List nonConstructors = new ArrayList(); // create the compilation unit CompilationUnit cu = RelationshipVisitor.getCompilationUnit(testSingleton); // run the recognizer on the generator - make sure there's no singleton SingletonRecognizer recognizer = new SingletonRecognizer(); cu.accept(recognizer);// w ww . ja v a 2 s. c o m assertEquals("The type shouldn't be a singleton yet", 0, recognizer.getSingletonCount()); // run the generator SingletonGenerator generator = new SingletonGenerator(testSingleton); cu.accept(generator); // run the recognizer on the generator cu.accept(recognizer); assertEquals("The type should be a singleton, but it's not", 1, recognizer.getSingletonCount()); ASTVisitor typeAsserter = new ASTVisitor(false) { public boolean visit(MethodDeclaration node) { if (node.isConstructor()) { assertTrue("A constructor (" + node.getName() + ") was not private", Flags.isPrivate(node.getModifiers())); } else { assertTrue("A method (" + node.getName() + ") was private", !Flags.isPrivate(node.getModifiers())); } return true; } }; // make sure all constructors are public and other methods are not cu.accept(typeAsserter); }
From source file:edu.buffalo.cse.green.test.designpattern.SingletonTest.java
License:Open Source License
public void testSingletonPatternWithOneConstructor() throws JavaModelException { IType testSingleton = createClass("MyS", "" + "public MyS() {\n" + " // default constructor\n" + "}\n") .getType();//from w w w. j av a 2 s. co m List constructors = new ArrayList(); List nonConstructors = new ArrayList(); for (int x = 0; x < testSingleton.getMethods().length; x++) { assertTrue("All methods should be public to begin with", Flags.isPublic(testSingleton.getMethods()[x].getFlags())); // add the method to the appropriate list if (testSingleton.getMethods()[x].isConstructor()) { constructors.add(testSingleton.getMethods()[x]); } else { nonConstructors.add(testSingleton.getMethods()[x]); } } // create the compilation unit CompilationUnit cu = RelationshipVisitor.getCompilationUnit(testSingleton); // run the recognizer on the generator - make sure there's no singleton SingletonRecognizer recognizer = new SingletonRecognizer(); cu.accept(recognizer); assertEquals("The type shouldn't be a singleton yet", 0, recognizer.getSingletonCount()); // run the generator SingletonGenerator generator = new SingletonGenerator(testSingleton); cu.accept(generator); // run the recognizer on the generator cu.accept(recognizer); assertEquals("The type should be a singleton, but it's not", 1, recognizer.getSingletonCount()); ASTVisitor typeAsserter = new ASTVisitor(false) { public boolean visit(MethodDeclaration node) { if (node.isConstructor()) { assertTrue("A constructor (" + node.getName() + ") was not private", Flags.isPrivate(node.getModifiers())); } else { assertTrue("A method (" + node.getName() + ") was private", !Flags.isPrivate(node.getModifiers())); } return true; } }; // make sure all constructors are public and other methods are not cu.accept(typeAsserter); }