Java Code Examples for org.eclipse.jdt.core.dom.MethodDeclaration#getJavadoc()

The following examples show how to use org.eclipse.jdt.core.dom.MethodDeclaration#getJavadoc() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UncommentedMainQuickfix.java    From eclipse-cs with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected ASTVisitor handleGetCorrectingASTVisitor(final IRegion lineInfo,
        final int markerStartOffset) {
  return new ASTVisitor() {

    @Override
    public boolean visit(MethodDeclaration node) {
      // recalculate start position because optional javadoc is mixed
      // into the original start position
      int pos = node.getStartPosition() + (node.getJavadoc() != null
              ? node.getJavadoc().getLength() + JAVADOC_COMMENT_LENGTH
              : 0);
      if (containsPosition(lineInfo, pos)
              && node.getName().getFullyQualifiedName().equals("main")) {
        node.delete();
      }
      return true;
    }
  };
}
 
Example 2
Source File: MoveMethodRefactoring.java    From JDeodorant with MIT License 6 votes vote down vote up
private void addParamTagElementToJavadoc(MethodDeclaration newMethodDeclaration, ASTRewrite targetRewriter, String parameterToBeAdded) {
	if(newMethodDeclaration.getJavadoc() != null) {
		AST ast = newMethodDeclaration.getAST();
		Javadoc javadoc = newMethodDeclaration.getJavadoc();
		List<TagElement> tags = javadoc.tags();
		TagElement returnTagElement = null;
		for(TagElement tag : tags) {
			if(tag.getTagName() != null && tag.getTagName().equals(TagElement.TAG_RETURN)) {
				returnTagElement = tag;
				break;
			}
		}
		
		TagElement tagElement = ast.newTagElement();
		targetRewriter.set(tagElement, TagElement.TAG_NAME_PROPERTY, TagElement.TAG_PARAM, null);
		ListRewrite fragmentsRewrite = targetRewriter.getListRewrite(tagElement, TagElement.FRAGMENTS_PROPERTY);
		SimpleName paramName = ast.newSimpleName(parameterToBeAdded);
		fragmentsRewrite.insertLast(paramName, null);
		
		ListRewrite tagsRewrite = targetRewriter.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY);
		if(returnTagElement != null)
			tagsRewrite.insertBefore(tagElement, returnTagElement, null);
		else
			tagsRewrite.insertLast(tagElement, null);
	}
}
 
Example 3
Source File: MoveMethodRefactoring.java    From JDeodorant with MIT License 6 votes vote down vote up
private void removeParamTagElementFromJavadoc(MethodDeclaration newMethodDeclaration, ASTRewrite targetRewriter, String parameterToBeRemoved) {
	if(newMethodDeclaration.getJavadoc() != null) {
		Javadoc javadoc = newMethodDeclaration.getJavadoc();
		List<TagElement> tags = javadoc.tags();
		for(TagElement tag : tags) {
			if(tag.getTagName() != null && tag.getTagName().equals(TagElement.TAG_PARAM)) {
				List<ASTNode> tagFragments = tag.fragments();
				boolean paramFound = false;
				for(ASTNode node : tagFragments) {
					if(node instanceof SimpleName) {
						SimpleName simpleName = (SimpleName)node;
						if(simpleName.getIdentifier().equals(parameterToBeRemoved)) {
							paramFound = true;
							break;
						}
					}
				}
				if(paramFound) {
					ListRewrite tagsRewrite = targetRewriter.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY);
					tagsRewrite.remove(tag, null);
					break;
				}
			}
		}
	}
}
 
Example 4
Source File: UncommentedMainQuickFix.java    From vscode-checkstyle with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public ASTVisitor getCorrectingASTVisitor(IRegion lineInfo, int markerStartOffset) {
    return new ASTVisitor() {

        @Override
        public boolean visit(MethodDeclaration node) {
            // recalculate start position because optional javadoc is mixed
            // into the original start position
            final int pos = node.getStartPosition() +
                    (node.getJavadoc() != null ? node.getJavadoc().getLength() + JAVADOC_COMMENT_LENGTH : 0);
            if (containsPosition(lineInfo, pos) && node.getName().getFullyQualifiedName().equals("main")) {
                node.delete();
            }
            return true;
        }
    };
}
 
