@Override public MethodSource<O> removeJavaDoc() { method.setJavadoc(null); return this; }
@Override public JavaDocSource<MethodSource<O>> getJavaDoc() { Javadoc javadoc = method.getJavadoc(); if (javadoc == null) { javadoc = method.getAST().newJavadoc(); method.setJavadoc(javadoc); } return new JavaDocImpl<MethodSource<O>>(this, javadoc); }
/** * Generate Javadoc for UML Operation. * * @param ast * AST tree JDT * @param operation * UML Operation - Method * @param md * MethodDeclaration */ public void generateMethodJavadoc(AST ast, Operation operation, MethodDeclaration md) { EList<Comment> comments = operation.getOwnedComments(); for (Comment comment : comments) { Javadoc javadoc = ast.newJavadoc(); generateJavadoc(ast, comment, javadoc); md.setJavadoc(javadoc); } }
/** * Generate Javadoc for Getter and Setter method. * * @param ast * JDT AST tree * @param property * UML Property * @param methodDeclaration * MethodDeclaration for Getter and Setter */ public void generateGetterSetterJavadoc(AST ast, Property property, MethodDeclaration methodDeclaration) { EList<Comment> comments = property.getOwnedComments(); for (Comment comment : comments) { Javadoc javadoc = ast.newJavadoc(); generateJavadoc(ast, comment, javadoc); methodDeclaration.setJavadoc(javadoc); } }
private MethodDeclaration createNewMethod(ASTNode[] selectedNodes, String lineDelimiter, TextEditGroup substitute) throws CoreException { MethodDeclaration result = createNewMethodDeclaration(); result.setBody(createMethodBody(selectedNodes, substitute, result.getModifiers())); if (fGenerateJavadoc) { AbstractTypeDeclaration enclosingType = ASTNodes.getParent(fAnalyzer.getEnclosingBodyDeclaration(), AbstractTypeDeclaration.class); String string = CodeGeneration.getMethodComment(fCUnit, enclosingType.getName().getIdentifier(), result, null, lineDelimiter); if (string != null) { Javadoc javadoc = (Javadoc) fRewriter.createStringPlaceholder(string, ASTNode.JAVADOC); result.setJavadoc(javadoc); } } return result; }
private MethodDeclaration createNewMethod(ASTNode[] selectedNodes, String lineDelimiter, TextEditGroup substitute) throws CoreException { MethodDeclaration result= createNewMethodDeclaration(); result.setBody(createMethodBody(selectedNodes, substitute, result.getModifiers())); if (fGenerateJavadoc) { AbstractTypeDeclaration enclosingType= ASTNodes.getParent(fAnalyzer.getEnclosingBodyDeclaration(), AbstractTypeDeclaration.class); String string= CodeGeneration.getMethodComment(fCUnit, enclosingType.getName().getIdentifier(), result, null, lineDelimiter); if (string != null) { Javadoc javadoc= (Javadoc)fRewriter.createStringPlaceholder(string, ASTNode.JAVADOC); result.setJavadoc(javadoc); } } return result; }
private MethodDeclaration createNewMethod(ASTNode[] selectedNodes, String lineDelimiter, TextEditGroup substitute) throws CoreException { MethodDeclaration result= createNewMethodDeclaration(); result.setBody(createMethodBody(selectedNodes, substitute, result.getModifiers())); if (fGenerateJavadoc) { AbstractTypeDeclaration enclosingType= (AbstractTypeDeclaration)ASTNodes.getParent(fAnalyzer.getEnclosingBodyDeclaration(), AbstractTypeDeclaration.class); String string= CodeGeneration.getMethodComment(fCUnit, enclosingType.getName().getIdentifier(), result, null, lineDelimiter); if (string != null) { Javadoc javadoc= (Javadoc)fRewriter.createStringPlaceholder(string, ASTNode.JAVADOC); result.setJavadoc(javadoc); } } return result; }
private void createMethodComment(MethodDeclaration newDeclaration, IMethodBinding copyFrom) throws CoreException { if (fSettings.createComments) { String string= CodeGeneration.getMethodComment(fRewrite.getCu(), fType.getQualifiedName(), newDeclaration, copyFrom, StubUtility.getLineDelimiterUsed(fRewrite.getCu())); if (string != null) { Javadoc javadoc= (Javadoc) fRewrite.getASTRewrite().createStringPlaceholder(string, ASTNode.JAVADOC); newDeclaration.setJavadoc(javadoc); } } if (fSettings.overrideAnnotation && JavaModelUtil.is50OrHigher(fUnit.getJavaElement().getJavaProject())) StubUtility2.addOverrideAnnotation(fRewrite.getASTRewrite(), newDeclaration, copyFrom); }
private void addNewConstructorToSubclass(AbstractTypeDeclaration subclass, CompilationUnitRewrite cuRewrite) { AST ast= subclass.getAST(); MethodDeclaration newConstructor= ast.newMethodDeclaration(); newConstructor.setName(ast.newSimpleName(subclass.getName().getIdentifier())); newConstructor.setConstructor(true); newConstructor.setJavadoc(null); newConstructor.modifiers().addAll(ASTNodeFactory.newModifiers(ast, getAccessModifier(subclass))); newConstructor.setReturnType2(ast.newPrimitiveType(PrimitiveType.VOID)); Block body= ast.newBlock(); newConstructor.setBody(body); SuperConstructorInvocation superCall= ast.newSuperConstructorInvocation(); addArgumentsToNewSuperConstructorCall(superCall, cuRewrite); body.statements().add(superCall); String msg= RefactoringCoreMessages.ChangeSignatureRefactoring_add_constructor; TextEditGroup description= cuRewrite.createGroupDescription(msg); cuRewrite.getASTRewrite().getListRewrite(subclass, subclass.getBodyDeclarationsProperty()).insertFirst(newConstructor, description); // TODO use AbstractTypeDeclaration }
private MethodDeclaration createGetterMethod(AST ast, ASTRewrite rewriter, String lineDelimiter) throws CoreException { FieldDeclaration field = ASTNodes.getParent(fFieldDeclaration, FieldDeclaration.class); Type type = field.getType(); MethodDeclaration result = ast.newMethodDeclaration(); result.setName(ast.newSimpleName(fGetterName)); result.modifiers().addAll(ASTNodeFactory.newModifiers(ast, createModifiers())); Type returnType = DimensionRewrite.copyTypeAndAddDimensions(type, fFieldDeclaration.extraDimensions(), rewriter); result.setReturnType2(returnType); Block block = ast.newBlock(); result.setBody(block); String body = CodeGeneration.getGetterMethodBodyContent(fField.getCompilationUnit(), getTypeName(field.getParent()), fGetterName, fField.getElementName(), lineDelimiter); if (body != null) { body = body.substring(0, body.lastIndexOf(lineDelimiter)); ASTNode getterNode = rewriter.createStringPlaceholder(body, ASTNode.BLOCK); block.statements().add(getterNode); } else { ReturnStatement rs = ast.newReturnStatement(); rs.setExpression(ast.newSimpleName(fField.getElementName())); block.statements().add(rs); } if (fGenerateJavadoc) { String string = CodeGeneration.getGetterComment(fField.getCompilationUnit(), getTypeName(field.getParent()), fGetterName, fField.getElementName(), ASTNodes.asString(type), StubUtility.getBaseName(fField), lineDelimiter); if (string != null) { Javadoc javadoc = (Javadoc) fRewriter.createStringPlaceholder(string, ASTNode.JAVADOC); result.setJavadoc(javadoc); } } return result; }
private void addNewConstructorToSubclass(AbstractTypeDeclaration subclass, CompilationUnitRewrite cuRewrite) { AST ast= subclass.getAST(); MethodDeclaration newConstructor= ast.newMethodDeclaration(); newConstructor.setName(ast.newSimpleName(subclass.getName().getIdentifier())); newConstructor.setConstructor(true); newConstructor.setJavadoc(null); newConstructor.modifiers().addAll(ASTNodeFactory.newModifiers(ast, getAccessModifier(subclass))); newConstructor.setReturnType2(ast.newPrimitiveType(PrimitiveType.VOID)); Block body= ast.newBlock(); newConstructor.setBody(body); SuperConstructorInvocation superCall= ast.newSuperConstructorInvocation(); addArgumentsToNewSuperConstructorCall(superCall, cuRewrite); body.statements().add(superCall); String msg= RefactoringCoreMessages.ChangeSignatureRefactoring_add_constructor; TextEditGroup description= cuRewrite.createGroupDescription(msg); cuRewrite.getASTRewrite().getListRewrite(subclass, subclass.getBodyDeclarationsProperty()).insertFirst(newConstructor, description); // TODO use AbstractTypeDeclaration }
private void createMethodComment(MethodDeclaration newDeclaration, IMethodBinding copyFrom) throws CoreException { if (fSettings.createComments) { String string= CodeGeneration.getMethodComment(fRewrite.getCu(), fType.getQualifiedName(), newDeclaration, copyFrom, StubUtility.getLineDelimiterUsed(fRewrite.getCu())); if (string != null) { Javadoc javadoc= (Javadoc) fRewrite.getASTRewrite().createStringPlaceholder(string, ASTNode.JAVADOC); newDeclaration.setJavadoc(javadoc); } } IJavaProject project= fUnit.getJavaElement().getJavaProject(); StubUtility2Core.addOverrideAnnotation(fSettings, project, fRewrite.getASTRewrite(), fRewrite.getImportRewrite(), newDeclaration, copyFrom.getDeclaringClass().isInterface(), null); }
private void createMethodComment(MethodDeclaration newDeclaration, IMethodBinding copyFrom) throws CoreException { if (fSettings.createComments) { String string= CodeGeneration.getMethodComment(fRewrite.getCu(), fType.getQualifiedName(), newDeclaration, copyFrom, StubUtility.getLineDelimiterUsed(fRewrite.getCu())); if (string != null) { Javadoc javadoc= (Javadoc) fRewrite.getASTRewrite().createStringPlaceholder(string, ASTNode.JAVADOC); newDeclaration.setJavadoc(javadoc); } } IJavaProject project= fUnit.getJavaElement().getJavaProject(); StubUtility2.addOverrideAnnotation(fSettings, project, fRewrite.getASTRewrite(), fRewrite.getImportRewrite(), newDeclaration, copyFrom.getDeclaringClass().isInterface(), null); }
private void addNewConstructorToSubclass(AbstractTypeDeclaration subclass, CompilationUnitRewrite cuRewrite) { AST ast= subclass.getAST(); MethodDeclaration newConstructor= ast.newMethodDeclaration(); newConstructor.setName(ast.newSimpleName(subclass.getName().getIdentifier())); newConstructor.setConstructor(true); newConstructor.setExtraDimensions(0); newConstructor.setJavadoc(null); newConstructor.modifiers().addAll(ASTNodeFactory.newModifiers(ast, getAccessModifier(subclass))); newConstructor.setReturnType2(ast.newPrimitiveType(PrimitiveType.VOID)); Block body= ast.newBlock(); newConstructor.setBody(body); SuperConstructorInvocation superCall= ast.newSuperConstructorInvocation(); addArgumentsToNewSuperConstructorCall(superCall, cuRewrite); body.statements().add(superCall); String msg= RefactoringCoreMessages.ChangeSignatureRefactoring_add_constructor; TextEditGroup description= cuRewrite.createGroupDescription(msg); cuRewrite.getASTRewrite().getListRewrite(subclass, subclass.getBodyDeclarationsProperty()).insertFirst(newConstructor, description); // TODO use AbstractTypeDeclaration }
/** * adds a comment (if necessary) and an <code>@Override</code> annotation to the generated * method * * @throws CoreException if creation failed */ protected void createMethodComment() throws CoreException { ITypeBinding object= fAst.resolveWellKnownType("java.lang.Object"); //$NON-NLS-1$ IMethodBinding[] objms= object.getDeclaredMethods(); IMethodBinding objectMethod= null; for (int i= 0; i < objms.length; i++) { if (objms[i].getName().equals(METHODNAME_TO_STRING) && objms[i].getParameterTypes().length == 0) objectMethod= objms[i]; } if (fContext.isCreateComments()) { String docString= CodeGeneration.getMethodComment(fContext.getCompilationUnit(), fContext.getTypeBinding().getQualifiedName(), toStringMethod, objectMethod, StubUtility .getLineDelimiterUsed(fContext.getCompilationUnit())); if (docString != null) { Javadoc javadoc= (Javadoc)fContext.getASTRewrite().createStringPlaceholder(docString, ASTNode.JAVADOC); toStringMethod.setJavadoc(javadoc); } } StubUtility2.addOverrideAnnotation(fContext.getCodeGenerationSettings(), fContext.getTypeBinding().getJavaElement().getJavaProject(), fContext.getASTRewrite(), fContext.getImportRewrite(), toStringMethod, objectMethod.getDeclaringClass().isInterface(), null); }
private void addInitializersToConstructors(ASTRewrite rewrite) throws CoreException { Assert.isTrue(! isDeclaredInAnonymousClass()); final AbstractTypeDeclaration declaration= (AbstractTypeDeclaration)getMethodDeclaration().getParent(); final MethodDeclaration[] constructors= getAllConstructors(declaration); if (constructors.length == 0) { AST ast= rewrite.getAST(); MethodDeclaration newConstructor= ast.newMethodDeclaration(); newConstructor.setConstructor(true); newConstructor.modifiers().addAll(ast.newModifiers(declaration.getModifiers() & ModifierRewrite.VISIBILITY_MODIFIERS)); newConstructor.setName(ast.newSimpleName(declaration.getName().getIdentifier())); newConstructor.setJavadoc(getNewConstructorComment(rewrite)); newConstructor.setBody(ast.newBlock()); addFieldInitializationToConstructor(rewrite, newConstructor); int insertionIndex= computeInsertIndexForNewConstructor(declaration); rewrite.getListRewrite(declaration, declaration.getBodyDeclarationsProperty()).insertAt(newConstructor, insertionIndex, null); } else { for (int index= 0; index < constructors.length; index++) { if (shouldInsertTempInitialization(constructors[index])) addFieldInitializationToConstructor(rewrite, constructors[index]); } } }
private void addInitializersToConstructors(ASTRewrite rewrite) throws CoreException { Assert.isTrue(! isDeclaredInAnonymousClass()); final AbstractTypeDeclaration declaration= (AbstractTypeDeclaration)getMethodDeclaration().getParent(); final MethodDeclaration[] constructors= getAllConstructors(declaration); if (constructors.length == 0) { AST ast= rewrite.getAST(); MethodDeclaration newConstructor= ast.newMethodDeclaration(); newConstructor.setConstructor(true); newConstructor.modifiers().addAll(ast.newModifiers(declaration.getModifiers() & ModifierRewrite.VISIBILITY_MODIFIERS)); newConstructor.setName(ast.newSimpleName(declaration.getName().getIdentifier())); newConstructor.setJavadoc(getNewConstructorComment(rewrite)); newConstructor.setBody(ast.newBlock()); addFieldInitializationToConstructor(rewrite, newConstructor); int insertionIndex= computeInsertIndexForNewConstructor(declaration); rewrite.getListRewrite(declaration, declaration.getBodyDeclarationsProperty()).insertAt(newConstructor, insertionIndex, null); } else { for (int index= 0; index < constructors.length; index++) { if (shouldInsertTempInitialization(constructors[index])) addFieldInitializationToConstructor(rewrite, constructors[index]); } } }
private void createAbstractMethod(final IMethod sourceMethod, final CompilationUnitRewrite sourceRewriter, final CompilationUnit declaringCuNode, final AbstractTypeDeclaration destination, final TypeVariableMaplet[] mapping, final CompilationUnitRewrite targetRewrite, final Map adjustments, final IProgressMonitor monitor, final RefactoringStatus status) throws JavaModelException { final MethodDeclaration oldMethod= ASTNodeSearchUtil.getMethodDeclarationNode(sourceMethod, declaringCuNode); if (JavaModelUtil.is50OrHigher(sourceMethod.getJavaProject()) && (fSettings.overrideAnnotation || JavaCore.ERROR.equals(sourceMethod.getJavaProject().getOption(JavaCore.COMPILER_PB_MISSING_OVERRIDE_ANNOTATION, true)))) { final MarkerAnnotation annotation= sourceRewriter.getAST().newMarkerAnnotation(); annotation.setTypeName(sourceRewriter.getAST().newSimpleName("Override")); //$NON-NLS-1$ sourceRewriter.getASTRewrite().getListRewrite(oldMethod, MethodDeclaration.MODIFIERS2_PROPERTY).insertFirst(annotation, sourceRewriter.createCategorizedGroupDescription(RefactoringCoreMessages.PullUpRefactoring_add_override_annotation, SET_PULL_UP)); } final MethodDeclaration newMethod= targetRewrite.getAST().newMethodDeclaration(); newMethod.setBody(null); newMethod.setConstructor(false); newMethod.setExtraDimensions(oldMethod.getExtraDimensions()); newMethod.setJavadoc(null); int modifiers= getModifiersWithUpdatedVisibility(sourceMethod, Modifier.ABSTRACT | JdtFlags.clearFlag(Modifier.NATIVE | Modifier.FINAL, sourceMethod.getFlags()), adjustments, monitor, false, status); if (oldMethod.isVarargs()) modifiers&= ~Flags.AccVarargs; newMethod.modifiers().addAll(ASTNodeFactory.newModifiers(targetRewrite.getAST(), modifiers)); newMethod.setName(((SimpleName) ASTNode.copySubtree(targetRewrite.getAST(), oldMethod.getName()))); copyReturnType(targetRewrite.getASTRewrite(), getDeclaringType().getCompilationUnit(), oldMethod, newMethod, mapping); copyParameters(targetRewrite.getASTRewrite(), getDeclaringType().getCompilationUnit(), oldMethod, newMethod, mapping); copyThrownExceptions(oldMethod, newMethod); ImportRewriteUtil.addImports(targetRewrite, newMethod, new HashMap(), new HashMap(), false); targetRewrite.getASTRewrite().getListRewrite(destination, destination.getBodyDeclarationsProperty()).insertAt(newMethod, ASTNodes.getInsertionIndex(newMethod, destination.bodyDeclarations()), targetRewrite.createCategorizedGroupDescription(RefactoringCoreMessages.PullUpRefactoring_add_abstract_method, SET_PULL_UP)); }
private void addInitializersToConstructors(ASTRewrite rewrite) throws CoreException { Assert.isTrue(! isDeclaredInAnonymousClass()); final AbstractTypeDeclaration declaration= (AbstractTypeDeclaration)getMethodDeclaration().getParent(); final MethodDeclaration[] constructors= getAllConstructors(declaration); if (constructors.length == 0) { AST ast= rewrite.getAST(); MethodDeclaration newConstructor= ast.newMethodDeclaration(); newConstructor.setConstructor(true); newConstructor.modifiers().addAll(ast.newModifiers(declaration.getModifiers() & ModifierRewrite.VISIBILITY_MODIFIERS)); newConstructor.setName(ast.newSimpleName(declaration.getName().getIdentifier())); newConstructor.setJavadoc(getNewConstructorComment(rewrite)); newConstructor.setBody(ast.newBlock()); addFieldInitializationToConstructor(rewrite, newConstructor); int insertionIndex= computeInsertIndexForNewConstructor(declaration); rewrite.getListRewrite(declaration, declaration.getBodyDeclarationsProperty()).insertAt(newConstructor, insertionIndex, null); } else { for (int index= 0; index < constructors.length; index++) { if (shouldInsertTempInitialization(constructors[index])) addFieldInitializationToConstructor(rewrite, constructors[index]); } } }
private void addMethodStubForAbstractMethod(final IMethod sourceMethod, final CompilationUnit declaringCuNode, final AbstractTypeDeclaration typeToCreateStubIn, final ICompilationUnit newCu, final CompilationUnitRewrite rewriter, final Map adjustments, final IProgressMonitor monitor, final RefactoringStatus status) throws CoreException { final MethodDeclaration methodToCreateStubFor= ASTNodeSearchUtil.getMethodDeclarationNode(sourceMethod, declaringCuNode); final AST ast= rewriter.getRoot().getAST(); final MethodDeclaration newMethod= ast.newMethodDeclaration(); newMethod.setBody(createMethodStub(methodToCreateStubFor, ast)); newMethod.setConstructor(false); newMethod.setExtraDimensions(methodToCreateStubFor.getExtraDimensions()); newMethod.modifiers().addAll(ASTNodeFactory.newModifiers(ast, getModifiersWithUpdatedVisibility(sourceMethod, JdtFlags.clearFlag(Modifier.NATIVE | Modifier.ABSTRACT, methodToCreateStubFor.getModifiers()), adjustments, new SubProgressMonitor(monitor, 1), false, status))); newMethod.setName(((SimpleName) ASTNode.copySubtree(ast, methodToCreateStubFor.getName()))); final TypeVariableMaplet[] mapping= TypeVariableUtil.composeMappings(TypeVariableUtil.subTypeToSuperType(getDeclaringType(), getDestinationType()), TypeVariableUtil.superTypeToInheritedType(getDestinationType(), ((IType) typeToCreateStubIn.resolveBinding().getJavaElement()))); copyReturnType(rewriter.getASTRewrite(), getDeclaringType().getCompilationUnit(), methodToCreateStubFor, newMethod, mapping); copyParameters(rewriter.getASTRewrite(), getDeclaringType().getCompilationUnit(), methodToCreateStubFor, newMethod, mapping); copyThrownExceptions(methodToCreateStubFor, newMethod); newMethod.setJavadoc(createJavadocForStub(typeToCreateStubIn.getName().getIdentifier(), methodToCreateStubFor, newMethod, newCu, rewriter.getASTRewrite())); ImportRewriteUtil.addImports(rewriter, newMethod, new HashMap(), new HashMap(), false); rewriter.getASTRewrite().getListRewrite(typeToCreateStubIn, typeToCreateStubIn.getBodyDeclarationsProperty()).insertAt(newMethod, ASTNodes.getInsertionIndex(newMethod, typeToCreateStubIn.bodyDeclarations()), rewriter.createCategorizedGroupDescription(RefactoringCoreMessages.PullUpRefactoring_add_method_stub, SET_PULL_UP)); }