private void insertAllMissingTypeTags(ASTRewrite rewriter, TypeDeclaration typeDecl) { AST ast= typeDecl.getAST(); Javadoc javadoc= typeDecl.getJavadoc(); ListRewrite tagsRewriter= rewriter.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY); List<TypeParameter> typeParams= typeDecl.typeParameters(); for (int i= typeParams.size() - 1; i >= 0; i--) { TypeParameter decl= typeParams.get(i); String name= '<' + decl.getName().getIdentifier() + '>'; if (findTag(javadoc, TagElement.TAG_PARAM, name) == null) { TagElement newTag= ast.newTagElement(); newTag.setTagName(TagElement.TAG_PARAM); TextElement text= ast.newTextElement(); text.setText(name); newTag.fragments().add(text); insertTabStop(rewriter, newTag.fragments(), "typeParam" + i); //$NON-NLS-1$ insertTag(tagsRewriter, newTag, getPreviousTypeParamNames(typeParams, decl)); } } }
private static String getArgument(TagElement curr) { List<? extends ASTNode> fragments= curr.fragments(); if (!fragments.isEmpty()) { Object first= fragments.get(0); if (first instanceof Name) { return ASTNodes.getSimpleNameIdentifier((Name) first); } else if (first instanceof TextElement && TagElement.TAG_PARAM.equals(curr.getTagName())) { String text= ((TextElement) first).getText(); if ("<".equals(text) && fragments.size() >= 3) { //$NON-NLS-1$ Object second= fragments.get(1); Object third= fragments.get(2); if (second instanceof Name && third instanceof TextElement && ">".equals(((TextElement) third).getText())) { //$NON-NLS-1$ return '<' + ASTNodes.getSimpleNameIdentifier((Name) second) + '>'; } } else if (text.startsWith(String.valueOf('<')) && text.endsWith(String.valueOf('>')) && text.length() > 2) { return text.substring(1, text.length() - 1); } } } return null; }
private TagElement getDelegateJavadocTag(BodyDeclaration declaration) throws JavaModelException { Assert.isNotNull(declaration); String msg = RefactoringCoreMessages.DelegateCreator_use_member_instead; int firstParam = msg.indexOf("{0}"); // $NON-NLS-1$ Assert.isTrue(firstParam != -1); List<ASTNode> fragments = new ArrayList<ASTNode>(); TextElement text = getAst().newTextElement(); text.setText(msg.substring(0, firstParam).trim()); fragments.add(text); fragments.add(createJavadocMemberReferenceTag(declaration, getAst())); text = getAst().newTextElement(); text.setText(msg.substring(firstParam + 3).trim()); fragments.add(text); final TagElement tag = getAst().newTagElement(); tag.setTagName(TagElement.TAG_DEPRECATED); tag.fragments().addAll(fragments); return tag; }
private void insertAllMissingTypeTags(ASTRewrite rewriter, TypeDeclaration typeDecl) { AST ast = typeDecl.getAST(); Javadoc javadoc = typeDecl.getJavadoc(); ListRewrite tagsRewriter = rewriter.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY); List<TypeParameter> typeParams = typeDecl.typeParameters(); for (int i = typeParams.size() - 1; i >= 0; i--) { TypeParameter decl = typeParams.get(i); String name = '<' + decl.getName().getIdentifier() + '>'; if (findTag(javadoc, TagElement.TAG_PARAM, name) == null) { TagElement newTag = ast.newTagElement(); newTag.setTagName(TagElement.TAG_PARAM); TextElement text = ast.newTextElement(); text.setText(name); newTag.fragments().add(text); insertTabStop(rewriter, newTag.fragments(), "typeParam" + i); // $NON-NLS-1$ insertTag(tagsRewriter, newTag, getPreviousTypeParamNames(typeParams, decl)); } } }
private static String getArgument(TagElement curr) { List<? extends ASTNode> fragments = curr.fragments(); if (!fragments.isEmpty()) { Object first = fragments.get(0); if (first instanceof Name) { return ASTNodes.getSimpleNameIdentifier((Name) first); } else if (first instanceof TextElement && TagElement.TAG_PARAM.equals(curr.getTagName())) { String text = ((TextElement) first).getText(); if ("<".equals(text) && fragments.size() >= 3) { // $NON-NLS-1$ Object second = fragments.get(1); Object third = fragments.get(2); if (second instanceof Name && third instanceof TextElement && ">".equals(((TextElement) third).getText())) { // $NON-NLS-1$ return '<' + ASTNodes.getSimpleNameIdentifier((Name) second) + '>'; } } else if (text.startsWith(String.valueOf('<')) && text.endsWith(String.valueOf('>')) && text.length() > 2) { return text.substring(1, text.length() - 1); } } } return null; }
/** * Creates a new region updater for the given text edit contained within the * given node. * * @param originalEdit the text edit * @param node the most-specific node that contains the text edit * @param referenceUpdater an reference updater knowledgeable about the * refactoring that is taking place * @param matcher an AST matcher knowledgeable about refactoring that is * taking place * @return a region updater instance for the given text edit * @throws RefactoringException if there was an error creating a region * updater */ public static RegionUpdater newRegionUpdater(ReplaceEdit originalEdit, ASTNode node, ReferenceUpdater referenceUpdater, ASTMatcher matcher) throws RefactoringException { if (node instanceof Name) { return new NameRegionUpdater(originalEdit, referenceUpdater, (Name) node, matcher); } else if (node instanceof TextElement) { return new TextElementRegionUpdater(originalEdit, referenceUpdater, (TextElement) node, matcher); } throw new RefactoringException("This AST node type is not supported"); }
private TagElement getDelegateJavadocTag(BodyDeclaration declaration) throws JavaModelException { Assert.isNotNull(declaration); String msg= RefactoringCoreMessages.DelegateCreator_use_member_instead; int firstParam= msg.indexOf("{0}"); //$NON-NLS-1$ Assert.isTrue(firstParam != -1); List<ASTNode> fragments= new ArrayList<ASTNode>(); TextElement text= getAst().newTextElement(); text.setText(msg.substring(0, firstParam).trim()); fragments.add(text); fragments.add(createJavadocMemberReferenceTag(declaration, getAst())); text= getAst().newTextElement(); text.setText(msg.substring(firstParam + 3).trim()); fragments.add(text); final TagElement tag= getAst().newTagElement(); tag.setTagName(TagElement.TAG_DEPRECATED); tag.fragments().addAll(fragments); return tag; }
@SuppressWarnings("unchecked") public BodyDeclaration createComment(AST ast, BodyDeclaration bodyDeclaration, String comment){ final Javadoc javadoc= ast.newJavadoc(); final TextElement textElement=ast.newTextElement(); textElement.setText(comment); final TagElement tagElement=ast.newTagElement(); tagElement.fragments().add(textElement); javadoc.tags().add(tagElement); bodyDeclaration.setJavadoc(javadoc); return bodyDeclaration; }
private TagElement createReturnTag() { TagElement returnNode = getASTRewrite().getAST().newTagElement(); returnNode.setTagName(TagElement.TAG_RETURN); TextElement textElement = getASTRewrite().getAST().newTextElement(); String text = StubUtility.getTodoTaskTag(fCuRewrite.getCu().getJavaProject()); if (text != null) textElement.setText(text); // TODO: use template with {@todo} ... returnNode.fragments().add(textElement); return returnNode; }
private TagElement createExceptionTag(String simpleName) { TagElement excptNode = getASTRewrite().getAST().newTagElement(); excptNode.setTagName(TagElement.TAG_THROWS); SimpleName nameNode = getASTRewrite().getAST().newSimpleName(simpleName); excptNode.fragments().add(nameNode); TextElement textElement = getASTRewrite().getAST().newTextElement(); String text = StubUtility.getTodoTaskTag(fCuRewrite.getCu().getJavaProject()); if (text != null) textElement.setText(text); // TODO: use template with {@todo} ... excptNode.fragments().add(textElement); return excptNode; }
public static TagElement createParamTag(String parameterName, AST ast, IJavaProject javaProject) { TagElement paramNode = ast.newTagElement(); paramNode.setTagName(TagElement.TAG_PARAM); SimpleName simpleName = ast.newSimpleName(parameterName); paramNode.fragments().add(simpleName); TextElement textElement = ast.newTextElement(); String text = StubUtility.getTodoTaskTag(javaProject); if (text != null) textElement.setText(text); // TODO: use template with {@todo} ... paramNode.fragments().add(textElement); return paramNode; }
@Override public boolean match(TextElement node, Object other) { if (!(other instanceof TextElement)) { return false; } TextElement o = (TextElement) other; return safeRenamedEquals(node.getText(), o.getText(), oldName, newName); }
@Override protected ReplaceEdit createUpdatedEditForEquivalentNode( TextElement equivalentNode) throws RefactoringException { // Adjust the offset based on the difference between start positions of // equivalent nodes int newOffset = getOriginalEdit().getOffset() + equivalentNode.getStartPosition() - getOriginalNode().getStartPosition(); return new ReplaceEdit(newOffset, getOriginalEdit().getLength(), getOriginalEdit().getText()); }
private TagElement createReturnTag() { TagElement returnNode= getASTRewrite().getAST().newTagElement(); returnNode.setTagName(TagElement.TAG_RETURN); TextElement textElement= getASTRewrite().getAST().newTextElement(); String text= StubUtility.getTodoTaskTag(fCuRewrite.getCu().getJavaProject()); if (text != null) textElement.setText(text); //TODO: use template with {@todo} ... returnNode.fragments().add(textElement); return returnNode; }
private TagElement createExceptionTag(String simpleName) { TagElement excptNode= getASTRewrite().getAST().newTagElement(); excptNode.setTagName(TagElement.TAG_THROWS); SimpleName nameNode= getASTRewrite().getAST().newSimpleName(simpleName); excptNode.fragments().add(nameNode); TextElement textElement= getASTRewrite().getAST().newTextElement(); String text= StubUtility.getTodoTaskTag(fCuRewrite.getCu().getJavaProject()); if (text != null) textElement.setText(text); //TODO: use template with {@todo} ... excptNode.fragments().add(textElement); return excptNode; }
public static TagElement createParamTag(String parameterName, AST ast, IJavaProject javaProject) { TagElement paramNode= ast.newTagElement(); paramNode.setTagName(TagElement.TAG_PARAM); SimpleName simpleName= ast.newSimpleName(parameterName); paramNode.fragments().add(simpleName); TextElement textElement= ast.newTextElement(); String text= StubUtility.getTodoTaskTag(javaProject); if (text != null) textElement.setText(text); //TODO: use template with {@todo} ... paramNode.fragments().add(textElement); return paramNode; }
private static boolean isWhitespaceTextElement(Object fragment) { if (!(fragment instanceof TextElement)) return false; TextElement textElement= (TextElement) fragment; return textElement.getText().trim().length() == 0; }
@Override public boolean visit(TagElement node) { if (node.isNested()) { // nested tags are always enclosed in braces this.fBuffer.append("{");//$NON-NLS-1$ } else { // top-level tags always begin on a new line this.fBuffer.append("\n * ");//$NON-NLS-1$ } boolean previousRequiresWhiteSpace= false; if (node.getTagName() != null) { this.fBuffer.append(node.getTagName()); previousRequiresWhiteSpace= true; } boolean previousRequiresNewLine= false; for (Iterator<? extends ASTNode> it= node.fragments().iterator(); it.hasNext();) { ASTNode e= it.next(); // assume text elements include necessary leading and trailing whitespace // but Name, MemberRef, MethodRef, and nested TagElement do not include white space boolean currentIncludesWhiteSpace= (e instanceof TextElement); if (previousRequiresNewLine && currentIncludesWhiteSpace) { this.fBuffer.append("\n * ");//$NON-NLS-1$ } previousRequiresNewLine= currentIncludesWhiteSpace; // add space if required to separate if (previousRequiresWhiteSpace && !currentIncludesWhiteSpace) { this.fBuffer.append(" "); //$NON-NLS-1$ } e.accept(this); previousRequiresWhiteSpace= !currentIncludesWhiteSpace && !(e instanceof TagElement); } if (node.isNested()) { this.fBuffer.append("}");//$NON-NLS-1$ } return false; }
public boolean visit(TagElement node) { if (node.isNested()) { // nested tags are always enclosed in braces this.buffer.append("{");//$NON-NLS-1$ } else { // top-level tags always begin on a new line this.buffer.append("\n * ");//$NON-NLS-1$ } boolean previousRequiresWhiteSpace = false; if (node.getTagName() != null) { this.buffer.append(node.getTagName()); previousRequiresWhiteSpace = true; } boolean previousRequiresNewLine = false; for (Iterator it = node.fragments().iterator(); it.hasNext(); ) { ASTNode e = (ASTNode) it.next(); // assume text elements include necessary leading and trailing whitespace // but Name, MemberRef, MethodRef, and nested TagElement do not include white space boolean currentIncludesWhiteSpace = (e instanceof TextElement); if (previousRequiresNewLine && currentIncludesWhiteSpace) { this.buffer.append("\n * ");//$NON-NLS-1$ } previousRequiresNewLine = currentIncludesWhiteSpace; // add space if required to separate if (previousRequiresWhiteSpace && !currentIncludesWhiteSpace) { this.buffer.append(" "); //$NON-NLS-1$ } e.accept(this); previousRequiresWhiteSpace = !currentIncludesWhiteSpace && !(e instanceof TagElement); } if (node.isNested()) { this.buffer.append("}");//$NON-NLS-1$ } return false; }
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(); }
/** * Return if a javadoc comment is generated or not */ private boolean isJavadocCommentGenerated(Javadoc jd) { if (jd == null) { throw new IllegalArgumentException(); } boolean found = false; List<TagElement> tags = jd.tags(); if (tags != null) { TagElement te = null; for (int inc = 0; inc < tags.size() && !found; inc++) { te = tags.get(inc); if ("@generated".equals(te.getTagName())) { List<ASTNode> fragments = te.fragments(); if (fragments != null && fragments.size() == 1) { if (fragments.get(0) instanceof TextElement) { String commentText = ((TextElement) fragments.get(0)).getText().trim(); found = commentText != null && commentText.startsWith(getGeneratorName()); } } } } } else { return false; } return found; }
public static void addMissingReturnTypeProposals(IInvocationContext context, IProblemLocation problem, Collection<CUCorrectionProposal> 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)); LinkedCorrectionProposal proposal= new LinkedCorrectionProposal(label, 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)); } } } }
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; }
private void insertTabStop(ASTRewrite rewriter, List<ASTNode> fragments, String linkedName) { TextElement textElement= rewriter.getAST().newTextElement(); textElement.setText(""); //$NON-NLS-1$ fragments.add(textElement); }
private void insertTabStop(ASTRewrite rewriter, List<ASTNode> fragments, String linkedName) { TextElement textElement = rewriter.getAST().newTextElement(); textElement.setText(""); // $NON-NLS-1$ fragments.add(textElement); addLinkedPosition(rewriter.track(textElement), false, linkedName); }
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; }
private static boolean isWhitespaceTextElement(Object fragment) { if (!(fragment instanceof TextElement)) return false; TextElement textElement = (TextElement) fragment; return textElement.getText().trim().length() == 0; }
public TextElementRegionUpdater(ReplaceEdit originalEdit, ReferenceUpdater referenceUpdater, TextElement originalNode, ASTMatcher matcher) throws RefactoringException { super(originalEdit, referenceUpdater, originalNode, matcher); }
/** {@inheritDoc} */ @Override public void endVisit(TextElement node) { logger.warn("Method endVisitTextElement for " + node + " for " + node + " not implemented!"); super.endVisit(node); }
/** {@inheritDoc} */ @Override public boolean visit(TextElement node) { logger.warn("Method visitTextElement for " + node + " not implemented!"); return super.visit(node); }