Example 5
Source File: ChangeMethodSignatureProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private TagElement findThrowsTag(MethodDeclaration decl, Type exception) {
	Javadoc javadoc= decl.getJavadoc();
	if (javadoc != null) {
		String name= ASTNodes.getTypeName(exception);
		return JavadocTagsSubProcessor.findThrowsTag(javadoc, name);
	}
	return null;
}
 
Example 6
Source File: ChangeMethodSignatureProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private TagElement findParamTag(MethodDeclaration decl, SingleVariableDeclaration param) {
	Javadoc javadoc= decl.getJavadoc();
	if (javadoc != null) {
		return JavadocTagsSubProcessor.findParamTag(javadoc, param.getName().getIdentifier());
	}
	return null;
}
 
Example 7
Source File: ExceptionOccurrencesFinder.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public boolean visit(MethodDeclaration node) {
	Javadoc javadoc= node.getJavadoc();
	if (javadoc != null) {
		List<TagElement> tags= javadoc.tags();
		for (TagElement tag : tags) {
			String tagName= tag.getTagName();
			if (TagElement.TAG_EXCEPTION.equals(tagName) || TagElement.TAG_THROWS.equals(tagName)) {
				ASTNode name= (ASTNode) tag.fragments().get(0);
				if (name instanceof Name) {
					if (name != fSelectedNode && Bindings.equals(fException, ((Name) name).resolveBinding())) {
						fResult.add(new OccurrenceLocation(name.getStartPosition(), name.getLength(), 0, fDescription));
					}
				}
			}
		}
	}
	List<Type> thrownExceptionTypes= node.thrownExceptionTypes();
	for (Iterator<Type> iter= thrownExceptionTypes.iterator(); iter.hasNext(); ) {
		Type type = iter.next();
		if (type != fSelectedNode && Bindings.equals(fException, type.resolveBinding())) {
			fResult.add(new OccurrenceLocation(type.getStartPosition(), type.getLength(), 0, fDescription));
		}
	}
	Block body= node.getBody();
	if (body != null) {
		node.getBody().accept(this);
	}
	return false;
}
 
Example 8
Source File: JavadocUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Decide whether to add a "param" javadoc tag or not.
 * @param methodDeclaration the method declaration
 * @return method has javadoc && (method had no parameter before || there is already an @param tag)
 */
public static boolean shouldAddParamJavadoc(MethodDeclaration methodDeclaration) {
	Javadoc javadoc= methodDeclaration.getJavadoc();
	if (javadoc == null)
		return false;
	if (methodDeclaration.parameters().size() == 0)
		return true;
	List<TagElement> tags= javadoc.tags();
	for (Iterator<TagElement> iter= tags.iterator(); iter.hasNext();) {
		TagElement element= iter.next();
		if (TagElement.TAG_PARAM.equals(element.getTagName()))
			return true;
	}
	return false;
}
 
Example 9
Source File: MethodInvocationResolver.java    From KodeBeagle with Apache License 2.0 5 votes vote down vote up
@Override
public boolean visit(MethodDeclaration node) {
    methodStack.push(node);
    MethodDecl methoDecl = getMethodDecl(node);
    declaredMethods.add(methoDecl);
    Javadoc javadoc = node.getJavadoc();
    ASTNode parent = node.getParent();
    if (javadoc != null && parent instanceof AbstractTypeDeclaration) {
        String typeName = ((AbstractTypeDeclaration) parent).getName().getFullyQualifiedName();
        String fqt = currentPackage + "." + removeSpecialSymbols(typeName);
        addMethodDoc(fqt, methoDecl, javadoc.toString());

    }
    return super.visit(node);
}
 
