List of usage examples for org.eclipse.jdt.core.dom MethodDeclaration toString
@Override public final String toString()
From source file:changetypes.ASTVisitorAtomicChange.java
License:Open Source License
public boolean visit(MethodDeclaration node) { IMethodBinding mtb = node.resolveBinding(); this.mtbStack.push(mtb); String nodeStr = node.toString(); String modifier = "protected"; int dex = nodeStr.indexOf(' '); if (dex >= 0) { String temp = nodeStr.substring(0, dex); if (temp.equals("public")) { modifier = "public"; } else if (temp.equals("private")) { modifier = "private"; }/*from w w w .j a v a 2 s . c om*/ } try { String visibility = getModifier(mtb); this.facts.add(Fact.makeMethodFact(getQualifiedName(mtb), getSimpleName(mtb), getQualifiedName(mtb.getDeclaringClass()), visibility)); } catch (Exception localException1) { System.err.println("Cannot resolve return method bindings for method " + node.getName().toString()); } try { String returntype = getQualifiedName(mtb.getReturnType()); this.facts.add(Fact.makeReturnsFact(getQualifiedName(mtb), returntype)); } catch (Exception localException2) { System.err.println("Cannot resolve return type bindings for method " + node.getName().toString()); } try { this.facts.add(Fact.makeModifierMethodFact(getQualifiedName(mtb), modifier)); } catch (Exception localException3) { System.err.println( "Cannot resolve return type bindings for method modifier " + node.getName().toString()); } try { String bodystring = node.getBody() != null ? node.getBody().toString() : ""; bodystring = bodystring.replace('\n', ' '); bodystring = bodystring.replace('"', ' '); bodystring = bodystring.replace('"', ' '); bodystring = bodystring.replace('\\', ' '); this.facts.add(Fact.makeMethodBodyFact(getQualifiedName(mtb), bodystring)); } catch (Exception localException4) { System.err.println("Cannot resolve bindings for body"); } SingleVariableDeclaration param; try { List<SingleVariableDeclaration> parameters = node.parameters(); StringBuilder sb = new StringBuilder(); for (Iterator localIterator = parameters.iterator(); localIterator.hasNext();) { param = (SingleVariableDeclaration) localIterator.next(); if (sb.length() != 0) { sb.append(", "); } sb.append(param.getType().toString()); sb.append(":"); sb.append(param.getName().toString()); } this.facts.add(Fact.makeParameterFact(getQualifiedName(mtb), sb.toString(), "")); } catch (Exception localException5) { System.err.println("Cannot resolve bindings for parameters"); } try { List<Name> thrownTypes = node.thrownExceptions(); for (Name n : thrownTypes) { this.facts.add(Fact.makeThrownExceptionFact(getQualifiedName(mtb), getQualifiedName(n.resolveTypeBinding()))); } } catch (Exception localException6) { System.err.println("Cannot resolve bindings for exceptions"); } return true; }
From source file:com.arcbees.gwtp.plugin.core.presenter.CreatePresenterPage.java
License:Apache License
private void addMethodsToNameTokens(ICompilationUnit unit, String nameToken, IProgressMonitor monitor) throws JavaModelException, MalformedTreeException, BadLocationException { String fieldName = getFieldNameFromNameToken(nameToken); Document document = new Document(unit.getSource()); CompilationUnit astRoot = initAstRoot(unit, monitor); // creation of ASTRewrite ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST()); // find existing method MethodDeclaration method = findMethod(astRoot, getNameTokenMethod(fieldName)); if (method != null) { logger.severe("FYI: the method in nameTokens already exists." + method.toString()); return;/*from ww w. j ava 2 s . co m*/ } List types = astRoot.types(); ASTNode rootNode = (ASTNode) types.get(0); ListRewrite listRewrite = rewrite.getListRewrite(rootNode, TypeDeclaration.BODY_DECLARATIONS_PROPERTY); ASTNode fieldNode = rewrite.createStringPlaceholder( "public static final String " + fieldName + " = \"" + nameToken + "\";", ASTNode.EMPTY_STATEMENT); StringBuilder nameTokenMethod = new StringBuilder(); nameTokenMethod.append("public static String ").append(getNameTokenMethod(fieldName)).append("() {\n") .append("return " + fieldName + ";\n").append("}\n"); ASTNode methodNode = rewrite.createStringPlaceholder(nameTokenMethod.toString(), ASTNode.EMPTY_STATEMENT); listRewrite.insertFirst(fieldNode, null); listRewrite.insertLast(methodNode, null); // computation of the text edits TextEdit edits = rewrite.rewriteAST(document, unit.getJavaProject().getOptions(true)); // computation of the new source code edits.apply(document); // format code String newSource = new CodeFormattingUtil(getJavaProject(), monitor).formatCodeJavaClass(document); // update of the compilation unit and save it IBuffer buffer = unit.getBuffer(); buffer.setContents(newSource); buffer.save(monitor, true); }
From source file:com.microsoft.javapkgsrv.JavaParser.java
License:MIT License
public List<Protocol.Response.OutlineResultResponse.Outline> ProcessOutlineRequest(Integer fileId) { final List<OutlineResultResponse.Outline> ret = new ArrayList<OutlineResultResponse.Outline>(); if (ActiveUnits.containsKey(fileId)) { CompilationUnit cu = ActiveUnits.get(fileId); cu.accept(new ASTVisitor() { @Override/*w w w . j ava2s .c o m*/ public boolean visit(TypeDeclaration type) { ret.add(OutlineResultResponse.Outline.newBuilder().setStartPosition(type.getStartPosition()) .setLength(type.getLength()).setHoverText(type.toString()) .setSummaryText(type.getName().toString()).build()); return true; } @Override public boolean visit(MethodDeclaration method) { ret.add(OutlineResultResponse.Outline.newBuilder().setStartPosition(method.getStartPosition()) .setLength(method.getLength()).setHoverText(method.toString()) .setSummaryText(method.getName().toString()).build()); return true; } }); } return ret; }
From source file:com.t3.doccreator.DocCreator.java
License:Open Source License
public DocCreator(String name, File file) { this.name = name; try (FileReader r = new FileReader(file)) { String content = IOUtils.toString(r); ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setSource(content.toCharArray()); final CompilationUnit cu = (CompilationUnit) parser.createAST(new NullProgressMonitor()); cu.accept(new ASTVisitor() { @Override/*from w ww . j a v a2 s.co m*/ public boolean visit(MethodDeclaration m) { if (!m.isConstructor() && !m.getName().getIdentifier().equals("getVariableName") && !m.toString().startsWith("private")) { MethodDefinition md = new MethodDefinition(m); methods.put(md.getName(), md); } return false; } }); for (String key : methods.keySet()) { } } catch (FileNotFoundException e) { try { System.err.println(file.getAbsoluteFile().getCanonicalPath()); } catch (IOException e1) { e1.printStackTrace(); } e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } }
From source file:de.crowdcode.kissmda.cartridges.simplejava.InterfaceGeneratorTest.java
License:Apache License
@SuppressWarnings("unchecked") @Test/*from w w w.j a v a 2 s . c o m*/ public void testGenerateMethodJavadoc() { AST ast = AST.newAST(AST.JLS3); ast.newCompilationUnit(); TypeDeclaration td = ast.newTypeDeclaration(); td.setInterface(true); Modifier modifier = ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD); td.modifiers().add(modifier); td.setName(ast.newSimpleName("Company")); MethodDeclaration md = ast.newMethodDeclaration(); md.setName(ast.newSimpleName("calculateAge")); Operation operation = mock(Operation.class, Answers.RETURNS_DEEP_STUBS.get()); EList<Comment> comments = mock(EList.class, Answers.RETURNS_DEEP_STUBS.get()); Iterator<Comment> commentIterator = mock(Iterator.class); Comment comment = mock(Comment.class, Answers.RETURNS_DEEP_STUBS.get()); when(operation.getOwnedComments()).thenReturn(comments); when(comments.iterator()).thenReturn(commentIterator); when(commentIterator.hasNext()).thenReturn(true, false); when(commentIterator.next()).thenReturn(comment); when(comment.getBody()).thenReturn("Comment...\nTest\n@author: Lofi Dewanto"); interfaceGenerator.generateMethodJavadoc(ast, operation, md); assertEquals("/** \n * Comment...\n * Test\n * @author: Lofi Dewanto\n */\nvoid calculateAge();\n", md.toString()); }
From source file:de.crowdcode.kissmda.cartridges.simplejava.InterfaceGeneratorTest.java
License:Apache License
@SuppressWarnings("unchecked") @Test/*from w ww . j a v a2 s . c o m*/ public void testGenerateGetterSetterJavadoc() { AST ast = AST.newAST(AST.JLS3); ast.newCompilationUnit(); TypeDeclaration td = ast.newTypeDeclaration(); td.setInterface(true); Modifier modifier = ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD); td.modifiers().add(modifier); td.setName(ast.newSimpleName("Company")); MethodDeclaration md = ast.newMethodDeclaration(); md.setName(ast.newSimpleName("calculateAge")); Property property = mock(Property.class, Answers.RETURNS_DEEP_STUBS.get()); EList<Comment> comments = mock(EList.class, Answers.RETURNS_DEEP_STUBS.get()); Iterator<Comment> commentIterator = mock(Iterator.class); Comment comment = mock(Comment.class, Answers.RETURNS_DEEP_STUBS.get()); when(property.getOwnedComments()).thenReturn(comments); when(comments.iterator()).thenReturn(commentIterator); when(commentIterator.hasNext()).thenReturn(true, false); when(commentIterator.next()).thenReturn(comment); when(comment.getBody()).thenReturn("Comment...\nTest\n@author: Lofi Dewanto"); interfaceGenerator.generateGetterSetterJavadoc(ast, property, md); assertEquals("/** \n * Comment...\n * Test\n * @author: Lofi Dewanto\n */\nvoid calculateAge();\n", md.toString()); }
From source file:de.crowdcode.kissmda.cartridges.simplejava.InterfaceGeneratorTest.java
License:Apache License
@SuppressWarnings("unchecked") @Test/* ww w. j a v a2s . c om*/ public void testGenerateMethodTemplateParams() { AST ast = AST.newAST(AST.JLS3); ast.newCompilationUnit(); TypeDeclaration td = ast.newTypeDeclaration(); td.setInterface(true); Modifier modifier = ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD); td.modifiers().add(modifier); td.setName(ast.newSimpleName("Company")); MethodDeclaration md = ast.newMethodDeclaration(); md.setName(ast.newSimpleName("calculateAge")); Operation operation = mock(Operation.class, Answers.RETURNS_DEEP_STUBS.get()); EList<TemplateParameter> templateParams = mock(EList.class, Answers.RETURNS_DEEP_STUBS.get()); Iterator<TemplateParameter> templateParamIterator = mock(Iterator.class); TemplateSignature templateSignature = mock(TemplateSignature.class, Answers.RETURNS_DEEP_STUBS.get()); TemplateParameter templateParameter = mock(TemplateParameter.class, Answers.RETURNS_DEEP_STUBS.get()); Classifier classifier = mock(Classifier.class); when(operation.getOwnedTemplateSignature()).thenReturn(templateSignature); when(templateSignature.getParameters()).thenReturn(templateParams); when(templateParams.iterator()).thenReturn(templateParamIterator); when(templateParamIterator.hasNext()).thenReturn(true, false); when(templateParamIterator.next()).thenReturn(templateParameter); when(templateParameter.getOwnedParameteredElement()).thenReturn(classifier); when(classifier.getLabel()).thenReturn("T"); interfaceGenerator.generateMethodTemplateParams(ast, operation, md); assertEquals("<T>void calculateAge();\n", md.toString()); }
From source file:de.crowdcode.kissmda.cartridges.simplejava.InterfaceGeneratorTest.java
License:Apache License
@SuppressWarnings("unchecked") @Test/*from w ww . j av a 2 s.c o m*/ public void testGenerateMethodParams() { AST ast = AST.newAST(AST.JLS3); ast.newCompilationUnit(); TypeDeclaration td = ast.newTypeDeclaration(); td.setInterface(true); Modifier modifier = ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD); td.modifiers().add(modifier); td.setName(ast.newSimpleName("Company")); MethodDeclaration md = ast.newMethodDeclaration(); md.setName(ast.newSimpleName("calculateAge")); Operation operation = mock(Operation.class, Answers.RETURNS_DEEP_STUBS.get()); EList<TemplateParameter> templateParams = mock(EList.class, Answers.RETURNS_DEEP_STUBS.get()); Iterator<TemplateParameter> templateParamIterator = mock(Iterator.class); TemplateSignature templateSignature = mock(TemplateSignature.class, Answers.RETURNS_DEEP_STUBS.get()); TemplateParameter templateParameter = mock(TemplateParameter.class, Answers.RETURNS_DEEP_STUBS.get()); Classifier classifier = mock(Classifier.class); when(operation.getOwnedTemplateSignature()).thenReturn(templateSignature); when(templateSignature.getParameters()).thenReturn(templateParams); when(templateParams.iterator()).thenReturn(templateParamIterator); when(templateParamIterator.hasNext()).thenReturn(true, false); when(templateParamIterator.next()).thenReturn(templateParameter); when(templateParameter.getOwnedParameteredElement()).thenReturn(classifier); interfaceGenerator.generateMethodParams(ast, td, operation, md); assertEquals("void calculateAge();\n", md.toString()); }
From source file:de.crowdcode.kissmda.cartridges.simplejava.InterfaceGeneratorTest.java
License:Apache License
@SuppressWarnings("unchecked") @Test/*from www .j a v a 2s .c om*/ public void testGenerateMethodParamsWithOneParameter() { AST ast = AST.newAST(AST.JLS3); ast.newCompilationUnit(); TypeDeclaration td = ast.newTypeDeclaration(); td.setInterface(true); Modifier modifier = ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD); td.modifiers().add(modifier); td.setName(ast.newSimpleName("Company")); MethodDeclaration md = ast.newMethodDeclaration(); md.setName(ast.newSimpleName("calculateAge")); Operation operation = mock(Operation.class, Answers.RETURNS_DEEP_STUBS.get()); EList<Parameter> parameters = mock(EList.class, Answers.RETURNS_DEEP_STUBS.get()); Iterator<Parameter> paramIterator = mock(Iterator.class); Parameter parameter = mock(Parameter.class, Answers.RETURNS_DEEP_STUBS.get()); Type type = mock(Type.class, Answers.RETURNS_DEEP_STUBS.get()); when(operation.getOwnedParameters()).thenReturn(parameters); when(parameters.iterator()).thenReturn(paramIterator); when(paramIterator.hasNext()).thenReturn(true, false); when(paramIterator.next()).thenReturn(parameter); when(parameter.getDirection()).thenReturn(ParameterDirectionKind.IN_LITERAL); when(parameter.getType()).thenReturn(type); when(parameter.getName()).thenReturn("person"); when(parameter.getUpper()).thenReturn(0); when(type.getName()).thenReturn("Person"); when(type.getQualifiedName()).thenReturn("de.component.Person"); interfaceGenerator.generateMethodParams(ast, td, operation, md); assertEquals("void calculateAge(de.component.Person person);\n", md.toString()); }
From source file:de.crowdcode.kissmda.cartridges.simplejava.InterfaceGeneratorTest.java
License:Apache License
@SuppressWarnings("unchecked") @Test/* w ww . j a va 2 s . co m*/ public void testGenerateMethodParamsWithOneParameterWithGenerics() { AST ast = AST.newAST(AST.JLS3); ast.newCompilationUnit(); TypeDeclaration td = ast.newTypeDeclaration(); td.setInterface(true); Modifier modifier = ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD); td.modifiers().add(modifier); td.setName(ast.newSimpleName("Company")); MethodDeclaration md = ast.newMethodDeclaration(); md.setName(ast.newSimpleName("calculateAge")); Operation operation = mock(Operation.class, Answers.RETURNS_DEEP_STUBS.get()); EList<Parameter> parameters = mock(EList.class, Answers.RETURNS_DEEP_STUBS.get()); Iterator<Parameter> paramIterator = mock(Iterator.class); Parameter parameter = mock(Parameter.class, Answers.RETURNS_DEEP_STUBS.get()); Type type = mock(Type.class, Answers.RETURNS_DEEP_STUBS.get()); when(operation.getOwnedParameters()).thenReturn(parameters); when(parameters.iterator()).thenReturn(paramIterator); when(paramIterator.hasNext()).thenReturn(true, false); when(paramIterator.next()).thenReturn(parameter); when(parameter.getDirection()).thenReturn(ParameterDirectionKind.IN_LITERAL); when(parameter.getType()).thenReturn(type); when(parameter.getName()).thenReturn("person"); when(parameter.getUpper()).thenReturn(0); when(type.getName()).thenReturn("Person<Integer>"); when(type.getQualifiedName()).thenReturn("de.component.Person<Integer>"); interfaceGenerator.generateMethodParams(ast, td, operation, md); assertEquals("void calculateAge(de.component.Person<Integer> person);\n", md.toString()); }