use of org.eclipse.jdt.core.dom.TagElement in project eclipse.jdt.ls by eclipse.
the class NewVariableCorrectionProposal method doAddParam.
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;
}
use of org.eclipse.jdt.core.dom.TagElement in project eclipse.jdt.ls by eclipse.
the class AddTypeParameterProposal method getRewrite.
@Override
protected ASTRewrite getRewrite() throws CoreException {
ASTNode boundNode = fAstRoot.findDeclaringNode(fBinding);
ASTNode declNode = null;
if (boundNode != null) {
// is same CU
declNode = boundNode;
createImportRewrite(fAstRoot);
} else {
CompilationUnit newRoot = ASTResolving.createQuickFixAST(getCompilationUnit(), null);
declNode = newRoot.findDeclaringNode(fBinding.getKey());
createImportRewrite(newRoot);
}
AST ast = declNode.getAST();
TypeParameter newTypeParam = ast.newTypeParameter();
newTypeParam.setName(ast.newSimpleName(fTypeParamName));
if (fBounds != null && fBounds.length > 0) {
List<Type> typeBounds = newTypeParam.typeBounds();
ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(declNode, getImportRewrite());
for (int i = 0; i < fBounds.length; i++) {
Type newBound = getImportRewrite().addImport(fBounds[i], ast, importRewriteContext, TypeLocation.TYPE_BOUND);
typeBounds.add(newBound);
}
}
ASTRewrite rewrite = ASTRewrite.create(ast);
ListRewrite listRewrite;
Javadoc javadoc;
List<TypeParameter> otherTypeParams;
if (declNode instanceof TypeDeclaration) {
TypeDeclaration declaration = (TypeDeclaration) declNode;
listRewrite = rewrite.getListRewrite(declaration, TypeDeclaration.TYPE_PARAMETERS_PROPERTY);
otherTypeParams = declaration.typeParameters();
javadoc = declaration.getJavadoc();
} else {
MethodDeclaration declaration = (MethodDeclaration) declNode;
listRewrite = rewrite.getListRewrite(declNode, MethodDeclaration.TYPE_PARAMETERS_PROPERTY);
otherTypeParams = declaration.typeParameters();
javadoc = declaration.getJavadoc();
}
listRewrite.insertLast(newTypeParam, null);
if (javadoc != null && otherTypeParams != null) {
ListRewrite tagsRewriter = rewrite.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY);
Set<String> previousNames = JavadocTagsSubProcessor.getPreviousTypeParamNames(otherTypeParams, null);
String name = '<' + fTypeParamName + '>';
TagElement newTag = ast.newTagElement();
newTag.setTagName(TagElement.TAG_PARAM);
TextElement text = ast.newTextElement();
text.setText(name);
newTag.fragments().add(text);
JavadocTagsSubProcessor.insertTag(tagsRewriter, newTag, previousNames);
}
return rewrite;
}
use of org.eclipse.jdt.core.dom.TagElement in project eclipse.jdt.ls by eclipse.
the class ChangeMethodSignatureProposal method modifyParameters.
private void modifyParameters(ASTRewrite rewrite, MethodDeclaration methodDecl) {
AST ast = methodDecl.getAST();
ArrayList<String> usedNames = new ArrayList<>();
boolean hasCreatedVariables = false;
IVariableBinding[] declaredFields = fSenderBinding.getDeclaringClass().getDeclaredFields();
for (int i = 0; i < declaredFields.length; i++) {
// avoid to take parameter names that are equal to field names
usedNames.add(declaredFields[i].getName());
}
ImportRewrite imports = getImportRewrite();
ImportRewriteContext context = new ContextSensitiveImportRewriteContext(methodDecl, imports);
ListRewrite listRewrite = rewrite.getListRewrite(methodDecl, MethodDeclaration.PARAMETERS_PROPERTY);
// old parameters
List<SingleVariableDeclaration> parameters = methodDecl.parameters();
// index over the oldParameters
int k = 0;
for (int i = 0; i < fParameterChanges.length; i++) {
ChangeDescription curr = fParameterChanges[i];
if (curr == null) {
SingleVariableDeclaration oldParam = parameters.get(k);
usedNames.add(oldParam.getName().getIdentifier());
k++;
} else if (curr instanceof InsertDescription) {
InsertDescription desc = (InsertDescription) curr;
SingleVariableDeclaration newNode = ast.newSingleVariableDeclaration();
newNode.setType(imports.addImport(desc.type, ast, context, TypeLocation.PARAMETER));
// $NON-NLS-1$
newNode.setName(ast.newSimpleName("x"));
// remember to set name later
desc.resultingParamName = new SimpleName[] { newNode.getName() };
desc.resultingParamType = newNode.getType();
hasCreatedVariables = true;
listRewrite.insertAt(newNode, i, null);
Javadoc javadoc = methodDecl.getJavadoc();
if (javadoc != null) {
TagElement newTagElement = ast.newTagElement();
newTagElement.setTagName(TagElement.TAG_PARAM);
// $NON-NLS-1$
SimpleName arg = ast.newSimpleName("x");
newTagElement.fragments().add(arg);
insertParamTag(rewrite.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY), parameters, k, newTagElement);
// set the name later
desc.resultingTagArg = arg;
} else {
desc.resultingTagArg = null;
}
} else if (curr instanceof RemoveDescription) {
SingleVariableDeclaration decl = parameters.get(k);
listRewrite.remove(decl, null);
k++;
TagElement tagNode = findParamTag(methodDecl, decl);
if (tagNode != null) {
rewrite.remove(tagNode, null);
}
} else if (curr instanceof EditDescription) {
EditDescription desc = (EditDescription) curr;
ITypeBinding newTypeBinding = desc.type;
SingleVariableDeclaration decl = parameters.get(k);
if (k == parameters.size() - 1 && i == fParameterChanges.length - 1 && decl.isVarargs() && newTypeBinding.isArray()) {
// stick with varargs if it was before
newTypeBinding = newTypeBinding.getElementType();
} else {
rewrite.set(decl, SingleVariableDeclaration.VARARGS_PROPERTY, Boolean.FALSE, null);
}
Type newType = imports.addImport(newTypeBinding, ast, context, TypeLocation.PARAMETER);
rewrite.replace(decl.getType(), newType, null);
DimensionRewrite.removeAllChildren(decl, SingleVariableDeclaration.EXTRA_DIMENSIONS2_PROPERTY, rewrite, null);
TypeAnnotationRewrite.removePureTypeAnnotations(decl, SingleVariableDeclaration.MODIFIERS2_PROPERTY, rewrite, null);
IBinding binding = decl.getName().resolveBinding();
if (binding != null) {
SimpleName[] names = LinkedNodeFinder.findByBinding(decl.getRoot(), binding);
SimpleName[] newNames = new SimpleName[names.length];
for (int j = 0; j < names.length; j++) {
// $NON-NLS-1$ // name will be set later
SimpleName newName = ast.newSimpleName("x");
newNames[j] = newName;
rewrite.replace(names[j], newName, null);
}
desc.resultingParamName = newNames;
} else {
// $NON-NLS-1$ // name will be set later
SimpleName newName = ast.newSimpleName("x");
rewrite.replace(decl.getName(), newName, null);
// remember to set name later
desc.resultingParamName = new SimpleName[] { newName };
}
desc.resultingParamType = newType;
desc.orginalName = decl.getName().getIdentifier();
hasCreatedVariables = true;
k++;
TagElement tagNode = findParamTag(methodDecl, decl);
if (tagNode != null) {
List<? extends ASTNode> fragments = tagNode.fragments();
if (!fragments.isEmpty()) {
// $NON-NLS-1$
SimpleName arg = ast.newSimpleName("x");
rewrite.replace(fragments.get(0), arg, null);
desc.resultingTagArg = arg;
}
}
} else if (curr instanceof SwapDescription) {
SingleVariableDeclaration decl1 = parameters.get(k);
SingleVariableDeclaration decl2 = parameters.get(((SwapDescription) curr).index);
rewrite.replace(decl1, rewrite.createCopyTarget(decl2), null);
rewrite.replace(decl2, rewrite.createCopyTarget(decl1), null);
usedNames.add(decl1.getName().getIdentifier());
k++;
TagElement tagNode1 = findParamTag(methodDecl, decl1);
TagElement tagNode2 = findParamTag(methodDecl, decl2);
if (tagNode1 != null && tagNode2 != null) {
rewrite.replace(tagNode1, rewrite.createCopyTarget(tagNode2), null);
rewrite.replace(tagNode2, rewrite.createCopyTarget(tagNode1), null);
}
}
}
if (!hasCreatedVariables) {
return;
}
if (methodDecl.getBody() != null) {
// avoid take a name of a local variable inside
CompilationUnit root = (CompilationUnit) methodDecl.getRoot();
IBinding[] bindings = (new ScopeAnalyzer(root)).getDeclarationsAfter(methodDecl.getBody().getStartPosition(), ScopeAnalyzer.VARIABLES);
for (int i = 0; i < bindings.length; i++) {
usedNames.add(bindings[i].getName());
}
}
fixupNames(rewrite, usedNames);
}
use of org.eclipse.jdt.core.dom.TagElement in project eclipse.jdt.ls by eclipse.
the class ChangeMethodSignatureProposal method modifyExceptions.
private void modifyExceptions(ASTRewrite rewrite, MethodDeclaration methodDecl) {
AST ast = methodDecl.getAST();
ImportRewrite imports = getImportRewrite();
ImportRewriteContext context = new ContextSensitiveImportRewriteContext(methodDecl, imports);
ListRewrite listRewrite = rewrite.getListRewrite(methodDecl, MethodDeclaration.THROWN_EXCEPTION_TYPES_PROPERTY);
// old exceptions
List<Type> exceptions = methodDecl.thrownExceptionTypes();
// index over the old exceptions
int k = 0;
for (int i = 0; i < fExceptionChanges.length; i++) {
ChangeDescription curr = fExceptionChanges[i];
if (curr == null) {
k++;
} else if (curr instanceof InsertDescription) {
InsertDescription desc = (InsertDescription) curr;
String type = imports.addImport(desc.type, context);
ASTNode newNode = imports.addImport(desc.type, ast, context, TypeLocation.EXCEPTION);
listRewrite.insertAt(newNode, i, null);
String key = getExceptionTypeGroupId(i);
Javadoc javadoc = methodDecl.getJavadoc();
if (javadoc != null && JavadocTagsSubProcessor.findThrowsTag(javadoc, type) == null) {
TagElement newTagElement = ast.newTagElement();
newTagElement.setTagName(TagElement.TAG_THROWS);
ASTNode newRef = ASTNodeFactory.newName(ast, type);
newTagElement.fragments().add(newRef);
insertThrowsTag(rewrite.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY), exceptions, k, newTagElement);
}
} else if (curr instanceof RemoveDescription) {
Type node = exceptions.get(k);
listRewrite.remove(node, null);
k++;
TagElement tagNode = findThrowsTag(methodDecl, node);
if (tagNode != null) {
rewrite.remove(tagNode, null);
}
} else if (curr instanceof EditDescription) {
EditDescription desc = (EditDescription) curr;
Type oldNode = exceptions.get(k);
String type = imports.addImport(desc.type, context);
ASTNode newNode = imports.addImport(desc.type, ast, context, TypeLocation.EXCEPTION);
listRewrite.replace(oldNode, newNode, null);
k++;
TagElement tagNode = findThrowsTag(methodDecl, oldNode);
if (tagNode != null) {
ASTNode newRef = ASTNodeFactory.newType(ast, type);
rewrite.replace((ASTNode) tagNode.fragments().get(0), newRef, null);
}
} else if (curr instanceof SwapDescription) {
Type decl1 = exceptions.get(k);
Type decl2 = exceptions.get(((SwapDescription) curr).index);
rewrite.replace(decl1, rewrite.createCopyTarget(decl2), null);
rewrite.replace(decl2, rewrite.createCopyTarget(decl1), null);
k++;
TagElement tagNode1 = findThrowsTag(methodDecl, decl1);
TagElement tagNode2 = findThrowsTag(methodDecl, decl2);
if (tagNode1 != null && tagNode2 != null) {
rewrite.replace(tagNode1, rewrite.createCopyTarget(tagNode2), null);
rewrite.replace(tagNode2, rewrite.createCopyTarget(tagNode1), null);
}
}
}
}
use of org.eclipse.jdt.core.dom.TagElement in project BIMserver by opensourceBIM.
the class SService method extractJavaDoc.
private void extractJavaDoc(Class<?> clazz) {
ASTParser parser = ASTParser.newParser(AST.JLS4);
parser.setSource(sourceCodeFetcher.get(clazz).toCharArray());
parser.setKind(ASTParser.K_COMPILATION_UNIT);
final CompilationUnit cu = (CompilationUnit) parser.createAST(null);
cu.accept(new ASTVisitor() {
MethodDeclaration currentMethod = null;
public boolean visit(Javadoc javaDoc) {
if (currentMethod != null) {
SMethod method = getSMethod(currentMethod.getName().getIdentifier());
if (method == null) {
LOGGER.error("Method " + currentMethod.getName().getIdentifier() + " not found in class");
} else {
for (Object tag : javaDoc.tags()) {
if (tag instanceof TagElement) {
TagElement tagElement = (TagElement) tag;
String tagName = tagElement.getTagName() == null ? null : tagElement.getTagName().trim();
if ("@param".equals(tagName)) {
SParameter parameter = null;
for (int i = 0; i < tagElement.fragments().size(); i++) {
Object fragment = tagElement.fragments().get(i);
if (i == 0 && fragment instanceof SimpleName) {
parameter = method.getParameter(((SimpleName) fragment).getIdentifier());
} else if (i == 1 && parameter != null && fragment instanceof TextElement) {
parameter.setDoc(((TextElement) fragment).getText());
}
}
} else if ("@return".equals(tagName)) {
method.setReturnDoc(extractFullText(tagElement));
} else if ("@throws".equals(tagName)) {
} else {
method.setDoc(extractFullText(tagElement));
}
}
}
}
}
return super.visit(javaDoc);
}
@Override
public boolean visit(MethodDeclaration node) {
currentMethod = node;
return super.visit(node);
}
@Override
public void endVisit(MethodDeclaration node) {
currentMethod = null;
super.endVisit(node);
}
});
}
Aggregations