Example 10
Source File: ChangeMethodSignatureProposal.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private TagElement findThrowsTag(MethodDeclaration decl, Type exception) {
	Javadoc javadoc= decl.getJavadoc();
	if (javadoc != null) {
		String name= ASTNodes.getTypeName(exception);
		return JavadocTagsSubProcessor.findThrowsTag(javadoc, name);
	}
	return null;
}
 
Example 11
Source File: ChangeMethodSignatureProposal.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private TagElement findParamTag(MethodDeclaration decl, SingleVariableDeclaration param) {
	Javadoc javadoc= decl.getJavadoc();
	if (javadoc != null) {
		return JavadocTagsSubProcessor.findParamTag(javadoc, param.getName().getIdentifier());
	}
	return null;
}
 
Example 12
Source File: DesignForExtensionQuickFix.java    From vscode-checkstyle with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public ASTVisitor getCorrectingASTVisitor(IRegion lineInfo, int markerStartOffset) {
    return new ASTVisitor() {

        @SuppressWarnings("unchecked")
        @Override
        public boolean visit(MethodDeclaration node) {
            // recalculate start position because optional javadoc is mixed
            // into the original start position
            final int pos = node.getStartPosition() +
                    (node.getJavadoc() != null ? node.getJavadoc().getLength() + JAVADOC_COMMENT_LENGTH : 0);
            if (containsPosition(lineInfo, pos)) {

                if (!Modifier.isFinal(node.getModifiers())) {

                    final Modifier finalModifier = node.getAST().newModifier(ModifierKeyword.FINAL_KEYWORD);
                    node.modifiers().add(finalModifier);

                    // reorder modifiers into their correct order
                    final List<ASTNode> reorderedModifiers = ModifierOrderQuickFix
                            .reorderModifiers(node.modifiers());
                    node.modifiers().clear();
                    node.modifiers().addAll(reorderedModifiers);
                }
            }
            return true;
        }
    };
}
 
Example 13
Source File: DesignForExtensionQuickfix.java    From eclipse-cs with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected ASTVisitor handleGetCorrectingASTVisitor(final IRegion lineInfo,
        final int markerStartOffset) {
  return new ASTVisitor() {

    @SuppressWarnings("unchecked")
    @Override
    public boolean visit(MethodDeclaration node) {
      // recalculate start position because optional javadoc is mixed
      // into the original start position
      int pos = node.getStartPosition() + (node.getJavadoc() != null
              ? node.getJavadoc().getLength() + JAVADOC_COMMENT_LENGTH
              : 0);
      if (containsPosition(lineInfo, pos)) {

        if (!Modifier.isFinal(node.getModifiers())) {

          Modifier finalModifier = node.getAST().newModifier(ModifierKeyword.FINAL_KEYWORD);
          node.modifiers().add(finalModifier);

          // reorder modifiers into their correct order
          List<ASTNode> reorderedModifiers = ModifierOrderQuickfix.reOrderModifiers(node.modifiers());
          node.modifiers().clear();
          node.modifiers().addAll(reorderedModifiers);
        }
      }
      return true;
    }
  };
}
 
