use of org.eclipse.jdt.core.dom.Javadoc in project che by eclipse.
the class StubUtility2 method createConstructorStub.
/* This method should work with all AST levels. */
public static MethodDeclaration createConstructorStub(ICompilationUnit unit, ASTRewrite rewrite, ImportRewrite imports, ImportRewriteContext context, IMethodBinding binding, String type, int modifiers, boolean omitSuperForDefConst, boolean todo, CodeGenerationSettings settings) throws CoreException {
AST ast = rewrite.getAST();
MethodDeclaration decl = ast.newMethodDeclaration();
decl.modifiers().addAll(ASTNodeFactory.newModifiers(ast, modifiers & ~Modifier.ABSTRACT & ~Modifier.NATIVE));
decl.setName(ast.newSimpleName(type));
decl.setConstructor(true);
createTypeParameters(imports, context, ast, binding, decl);
List<SingleVariableDeclaration> parameters = createParameters(unit.getJavaProject(), imports, context, ast, binding, null, decl);
createThrownExceptions(decl, binding, imports, context, ast);
Block body = ast.newBlock();
decl.setBody(body);
String delimiter = StubUtility.getLineDelimiterUsed(unit);
//$NON-NLS-1$
String bodyStatement = "";
if (!omitSuperForDefConst || !parameters.isEmpty()) {
SuperConstructorInvocation invocation = ast.newSuperConstructorInvocation();
SingleVariableDeclaration varDecl = null;
for (Iterator<SingleVariableDeclaration> iterator = parameters.iterator(); iterator.hasNext(); ) {
varDecl = iterator.next();
invocation.arguments().add(ast.newSimpleName(varDecl.getName().getIdentifier()));
}
bodyStatement = ASTNodes.asFormattedString(invocation, 0, delimiter, unit.getJavaProject().getOptions(true));
}
if (todo) {
String placeHolder = CodeGeneration.getMethodBodyContent(unit, type, binding.getName(), true, bodyStatement, delimiter);
if (placeHolder != null) {
ReturnStatement todoNode = (ReturnStatement) rewrite.createStringPlaceholder(placeHolder, ASTNode.RETURN_STATEMENT);
body.statements().add(todoNode);
}
} else {
ReturnStatement statementNode = (ReturnStatement) rewrite.createStringPlaceholder(bodyStatement, ASTNode.RETURN_STATEMENT);
body.statements().add(statementNode);
}
if (settings != null && settings.createComments) {
String string = CodeGeneration.getMethodComment(unit, type, decl, binding, delimiter);
if (string != null) {
Javadoc javadoc = (Javadoc) rewrite.createStringPlaceholder(string, ASTNode.JAVADOC);
decl.setJavadoc(javadoc);
}
}
return decl;
}
use of org.eclipse.jdt.core.dom.Javadoc in project che by eclipse.
the class StubUtility2 method createImplementationStub.
public static MethodDeclaration createImplementationStub(ICompilationUnit unit, ASTRewrite rewrite, ImportRewrite imports, ImportRewriteContext context, IMethodBinding binding, String[] parameterNames, String type, CodeGenerationSettings settings, boolean inInterface) throws CoreException {
Assert.isNotNull(imports);
Assert.isNotNull(rewrite);
AST ast = rewrite.getAST();
MethodDeclaration decl = ast.newMethodDeclaration();
decl.modifiers().addAll(getImplementationModifiers(ast, binding, inInterface, imports, context));
decl.setName(ast.newSimpleName(binding.getName()));
decl.setConstructor(false);
ITypeBinding bindingReturnType = binding.getReturnType();
if (bindingReturnType.isWildcardType()) {
ITypeBinding bound = bindingReturnType.getBound();
bindingReturnType = (bound != null) ? bound : bindingReturnType.getErasure();
}
IJavaProject javaProject = unit.getJavaProject();
if (JavaModelUtil.is50OrHigher(javaProject)) {
createTypeParameters(imports, context, ast, binding, decl);
} else {
bindingReturnType = bindingReturnType.getErasure();
}
decl.setReturnType2(imports.addImport(bindingReturnType, ast, context));
List<SingleVariableDeclaration> parameters = createParameters(javaProject, imports, context, ast, binding, parameterNames, decl);
createThrownExceptions(decl, binding, imports, context, ast);
String delimiter = unit.findRecommendedLineSeparator();
int modifiers = binding.getModifiers();
if (!(inInterface && Modifier.isAbstract(modifiers))) {
// generate a method body
Map<String, String> options = javaProject.getOptions(true);
Block body = ast.newBlock();
decl.setBody(body);
//$NON-NLS-1$
String bodyStatement = "";
if (Modifier.isAbstract(modifiers)) {
Expression expression = ASTNodeFactory.newDefaultExpression(ast, decl.getReturnType2(), decl.getExtraDimensions());
if (expression != null) {
ReturnStatement returnStatement = ast.newReturnStatement();
returnStatement.setExpression(expression);
bodyStatement = ASTNodes.asFormattedString(returnStatement, 0, delimiter, options);
}
} else {
SuperMethodInvocation invocation = ast.newSuperMethodInvocation();
ITypeBinding declaringType = binding.getDeclaringClass();
if (declaringType.isInterface()) {
String qualifier = imports.addImport(declaringType.getErasure(), context);
Name name = ASTNodeFactory.newName(ast, qualifier);
invocation.setQualifier(name);
}
invocation.setName(ast.newSimpleName(binding.getName()));
SingleVariableDeclaration varDecl = null;
for (Iterator<SingleVariableDeclaration> iterator = parameters.iterator(); iterator.hasNext(); ) {
varDecl = iterator.next();
invocation.arguments().add(ast.newSimpleName(varDecl.getName().getIdentifier()));
}
Expression expression = invocation;
Type returnType = decl.getReturnType2();
if (returnType instanceof PrimitiveType && ((PrimitiveType) returnType).getPrimitiveTypeCode().equals(PrimitiveType.VOID)) {
bodyStatement = ASTNodes.asFormattedString(ast.newExpressionStatement(expression), 0, delimiter, options);
} else {
ReturnStatement returnStatement = ast.newReturnStatement();
returnStatement.setExpression(expression);
bodyStatement = ASTNodes.asFormattedString(returnStatement, 0, delimiter, options);
}
}
String placeHolder = CodeGeneration.getMethodBodyContent(unit, type, binding.getName(), false, bodyStatement, delimiter);
if (placeHolder != null) {
ReturnStatement todoNode = (ReturnStatement) rewrite.createStringPlaceholder(placeHolder, ASTNode.RETURN_STATEMENT);
body.statements().add(todoNode);
}
}
if (settings != null && settings.createComments) {
String string = CodeGeneration.getMethodComment(unit, type, decl, binding, delimiter);
if (string != null) {
Javadoc javadoc = (Javadoc) rewrite.createStringPlaceholder(string, ASTNode.JAVADOC);
decl.setJavadoc(javadoc);
}
}
if (settings != null && settings.overrideAnnotation && JavaModelUtil.is50OrHigher(javaProject)) {
addOverrideAnnotation(javaProject, rewrite, decl, binding);
}
return decl;
}
use of org.eclipse.jdt.core.dom.Javadoc in project che by eclipse.
the class ChangeMethodSignatureProposal method modifyParameters.
private void modifyParameters(ASTRewrite rewrite, MethodDeclaration methodDecl) {
AST ast = methodDecl.getAST();
ArrayList<String> usedNames = new ArrayList<String>();
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));
//$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);
//$NON-NLS-1$
insertTabStop(rewrite, newTagElement.fragments(), "param_tagcomment" + i);
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);
rewrite.replace(decl.getType(), newType, null);
DimensionRewrite.removeAllChildren(decl, SingleVariableDeclaration.EXTRA_DIMENSIONS2_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.Javadoc in project che by eclipse.
the class AbstractMethodCorrectionProposal method getStub.
private MethodDeclaration getStub(ASTRewrite rewrite, ASTNode targetTypeDecl) throws CoreException {
AST ast = targetTypeDecl.getAST();
MethodDeclaration decl = ast.newMethodDeclaration();
SimpleName newNameNode = getNewName(rewrite);
decl.setConstructor(isConstructor());
addNewModifiers(rewrite, targetTypeDecl, decl.modifiers());
ArrayList<String> takenNames = new ArrayList<String>();
addNewTypeParameters(rewrite, takenNames, decl.typeParameters());
decl.setName(newNameNode);
IVariableBinding[] declaredFields = fSenderBinding.getDeclaredFields();
for (int i = 0; i < declaredFields.length; i++) {
// avoid to take parameter names that are equal to field names
takenNames.add(declaredFields[i].getName());
}
//$NON-NLS-1$
String bodyStatement = "";
if (!isConstructor()) {
Type returnType = getNewMethodType(rewrite);
decl.setReturnType2(returnType);
boolean isVoid = returnType instanceof PrimitiveType && PrimitiveType.VOID.equals(((PrimitiveType) returnType).getPrimitiveTypeCode());
if (!fSenderBinding.isInterface() && !isVoid) {
ReturnStatement returnStatement = ast.newReturnStatement();
returnStatement.setExpression(ASTNodeFactory.newDefaultExpression(ast, returnType, 0));
bodyStatement = ASTNodes.asFormattedString(returnStatement, 0, String.valueOf('\n'), getCompilationUnit().getJavaProject().getOptions(true));
}
}
addNewParameters(rewrite, takenNames, decl.parameters());
addNewExceptions(rewrite, decl.thrownExceptionTypes());
Block body = null;
if (!fSenderBinding.isInterface()) {
body = ast.newBlock();
String placeHolder = CodeGeneration.getMethodBodyContent(getCompilationUnit(), fSenderBinding.getName(), newNameNode.getIdentifier(), isConstructor(), bodyStatement, String.valueOf('\n'));
if (placeHolder != null) {
ReturnStatement todoNode = (ReturnStatement) rewrite.createStringPlaceholder(placeHolder, ASTNode.RETURN_STATEMENT);
body.statements().add(todoNode);
}
}
decl.setBody(body);
CodeGenerationSettings settings = JavaPreferencesSettings.getCodeGenerationSettings(getCompilationUnit().getJavaProject());
if (settings.createComments && !fSenderBinding.isAnonymous()) {
String string = CodeGeneration.getMethodComment(getCompilationUnit(), fSenderBinding.getName(), decl, null, String.valueOf('\n'));
if (string != null) {
Javadoc javadoc = (Javadoc) rewrite.createStringPlaceholder(string, ASTNode.JAVADOC);
decl.setJavadoc(javadoc);
}
}
return decl;
}
use of org.eclipse.jdt.core.dom.Javadoc in project che by eclipse.
the class ReturnTypeSubProcessor method addVoidMethodReturnsProposals.
public static void addVoidMethodReturnsProposals(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 && selectedNode.getNodeType() == ASTNode.RETURN_STATEMENT) {
ReturnStatement returnStatement = (ReturnStatement) selectedNode;
Expression expr = returnStatement.getExpression();
if (expr != null) {
AST ast = astRoot.getAST();
ITypeBinding binding = Bindings.normalizeTypeBinding(expr.resolveTypeBinding());
if (binding == null) {
//$NON-NLS-1$
binding = ast.resolveWellKnownType("java.lang.Object");
}
if (binding.isWildcardType()) {
binding = ASTResolving.normalizeWildcardType(binding, true, ast);
}
MethodDeclaration methodDeclaration = (MethodDeclaration) decl;
ASTRewrite rewrite = ASTRewrite.create(ast);
String label = Messages.format(CorrectionMessages.ReturnTypeSubProcessor_voidmethodreturns_description, BindingLabelProvider.getBindingLabel(binding, BindingLabelProvider.DEFAULT_TEXTFLAGS));
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, cu, rewrite, IProposalRelevance.VOID_METHOD_RETURNS, image);
ImportRewrite imports = proposal.createImportRewrite(astRoot);
ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(methodDeclaration, imports);
Type newReturnType = imports.addImport(binding, ast, importRewriteContext);
if (methodDeclaration.isConstructor()) {
rewrite.set(methodDeclaration, MethodDeclaration.CONSTRUCTOR_PROPERTY, Boolean.FALSE, null);
rewrite.set(methodDeclaration, MethodDeclaration.RETURN_TYPE2_PROPERTY, newReturnType, null);
} else {
rewrite.replace(methodDeclaration.getReturnType2(), newReturnType, null);
}
//$NON-NLS-1$
String key = "return_type";
proposal.addLinkedPosition(rewrite.track(newReturnType), true, key);
ITypeBinding[] bindings = ASTResolving.getRelaxingTypes(ast, binding);
for (int i = 0; i < bindings.length; i++) {
proposal.addLinkedPositionProposal(key, bindings[i]);
}
Javadoc javadoc = methodDeclaration.getJavadoc();
if (javadoc != 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);
//$NON-NLS-1$
proposal.addLinkedPosition(rewrite.track(commentStart), false, "comment_start");
}
proposals.add(proposal);
}
ASTRewrite rewrite = ASTRewrite.create(decl.getAST());
rewrite.remove(returnStatement.getExpression(), null);
String label = CorrectionMessages.ReturnTypeSubProcessor_removereturn_description;
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.CHANGE_TO_RETURN, image);
proposals.add(proposal);
}
}
Aggregations