CompilationUnit cu = ast.newCompilationUnit(); PackageDeclaration p1 = ast.newPackageDeclaration(); p1.setName(ast.newSimpleName("foo")); cu.setPackage(p1); ImportDeclaration id = ast.newImportDeclaration(); id.setName(ast.newName(new String[] { "java", "util", "Set" })); cu.imports().add(id); TypeDeclaration td = ast.newTypeDeclaration(); td.setName(ast.newSimpleName("Foo")); TypeParameter tp = ast.newTypeParameter(); tp.setName(ast.newSimpleName("X")); td.typeParameters().add(tp); cu.types().add(td); MethodDeclaration md = ast.newMethodDeclaration(); td.bodyDeclarations().add(md); Block block = ast.newBlock(); md.setBody(block); MethodInvocation mi = ast.newMethodInvocation(); mi.setName(ast.newSimpleName("x")); ExpressionStatement e = ast.newExpressionStatement(mi); block.statements().add(e);
public static org.eclipse.jdt.core.dom.AbstractTypeDeclaration findTypeDeclaration(IType searchType, List<?> nodes) { for (Object object : nodes) { if (object instanceof org.eclipse.jdt.core.dom.AbstractTypeDeclaration) { org.eclipse.jdt.core.dom.AbstractTypeDeclaration typeDeclaration = (org.eclipse.jdt.core.dom.AbstractTypeDeclaration) object; if (typeDeclaration.getName().toString().equals(searchType.getElementName())) return typeDeclaration; } } return null; }
if (annotation.isSingleMemberAnnotation()) { org.eclipse.jdt.core.dom.SingleMemberAnnotation smAnn = (org.eclipse.jdt.core.dom.SingleMemberAnnotation) annotation; values.add(smAnn.getValue().toString()); } else if (annotation.isNormalAnnotation()) { org.eclipse.jdt.core.dom.NormalAnnotation normalAnn = (org.eclipse.jdt.core.dom.NormalAnnotation) annotation; for (Object value : normalAnn.values()) values.add(value.toString()); signature.append("@").append(annotation.resolveTypeBinding().getQualifiedName()); if (!values.isEmpty()) { signature.append("(");
for (Object modifier : declaration.modifiers()) { if (modifier instanceof org.eclipse.jdt.core.dom.Annotation) { org.eclipse.jdt.core.dom.Annotation annotation = (org.eclipse.jdt.core.dom.Annotation)modifier; String qualifiedAnnotationName = annotation.resolveTypeBinding().getQualifiedName(); if (!"java.lang.Override".equals(qualifiedAnnotationName) && !"java.lang.SuppressWarnings".equals(qualifiedAnnotationName)) annotations.add(annotation); .append(declaration.getReturnType2().toString()) .append(" ").append(declaration.getName().getFullyQualifiedName()) .append("("); for (Object parameter : declaration.parameters()) { if (!first) signature.append(", "); first = false;
Document document = new Document(source); ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setSource(document.get().toCharArray()); CompilationUnit unit = (CompilationUnit)parser.createAST(null); unit.recordModifications(); System.out.println(i.getName().getFullyQualifiedName()); ImportDeclaration id = ast.newImportDeclaration(); String classToImport = "path.to.some.class"; id.setName(ast.newName(classToImport.split("\\."))); if (type.getNodeType() == ASTNode.TYPE_DECLARATION) { List<BodyDeclaration> bodies = type.bodyDeclarations(); for (BodyDeclaration body : bodies) { if (body.getNodeType() == ASTNode.METHOD_DECLARATION) { MethodDeclaration method = (MethodDeclaration)body; System.out.println("name: " + method.getName().getFullyQualifiedName());
private void extractJavaDoc(Class<?> clazz) { ASTParser parser = ASTParser.newParser(AST.JLS4); parser.setSource(sourceCodeFetcher.get(clazz).toCharArray()); parser.setKind(ASTParser.K_COMPILATION_UNIT); final CompilationUnit cu = (CompilationUnit) parser.createAST(null); cu.accept(new ASTVisitor() { MethodDeclaration currentMethod = null;
public boolean visit(Javadoc javaDoc) { if (currentMethod != null) { SMethod method = getSMethod(currentMethod.getName().getIdentifier()); if (method == null) { LOGGER.error("Method " + currentMethod.getName().getIdentifier() + " not found in class"); } else { for (Object tag : javaDoc.tags()) { if (tag instanceof TagElement) { TagElement tagElement = (TagElement) tag; String tagName = tagElement.getTagName() == null ? null : tagElement.getTagName().trim(); if ("@param".equals(tagName)) { SParameter parameter = null; for (int i = 0; i < tagElement.fragments().size(); i++) { Object fragment = tagElement.fragments().get(i); if (i == 0 && fragment instanceof SimpleName) { parameter = method.getParameter(((SimpleName) fragment).getIdentifier()); } else if (i == 1 && parameter != null && fragment instanceof TextElement) { parameter.setDoc(((TextElement) fragment).getText()); return super.visit(javaDoc);
@Override ASTNode clone0(AST target) { IfStatement result = new IfStatement(target); result.setSourceRange(getStartPosition(), getLength()); result.copyLeadingComment(this); result.setExpression((Expression) getExpression().clone(target)); result.setThenStatement( (Statement) getThenStatement().clone(target)); result.setElseStatement( (Statement) ASTNode.copySubtree(target, getElseStatement())); return result; }
org.eclipse.jdt.core.dom.AbstractTypeDeclaration typeDeclaration = findTypeDeclaration(rootType, cuUnit.types()); while (!typeStack.isEmpty() && typeDeclaration != null) { typeDeclaration = findTypeDeclaration(typeStack.pop(), typeDeclaration.bodyDeclarations()); for (Object declaration : typeDeclaration.bodyDeclarations()) { if (declaration instanceof org.eclipse.jdt.core.dom.MethodDeclaration) { org.eclipse.jdt.core.dom.MethodDeclaration methodDeclaration = (org.eclipse.jdt.core.dom.MethodDeclaration) declaration; if (methodDeclaration.getName().toString().equals(methodName)) { return methodDeclaration;
@Override ASTNode clone0(AST target) { EnhancedForStatement result = new EnhancedForStatement(target); result.setSourceRange(getStartPosition(), getLength()); result.copyLeadingComment(this); result.setParameter((SingleVariableDeclaration) getParameter().clone(target)); result.setExpression((Expression) getExpression().clone(target)); result.setBody( (Statement) ASTNode.copySubtree(target, getBody())); return result; }
int treeSize() { return memSize() + (this.optionalDocComment == null ? 0 : getJavadoc().treeSize()) + this.modifiers.listSize() + (this.memberName == null ? 0 : getName().treeSize()) + (this.memberType == null ? 0 : getType().treeSize()) + (this.optionalDefaultValue == null ? 0 : getDefault().treeSize()); } }
private boolean isEnhancedForStatementVariable(Statement statement, SimpleName name) { if (statement instanceof EnhancedForStatement) { EnhancedForStatement forStatement= (EnhancedForStatement) statement; SingleVariableDeclaration param= forStatement.getParameter(); return param.getType() == name.getParent(); // strange recovery, see https://bugs.eclipse.org/180456 } return false; }
private static int getReturnFlag(ReturnStatement node) { Expression expression = node.getExpression(); if (expression == null || expression.resolveTypeBinding() == node.getAST().resolveWellKnownType("void")) { return VOID_RETURN; } return VALUE_RETURN; } }
RecoveredTypeBinding(BindingResolver resolver, VariableDeclaration variableDeclaration) { this.variableDeclaration = variableDeclaration; this.resolver = resolver; this.currentType = getType(); this.dimensions = variableDeclaration.getExtraDimensions(); if (this.currentType.isArrayType()) { this.dimensions += ((ArrayType) this.currentType).getDimensions(); } }
private String extractFullText(TagElement tagElement) { StringBuilder builder = new StringBuilder(); for (Object fragment : tagElement.fragments()) { if (fragment instanceof TextElement) { TextElement textElement = (TextElement) fragment; builder.append(textElement.getText() + " "); } } return builder.toString().trim(); }
public static boolean isGenerated(org.eclipse.jdt.core.dom.ASTNode node) { boolean result = false; try { result = ((Boolean)node.getClass().getField("$isGenerated").get(node)).booleanValue(); if (!result && node.getParent() != null && node.getParent() instanceof org.eclipse.jdt.core.dom.QualifiedName) { result = isGenerated(node.getParent()); } } catch (Exception e) { // better to assume it isn't generated } return result; }
@Override public boolean visit(MethodDeclaration node) { currentMethod = node; return super.visit(node); }
@Override public void endVisit(MethodDeclaration node) { currentMethod = null; super.endVisit(node); } });
int treeSize() { return memSize() + (this.optionalDocComment == null ? 0 : getJavadoc().treeSize()) + this.modifiers.listSize() + (this.memberName == null ? 0 : getName().treeSize()) + (this.memberType == null ? 0 : getType().treeSize()) + (this.optionalDefaultValue == null ? 0 : getDefault().treeSize()); } }