Example 14
Source File: NewVariableCorrectionProposal.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private ASTRewrite doAddParam(CompilationUnit cu) {
	AST ast= cu.getAST();
	SimpleName node= fOriginalNode;

	BodyDeclaration decl= ASTResolving.findParentBodyDeclaration(node);
	if (decl instanceof MethodDeclaration) {
		MethodDeclaration methodDeclaration= (MethodDeclaration) decl;

		ASTRewrite rewrite= ASTRewrite.create(ast);

		ImportRewrite imports= createImportRewrite((CompilationUnit) decl.getRoot());
		ImportRewriteContext importRewriteContext= new ContextSensitiveImportRewriteContext(decl, imports);

		SingleVariableDeclaration newDecl= ast.newSingleVariableDeclaration();
		newDecl.setType(evaluateVariableType(ast, imports, importRewriteContext, methodDeclaration.resolveBinding(), TypeLocation.PARAMETER));
		newDecl.setName(ast.newSimpleName(node.getIdentifier()));

		ListRewrite listRewriter= rewrite.getListRewrite(decl, MethodDeclaration.PARAMETERS_PROPERTY);
		listRewriter.insertLast(newDecl, null);

		// add javadoc tag
		Javadoc javadoc= methodDeclaration.getJavadoc();
		if (javadoc != null) {
			HashSet<String> leadingNames= new HashSet<>();
			for (Iterator<SingleVariableDeclaration> iter= methodDeclaration.parameters().iterator(); iter.hasNext();) {
				SingleVariableDeclaration curr= iter.next();
				leadingNames.add(curr.getName().getIdentifier());
			}
			SimpleName newTagRef= ast.newSimpleName(node.getIdentifier());

			TagElement newTagElement= ast.newTagElement();
			newTagElement.setTagName(TagElement.TAG_PARAM);
			newTagElement.fragments().add(newTagRef);
			TextElement commentStart= ast.newTextElement();
			newTagElement.fragments().add(commentStart);

			ListRewrite tagsRewriter= rewrite.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY);
			JavadocTagsSubProcessor.insertTag(tagsRewriter, newTagElement, leadingNames);
		}

		return rewrite;
	}
	return null;
}
 
Example 15
Source File: MoveInstanceMethodProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Creates the method comment for the target method declaration.
 *
 * @param rewrite
 *            the source ast rewrite
 * @param declaration
 *            the source method declaration
 * @throws JavaModelException
 *             if the argument references could not be generated
 */
protected void createMethodComment(final ASTRewrite rewrite, final MethodDeclaration declaration) throws JavaModelException {
	Assert.isNotNull(rewrite);
	Assert.isNotNull(declaration);
	final Javadoc comment= declaration.getJavadoc();
	if (comment != null) {
		final List<TagElement> tags= new LinkedList<TagElement>(comment.tags());
		final IVariableBinding[] bindings= getArgumentBindings(declaration);
		final Map<String, TagElement> elements= new HashMap<String, TagElement>(bindings.length);
		String name= null;
		List<? extends ASTNode> fragments= null;
		TagElement element= null;
		TagElement reference= null;
		IVariableBinding binding= null;
		for (int index= 0; index < bindings.length; index++) {
			binding= bindings[index];
			for (final Iterator<TagElement> iterator= comment.tags().iterator(); iterator.hasNext();) {
				element= iterator.next();
				name= element.getTagName();
				fragments= element.fragments();
				if (name != null) {
					if (name.equals(TagElement.TAG_PARAM) && !fragments.isEmpty() && fragments.get(0) instanceof SimpleName) {
						final SimpleName simple= (SimpleName) fragments.get(0);
						if (binding.getName().equals(simple.getIdentifier())) {
							elements.put(binding.getKey(), element);
							tags.remove(element);
						}
					} else if (reference == null)
						reference= element;
				}
			}
		}
		if (bindings.length == 0 && reference == null) {
			for (final Iterator<TagElement> iterator= comment.tags().iterator(); iterator.hasNext();) {
				element= iterator.next();
				name= element.getTagName();
				fragments= element.fragments();
				if (name != null && !name.equals(TagElement.TAG_PARAM))
					reference= element;
			}
		}
		final List<ASTNode> arguments= new ArrayList<ASTNode>(bindings.length + 1);
		createArgumentList(declaration, arguments, new IArgumentFactory() {

			public final ASTNode getArgumentNode(final IVariableBinding argument, final boolean last) throws JavaModelException {
				Assert.isNotNull(argument);
				if (elements.containsKey(argument.getKey()))
					return rewrite.createCopyTarget(elements.get(argument.getKey()));
				return JavadocUtil.createParamTag(argument.getName(), declaration.getAST(), fMethod.getJavaProject());
			}

			public final ASTNode getTargetNode() throws JavaModelException {
				return JavadocUtil.createParamTag(fTargetName, declaration.getAST(), fMethod.getJavaProject());
			}
		});
		final ListRewrite rewriter= rewrite.getListRewrite(comment, Javadoc.TAGS_PROPERTY);
		ASTNode tag= null;
		for (final Iterator<TagElement> iterator= comment.tags().iterator(); iterator.hasNext();) {
			tag= iterator.next();
			if (!tags.contains(tag))
				rewriter.remove(tag, null);
		}
		for (final Iterator<ASTNode> iterator= arguments.iterator(); iterator.hasNext();) {
			tag= iterator.next();
			if (reference != null)
				rewriter.insertBefore(tag, reference, null);
			else
				rewriter.insertLast(tag, null);
		}
	}
}
 
