org.eclipse.jdt.core.dom.TextElement Java Examples

The following examples show how to use org.eclipse.jdt.core.dom.TextElement. 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: DelegateCreator.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
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<>();
	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;
}
 
Example #2
Source File: JavadocTagsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
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;
}
 
Example #3
Source File: JavadocTagsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
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));
		}
	}
}
 
Example #4
Source File: JavadocTagsSubProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
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));
		}
	}
}
 
Example #5
Source File: JavadocTagsSubProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
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;
}
 
Example #6
Source File: DelegateCreator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
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;
}
 
Example #7
Source File: RegionUpdaterFactory.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * 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");
}
 
Example #8
Source File: SService.java    From BIMserver with GNU Affero General Public License v3.0 5 votes vote down vote up
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();
}
 
Example #9
Source File: JavadocContentAccess2.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean isWhitespaceTextElement(Object fragment) {
	if (!(fragment instanceof TextElement))
		return false;
	
	TextElement textElement= (TextElement) fragment;
	return textElement.getText().trim().length() == 0;
}
 
Example #10
Source File: JavadocUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
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;
}
 
Example #11
Source File: ChangeSignatureProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
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;
}
 
Example #12
Source File: ChangeSignatureProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
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;
}
 
Example #13
Source File: TreedUtils.java    From compiler with Apache License 2.0 5 votes vote down vote up
public static String buildASTLabel(ASTNode node) {
	String label = node.getClass().getSimpleName();
	if (node instanceof Expression) {
		if (node.getClass().getSimpleName().endsWith("Literal")) {
			return label + "(" + node.toString() + ")";
		}
		int type = node.getNodeType();
		switch (type) {
		case ASTNode.INFIX_EXPRESSION:
			return label + "(" + ((InfixExpression) node).getOperator().toString() + ")";
		case ASTNode.SIMPLE_NAME:
			return label + "(" + node.toString() + ")";
		case ASTNode.POSTFIX_EXPRESSION:
			return label + "(" + ((PostfixExpression) node).getOperator().toString() + ")";
		case ASTNode.PREFIX_EXPRESSION:
			return label + "(" + ((PrefixExpression) node).getOperator().toString() + ")";
		default:
			break;
		}
	} else if (node instanceof Modifier) {
		return label + "(" + node.toString() + ")";
	} else if (node instanceof Type) {
		if (node instanceof PrimitiveType)
			return label + "(" + node.toString() + ")";
	} else if (node instanceof TextElement) {
		return label + "(" + node.toString() + ")";
	} else if (node instanceof TagElement) {
		String tag = ((TagElement) node).getTagName();
		if (tag == null)
			return label;
		return label + "(" + tag + ")";
	}
	return label;
}
 
Example #14
Source File: TreedUtils.java    From compiler with Apache License 2.0 5 votes vote down vote up
public static char buildLabelForVector(ASTNode node) {
	char label = (char) node.getNodeType();
	if (node instanceof Expression) {
		if (node.getClass().getSimpleName().endsWith("Literal")) {
			return (char) (label | (node.toString().hashCode() << 7));
		}
		int type = node.getNodeType();
		switch (type) {
		case ASTNode.INFIX_EXPRESSION:
			return (char) (label | (((InfixExpression) node).getOperator().toString().hashCode() << 7));
		case ASTNode.SIMPLE_NAME:
			return (char) (label | (node.toString().hashCode() << 7));
		case ASTNode.POSTFIX_EXPRESSION:
			return (char) (label | (((PostfixExpression) node).getOperator().toString().hashCode() << 7));
		case ASTNode.PREFIX_EXPRESSION:
			return (char) (label | (((PrefixExpression) node).getOperator().toString().hashCode() << 7));
		default:
			break;
		}
	} else if (node instanceof Modifier) {
		return (char) (label | (node.toString().hashCode() << 7));
	} else if (node instanceof Type) {
		if (node instanceof PrimitiveType)
			return (char) (label | (node.toString().hashCode() << 7));
	} else if (node instanceof TextElement) {
		return (char) (label | (node.toString().hashCode() << 7));
	} else if (node instanceof TagElement) {
		String tag = ((TagElement) node).getTagName();
		if (tag != null)
			return (char) (label | (((TagElement) node).getTagName().hashCode() << 7));
	}
	return label;
}
 
Example #15
Source File: RenamedElementAstMatcher.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
@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);
}
 
Example #16
Source File: JavaASTFlattener.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public boolean visit(final TagElement node) {
  boolean _isNested = node.isNested();
  if (_isNested) {
    this.appendToBuffer("{");
  } else {
    this.appendLineWrapToBuffer();
    this.appendToBuffer(" * ");
  }
  boolean previousRequiresWhiteSpace = false;
  String _tagName = node.getTagName();
  boolean _tripleNotEquals = (_tagName != null);
  if (_tripleNotEquals) {
    this.appendToBuffer(node.getTagName());
    previousRequiresWhiteSpace = true;
  }
  boolean previousRequiresNewLine = false;
  for (Iterator<? extends ASTNode> it = node.fragments().iterator(); it.hasNext();) {
    {
      ASTNode e = it.next();
      boolean currentIncludesWhiteSpace = (e instanceof TextElement);
      if ((previousRequiresNewLine && currentIncludesWhiteSpace)) {
        this.appendLineWrapToBuffer();
        this.appendToBuffer(" * ");
      }
      previousRequiresNewLine = currentIncludesWhiteSpace;
      if ((previousRequiresWhiteSpace && (!currentIncludesWhiteSpace))) {
        this.appendSpaceToBuffer();
      }
      e.accept(this);
      previousRequiresWhiteSpace = ((!currentIncludesWhiteSpace) && (!(e instanceof TagElement)));
    }
  }
  boolean _isNested_1 = node.isNested();
  if (_isNested_1) {
    this.appendToBuffer("}");
  }
  return false;
}
 
Example #17
Source File: JavadocContentAccess2.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static boolean isWhitespaceTextElement(Object fragment) {
	if (!(fragment instanceof TextElement)) {
		return false;
	}

	TextElement textElement = (TextElement) fragment;
	return textElement.getText().trim().length() == 0;
}
 
Example #18
Source File: TextElementRegionUpdater.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
@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());
}
 
Example #19
Source File: GenericVisitor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public boolean visit(TextElement node) {
	return visitNode(node);
}
 
Example #20
Source File: JavadocLineBreakPreparator.java    From spring-javaformat with Apache License 2.0 4 votes vote down vote up
@Override
public boolean visit(TextElement node) {
	this.hasText = true;
	return true;
}
 
Example #21
Source File: ChangeMethodSignatureProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
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);
}
 
Example #22
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 #23
Source File: JavadocTagsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
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);
}
 
Example #24
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 #25
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 #26
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 #27
Source File: GenericVisitor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public void endVisit(TextElement node) {
	endVisitNode(node);
}
 
Example #28
Source File: JavadocTagsSubProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private void insertTabStop(ASTRewrite rewriter, List<ASTNode> fragments, String linkedName) {
	TextElement textElement= rewriter.getAST().newTextElement();
	textElement.setText(""); //$NON-NLS-1$
	fragments.add(textElement);
}
 
Example #29
Source File: AstMatchingNodeFinder.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public boolean visit(TextElement node) {
	if (node.subtreeMatch(fMatcher, fNodeToMatch))
		return matches(node);
	return super.visit(node);
}
 
Example #30
Source File: JavaASTFlattener.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public boolean visit(final TextElement node) {
  this.appendToBuffer(node.getText());
  return false;
}