use of org.eclipse.jdt.core.dom.Javadoc in project che by eclipse.
the class ModifierCorrectionSubProcessor method addOverridingDeprecatedMethodProposal.
public static void addOverridingDeprecatedMethodProposal(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
ICompilationUnit cu = context.getCompilationUnit();
ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
if (!(selectedNode instanceof MethodDeclaration)) {
return;
}
boolean is50OrHigher = JavaModelUtil.is50OrHigher(cu.getJavaProject());
MethodDeclaration methodDecl = (MethodDeclaration) selectedNode;
AST ast = methodDecl.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
if (is50OrHigher) {
Annotation annot = ast.newMarkerAnnotation();
//$NON-NLS-1$
annot.setTypeName(ast.newName("Deprecated"));
rewrite.getListRewrite(methodDecl, methodDecl.getModifiersProperty()).insertFirst(annot, null);
}
Javadoc javadoc = methodDecl.getJavadoc();
if (javadoc != null || !is50OrHigher) {
if (!is50OrHigher) {
javadoc = ast.newJavadoc();
rewrite.set(methodDecl, MethodDeclaration.JAVADOC_PROPERTY, javadoc, null);
}
TagElement newTag = ast.newTagElement();
newTag.setTagName(TagElement.TAG_DEPRECATED);
JavadocTagsSubProcessor.insertTag(rewrite.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY), newTag, null);
}
String label = CorrectionMessages.ModifierCorrectionSubProcessor_overrides_deprecated_description;
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.OVERRIDES_DEPRECATED, image);
proposals.add(proposal);
}
use of org.eclipse.jdt.core.dom.Javadoc in project che by eclipse.
the class SelfEncapsulateFieldRefactoring method createGetterMethod.
private MethodDeclaration createGetterMethod(AST ast, ASTRewrite rewriter, String lineDelimiter) throws CoreException {
FieldDeclaration field = (FieldDeclaration) ASTNodes.getParent(fFieldDeclaration, FieldDeclaration.class);
Type type = field.getType();
MethodDeclaration result = ast.newMethodDeclaration();
result.setName(ast.newSimpleName(fGetterName));
result.modifiers().addAll(ASTNodeFactory.newModifiers(ast, createModifiers()));
Type returnType = DimensionRewrite.copyTypeAndAddDimensions(type, fFieldDeclaration.extraDimensions(), rewriter);
result.setReturnType2(returnType);
Block block = ast.newBlock();
result.setBody(block);
String body = CodeGeneration.getGetterMethodBodyContent(fField.getCompilationUnit(), getTypeName(field.getParent()), fGetterName, fField.getElementName(), lineDelimiter);
if (body != null) {
ASTNode getterNode = rewriter.createStringPlaceholder(body, ASTNode.BLOCK);
block.statements().add(getterNode);
} else {
ReturnStatement rs = ast.newReturnStatement();
rs.setExpression(ast.newSimpleName(fField.getElementName()));
block.statements().add(rs);
}
if (fGenerateJavadoc) {
String string = CodeGeneration.getGetterComment(fField.getCompilationUnit(), getTypeName(field.getParent()), fGetterName, fField.getElementName(), ASTNodes.asString(type), StubUtility.getBaseName(fField), lineDelimiter);
if (string != null) {
Javadoc javadoc = (Javadoc) fRewriter.createStringPlaceholder(string, ASTNode.JAVADOC);
result.setJavadoc(javadoc);
}
}
return result;
}
use of org.eclipse.jdt.core.dom.Javadoc in project che by eclipse.
the class ConvertAnonymousToNestedRefactoring method createNewNestedClass.
private AbstractTypeDeclaration createNewNestedClass(CompilationUnitRewrite rewrite, ITypeBinding[] typeParameters) throws CoreException {
final AST ast = fAnonymousInnerClassNode.getAST();
final TypeDeclaration newDeclaration = ast.newTypeDeclaration();
newDeclaration.setInterface(false);
newDeclaration.setJavadoc(null);
newDeclaration.modifiers().addAll(ASTNodeFactory.newModifiers(ast, createModifiersForNestedClass()));
newDeclaration.setName(ast.newSimpleName(fClassName));
TypeParameter parameter = null;
for (int index = 0; index < typeParameters.length; index++) {
parameter = ast.newTypeParameter();
parameter.setName(ast.newSimpleName(typeParameters[index].getName()));
newDeclaration.typeParameters().add(parameter);
}
setSuperType(newDeclaration);
IJavaProject project = fCu.getJavaProject();
IVariableBinding[] bindings = getUsedLocalVariables();
ArrayList<String> fieldNames = new ArrayList<String>();
for (int i = 0; i < bindings.length; i++) {
String name = StubUtility.getBaseName(bindings[i], project);
String[] fieldNameProposals = StubUtility.getVariableNameSuggestions(NamingConventions.VK_INSTANCE_FIELD, project, name, 0, fieldNames, true);
fieldNames.add(fieldNameProposals[0]);
if (fLinkedProposalModel != null) {
LinkedProposalPositionGroup positionGroup = fLinkedProposalModel.getPositionGroup(KEY_FIELD_NAME_EXT + i, true);
for (int k = 0; k < fieldNameProposals.length; k++) {
positionGroup.addProposal(fieldNameProposals[k], null, fieldNameProposals.length - k);
}
}
}
String[] allFieldNames = fieldNames.toArray(new String[fieldNames.size()]);
List<BodyDeclaration> newBodyDeclarations = newDeclaration.bodyDeclarations();
createFieldsForAccessedLocals(rewrite, bindings, allFieldNames, newBodyDeclarations);
MethodDeclaration newConstructorDecl = createNewConstructor(rewrite, bindings, allFieldNames);
if (newConstructorDecl != null) {
newBodyDeclarations.add(newConstructorDecl);
}
updateAndMoveBodyDeclarations(rewrite, bindings, allFieldNames, newBodyDeclarations, newConstructorDecl);
if (doAddComments()) {
String[] parameterNames = new String[typeParameters.length];
for (int index = 0; index < parameterNames.length; index++) {
parameterNames[index] = typeParameters[index].getName();
}
String string = CodeGeneration.getTypeComment(rewrite.getCu(), fClassName, parameterNames, StubUtility.getLineDelimiterUsed(fCu));
if (string != null) {
Javadoc javadoc = (Javadoc) rewrite.getASTRewrite().createStringPlaceholder(string, ASTNode.JAVADOC);
newDeclaration.setJavadoc(javadoc);
}
}
if (fLinkedProposalModel != null) {
addLinkedPosition(KEY_TYPE_NAME, newDeclaration.getName(), rewrite.getASTRewrite(), false);
ModifierCorrectionSubProcessor.installLinkedVisibilityProposals(fLinkedProposalModel, rewrite.getASTRewrite(), newDeclaration.modifiers(), false);
}
return newDeclaration;
}
use of org.eclipse.jdt.core.dom.Javadoc in project che by eclipse.
the class ConvertAnonymousToNestedRefactoring method createNewConstructor.
private MethodDeclaration createNewConstructor(CompilationUnitRewrite rewrite, IVariableBinding[] bindings, String[] fieldNames) throws JavaModelException {
ClassInstanceCreation instanceCreation = (ClassInstanceCreation) fAnonymousInnerClassNode.getParent();
if (instanceCreation.arguments().isEmpty() && bindings.length == 0)
return null;
IJavaProject project = fCu.getJavaProject();
AST ast = rewrite.getAST();
ImportRewrite importRewrite = rewrite.getImportRewrite();
ASTRewrite astRewrite = rewrite.getASTRewrite();
MethodDeclaration newConstructor = ast.newMethodDeclaration();
newConstructor.setConstructor(true);
newConstructor.setJavadoc(null);
newConstructor.modifiers().addAll(ASTNodeFactory.newModifiers(ast, fVisibility));
newConstructor.setName(ast.newSimpleName(fClassName));
addLinkedPosition(KEY_TYPE_NAME, newConstructor.getName(), astRewrite, false);
newConstructor.setBody(ast.newBlock());
List<Statement> newStatements = newConstructor.getBody().statements();
List<SingleVariableDeclaration> newParameters = newConstructor.parameters();
List<String> newParameterNames = new ArrayList<String>();
// add parameters for elements passed with the instance creation
if (!instanceCreation.arguments().isEmpty()) {
IMethodBinding constructorBinding = getSuperConstructorBinding();
if (constructorBinding != null) {
SuperConstructorInvocation superConstructorInvocation = ast.newSuperConstructorInvocation();
ITypeBinding[] parameterTypes = constructorBinding.getParameterTypes();
String[][] parameterNames = StubUtility.suggestArgumentNamesWithProposals(project, constructorBinding);
for (int i = 0; i < parameterNames.length; i++) {
String[] nameProposals = parameterNames[i];
String paramName = nameProposals[0];
SingleVariableDeclaration param = newParameterDeclaration(ast, importRewrite, paramName, parameterTypes[i]);
newParameters.add(param);
newParameterNames.add(paramName);
SimpleName newSIArgument = ast.newSimpleName(paramName);
superConstructorInvocation.arguments().add(newSIArgument);
if (fLinkedProposalModel != null) {
LinkedProposalPositionGroup positionGroup = fLinkedProposalModel.getPositionGroup(KEY_PARAM_NAME_CONST + String.valueOf(i), true);
positionGroup.addPosition(astRewrite.track(param.getName()), false);
positionGroup.addPosition(astRewrite.track(newSIArgument), false);
for (int k = 0; k < nameProposals.length; k++) {
positionGroup.addProposal(nameProposals[k], null, nameProposals.length - k);
}
}
}
newStatements.add(superConstructorInvocation);
}
}
// add parameters for all outer variables used
boolean useThisAccess = useThisForFieldAccess();
for (int i = 0; i < bindings.length; i++) {
String baseName = StubUtility.getBaseName(bindings[i], project);
String[] paramNameProposals = StubUtility.getVariableNameSuggestions(NamingConventions.VK_PARAMETER, project, baseName, 0, newParameterNames, true);
String paramName = paramNameProposals[0];
SingleVariableDeclaration param = newParameterDeclaration(ast, importRewrite, paramName, bindings[i].getType());
newParameters.add(param);
newParameterNames.add(paramName);
String fieldName = fieldNames[i];
SimpleName fieldNameNode = ast.newSimpleName(fieldName);
SimpleName paramNameNode = ast.newSimpleName(paramName);
newStatements.add(newFieldAssignment(ast, fieldNameNode, paramNameNode, useThisAccess || newParameterNames.contains(fieldName)));
if (fLinkedProposalModel != null) {
LinkedProposalPositionGroup positionGroup = fLinkedProposalModel.getPositionGroup(KEY_PARAM_NAME_EXT + String.valueOf(i), true);
positionGroup.addPosition(astRewrite.track(param.getName()), false);
positionGroup.addPosition(astRewrite.track(paramNameNode), false);
for (int k = 0; k < paramNameProposals.length; k++) {
positionGroup.addProposal(paramNameProposals[k], null, paramNameProposals.length - k);
}
fLinkedProposalModel.getPositionGroup(KEY_FIELD_NAME_EXT + i, true).addPosition(astRewrite.track(fieldNameNode), false);
}
}
addExceptionsToNewConstructor(newConstructor, importRewrite);
if (doAddComments()) {
try {
String[] allParamNames = newParameterNames.toArray(new String[newParameterNames.size()]);
String string = CodeGeneration.getMethodComment(fCu, fClassName, fClassName, allParamNames, new String[0], null, new String[0], null, StubUtility.getLineDelimiterUsed(fCu));
if (string != null) {
Javadoc javadoc = (Javadoc) astRewrite.createStringPlaceholder(string, ASTNode.JAVADOC);
newConstructor.setJavadoc(javadoc);
}
} catch (CoreException exception) {
throw new JavaModelException(exception);
}
}
return newConstructor;
}
use of org.eclipse.jdt.core.dom.Javadoc in project che by eclipse.
the class ConvertAnonymousToNestedRefactoring method createFieldsForAccessedLocals.
private void createFieldsForAccessedLocals(CompilationUnitRewrite rewrite, IVariableBinding[] varBindings, String[] fieldNames, List<BodyDeclaration> newBodyDeclarations) throws CoreException {
final ImportRewrite importRewrite = rewrite.getImportRewrite();
final ASTRewrite astRewrite = rewrite.getASTRewrite();
final AST ast = astRewrite.getAST();
for (int i = 0; i < varBindings.length; i++) {
VariableDeclarationFragment fragment = ast.newVariableDeclarationFragment();
fragment.setInitializer(null);
fragment.setName(ast.newSimpleName(fieldNames[i]));
FieldDeclaration field = ast.newFieldDeclaration(fragment);
ITypeBinding varType = varBindings[i].getType();
field.setType(importRewrite.addImport(varType, ast));
field.modifiers().addAll(ASTNodeFactory.newModifiers(ast, Modifier.PRIVATE | Modifier.FINAL));
if (doAddComments()) {
String string = CodeGeneration.getFieldComment(rewrite.getCu(), varType.getName(), fieldNames[i], StubUtility.getLineDelimiterUsed(fCu));
if (string != null) {
Javadoc javadoc = (Javadoc) astRewrite.createStringPlaceholder(string, ASTNode.JAVADOC);
field.setJavadoc(javadoc);
}
}
newBodyDeclarations.add(field);
addLinkedPosition(KEY_FIELD_NAME_EXT + i, fragment.getName(), astRewrite, false);
}
}
Aggregations