Example 16
Source File: ReturnTypeSubProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
public static void addMissingReturnTypeProposals(IInvocationContext context, IProblemLocationCore problem, Collection<ChangeCorrectionProposal> proposals) {
	ICompilationUnit cu= context.getCompilationUnit();

	CompilationUnit astRoot= context.getASTRoot();
	ASTNode selectedNode= problem.getCoveringNode(astRoot);
	if (selectedNode == null) {
		return;
	}
	BodyDeclaration decl= ASTResolving.findParentBodyDeclaration(selectedNode);
	if (decl instanceof MethodDeclaration) {
		MethodDeclaration methodDeclaration= (MethodDeclaration) decl;

		ReturnStatementCollector eval= new ReturnStatementCollector();
		decl.accept(eval);

		AST ast= astRoot.getAST();

		ITypeBinding typeBinding= eval.getTypeBinding(decl.getAST());
		typeBinding= Bindings.normalizeTypeBinding(typeBinding);
		if (typeBinding == null) {
			typeBinding= ast.resolveWellKnownType("void"); //$NON-NLS-1$
		}
		if (typeBinding.isWildcardType()) {
			typeBinding= ASTResolving.normalizeWildcardType(typeBinding, true, ast);
		}

		ASTRewrite rewrite= ASTRewrite.create(ast);

		String label= Messages.format(CorrectionMessages.ReturnTypeSubProcessor_missingreturntype_description, BindingLabelProviderCore.getBindingLabel(typeBinding, BindingLabelProviderCore.DEFAULT_TEXTFLAGS));
		LinkedCorrectionProposal proposal= new LinkedCorrectionProposal(label, CodeActionKind.QuickFix, cu, rewrite, IProposalRelevance.MISSING_RETURN_TYPE);

		ImportRewrite imports= proposal.createImportRewrite(astRoot);
		ImportRewriteContext importRewriteContext= new ContextSensitiveImportRewriteContext(decl, imports);
		Type type= imports.addImport(typeBinding, ast, importRewriteContext, TypeLocation.RETURN_TYPE);

		rewrite.set(methodDeclaration, MethodDeclaration.RETURN_TYPE2_PROPERTY, type, null);
		rewrite.set(methodDeclaration, MethodDeclaration.CONSTRUCTOR_PROPERTY, Boolean.FALSE, null);

		Javadoc javadoc= methodDeclaration.getJavadoc();
		if (javadoc != null && typeBinding != null) {
			TagElement newTag= ast.newTagElement();
			newTag.setTagName(TagElement.TAG_RETURN);
			TextElement commentStart= ast.newTextElement();
			newTag.fragments().add(commentStart);

			JavadocTagsSubProcessor.insertTag(rewrite.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY), newTag, null);
			proposal.addLinkedPosition(rewrite.track(commentStart), false, "comment_start"); //$NON-NLS-1$
		}

		String key= "return_type"; //$NON-NLS-1$
		proposal.addLinkedPosition(rewrite.track(type), true, key);
		if (typeBinding != null) {
			ITypeBinding[] bindings= ASTResolving.getRelaxingTypes(ast, typeBinding);
			for (int i= 0; i < bindings.length; i++) {
				proposal.addLinkedPositionProposal(key, bindings[i]);
			}
		}

		proposals.add(proposal);

		// change to constructor
		ASTNode parentType= ASTResolving.findParentType(decl);
		if (parentType instanceof AbstractTypeDeclaration) {
			boolean isInterface= parentType instanceof TypeDeclaration && ((TypeDeclaration) parentType).isInterface();
			if (!isInterface) {
				String constructorName= ((AbstractTypeDeclaration) parentType).getName().getIdentifier();
				ASTNode nameNode= methodDeclaration.getName();
				label= Messages.format(CorrectionMessages.ReturnTypeSubProcessor_wrongconstructorname_description, BasicElementLabels.getJavaElementName(constructorName));
				proposals.add(new ReplaceCorrectionProposal(label, cu, nameNode.getStartPosition(), nameNode.getLength(), constructorName, IProposalRelevance.CHANGE_TO_CONSTRUCTOR));
			}
		}
	}
}
 
Example 17
Source File: UtilTools.java    From apidiff with MIT License 4 votes vote down vote up
public static String getSufixJavadoc(final MethodDeclaration methodDeclaration){
	return ((methodDeclaration != null) && (methodDeclaration.getJavadoc() != null) && (!methodDeclaration.getJavadoc().equals("")))? "" : " WITHOUT JAVADOC";
}
 
Example 18
Source File: ReturnTypeSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public static void addMissingReturnTypeProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
	ICompilationUnit cu= context.getCompilationUnit();

	CompilationUnit astRoot= context.getASTRoot();
	ASTNode selectedNode= problem.getCoveringNode(astRoot);
	if (selectedNode == null) {
		return;
	}
	BodyDeclaration decl= ASTResolving.findParentBodyDeclaration(selectedNode);
	if (decl instanceof MethodDeclaration) {
		MethodDeclaration methodDeclaration= (MethodDeclaration) decl;

		ReturnStatementCollector eval= new ReturnStatementCollector();
		decl.accept(eval);

		AST ast= astRoot.getAST();

		ITypeBinding typeBinding= eval.getTypeBinding(decl.getAST());
		typeBinding= Bindings.normalizeTypeBinding(typeBinding);
		if (typeBinding == null) {
			typeBinding= ast.resolveWellKnownType("void"); //$NON-NLS-1$
		}
		if (typeBinding.isWildcardType()) {
			typeBinding= ASTResolving.normalizeWildcardType(typeBinding, true, ast);
		}

		ASTRewrite rewrite= ASTRewrite.create(ast);

		String label= Messages.format(CorrectionMessages.ReturnTypeSubProcessor_missingreturntype_description, BindingLabelProvider.getBindingLabel(typeBinding, BindingLabelProvider.DEFAULT_TEXTFLAGS));
		Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
		LinkedCorrectionProposal proposal= new LinkedCorrectionProposal(label, cu, rewrite, IProposalRelevance.MISSING_RETURN_TYPE, image);

		ImportRewrite imports= proposal.createImportRewrite(astRoot);
		ImportRewriteContext importRewriteContext= new ContextSensitiveImportRewriteContext(decl, imports);
		Type type= imports.addImport(typeBinding, ast, importRewriteContext);

		rewrite.set(methodDeclaration, MethodDeclaration.RETURN_TYPE2_PROPERTY, type, null);
		rewrite.set(methodDeclaration, MethodDeclaration.CONSTRUCTOR_PROPERTY, Boolean.FALSE, null);

		Javadoc javadoc= methodDeclaration.getJavadoc();
		if (javadoc != null && typeBinding != null) {
			TagElement newTag= ast.newTagElement();
			newTag.setTagName(TagElement.TAG_RETURN);
			TextElement commentStart= ast.newTextElement();
			newTag.fragments().add(commentStart);

			JavadocTagsSubProcessor.insertTag(rewrite.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY), newTag, null);
			proposal.addLinkedPosition(rewrite.track(commentStart), false, "comment_start"); //$NON-NLS-1$
		}

		String key= "return_type"; //$NON-NLS-1$
		proposal.addLinkedPosition(rewrite.track(type), true, key);
		if (typeBinding != null) {
			ITypeBinding[] bindings= ASTResolving.getRelaxingTypes(ast, typeBinding);
			for (int i= 0; i < bindings.length; i++) {
				proposal.addLinkedPositionProposal(key, bindings[i]);
			}
		}

		proposals.add(proposal);

		// change to constructor
		ASTNode parentType= ASTResolving.findParentType(decl);
		if (parentType instanceof AbstractTypeDeclaration) {
			boolean isInterface= parentType instanceof TypeDeclaration && ((TypeDeclaration) parentType).isInterface();
			if (!isInterface) {
				String constructorName= ((AbstractTypeDeclaration) parentType).getName().getIdentifier();
				ASTNode nameNode= methodDeclaration.getName();
				label= Messages.format(CorrectionMessages.ReturnTypeSubProcessor_wrongconstructorname_description, BasicElementLabels.getJavaElementName(constructorName));
				proposals.add(new ReplaceCorrectionProposal(label, cu, nameNode.getStartPosition(), nameNode.getLength(), constructorName, IProposalRelevance.CHANGE_TO_CONSTRUCTOR));
			}
		}
	}
}
 
Example 19
Source File: NewVariableCorrectionProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private ASTRewrite doAddParam(CompilationUnit cu) {
	AST ast= cu.getAST();
	SimpleName node= fOriginalNode;

	BodyDeclaration decl= ASTResolving.findParentBodyDeclaration(node);
	if (decl instanceof MethodDeclaration) {
		MethodDeclaration methodDeclaration= (MethodDeclaration) decl;

		ASTRewrite rewrite= ASTRewrite.create(ast);

		ImportRewrite imports= createImportRewrite((CompilationUnit) decl.getRoot());
		ImportRewriteContext importRewriteContext= new ContextSensitiveImportRewriteContext(decl, imports);

		SingleVariableDeclaration newDecl= ast.newSingleVariableDeclaration();
		newDecl.setType(evaluateVariableType(ast, imports, importRewriteContext, methodDeclaration.resolveBinding()));
		newDecl.setName(ast.newSimpleName(node.getIdentifier()));

		ListRewrite listRewriter= rewrite.getListRewrite(decl, MethodDeclaration.PARAMETERS_PROPERTY);
		listRewriter.insertLast(newDecl, null);

		addLinkedPosition(rewrite.track(node), true, KEY_NAME);

		// add javadoc tag
		Javadoc javadoc= methodDeclaration.getJavadoc();
		if (javadoc != null) {
			HashSet<String> leadingNames= new HashSet<String>();
			for (Iterator<SingleVariableDeclaration> iter= methodDeclaration.parameters().iterator(); iter.hasNext();) {
				SingleVariableDeclaration curr= iter.next();
				leadingNames.add(curr.getName().getIdentifier());
			}
			SimpleName newTagRef= ast.newSimpleName(node.getIdentifier());

			TagElement newTagElement= ast.newTagElement();
			newTagElement.setTagName(TagElement.TAG_PARAM);
			newTagElement.fragments().add(newTagRef);
			TextElement commentStart= ast.newTextElement();
			newTagElement.fragments().add(commentStart);

			addLinkedPosition(rewrite.track(newTagRef), false, KEY_NAME);
			addLinkedPosition(rewrite.track(commentStart), false, "comment_start"); //$NON-NLS-1$

			ListRewrite tagsRewriter= rewrite.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY);
			JavadocTagsSubProcessor.insertTag(tagsRewriter, newTagElement, leadingNames);
		}
		
		addLinkedPosition(rewrite.track(newDecl.getType()), false, KEY_TYPE);
		addLinkedPosition(rewrite.track(newDecl.getName()), false, KEY_NAME);

		return rewrite;
	}
	return null;
}
 
Example 20
Source File: UtilTools.java    From apidiff with MIT License 4 votes vote down vote up
public static Boolean containsJavadoc(final AbstractTypeDeclaration node, final MethodDeclaration methodDeclaration){
	Boolean typeContainsJavadoc = containsJavadoc(node);
	return (typeContainsJavadoc && (methodDeclaration != null) && (methodDeclaration.getJavadoc() != null) && (!methodDeclaration.getJavadoc().equals("")))? true : false;
}