use of org.eclipse.jdt.core.dom.EnumConstantDeclaration in project che by eclipse.
the class SuppressWarningsSubProcessor method addSuppressWarningsProposalIfPossible.
/**
* Adds a SuppressWarnings proposal if possible and returns whether parent nodes should be processed or not (and with what relevance).
*
* @param cu the compilation unit
* @param node the node on which to add a SuppressWarning token
* @param warningToken the warning token to add
* @param relevance the proposal's relevance
* @param proposals collector to which the proposal should be added
* @return <code>0</code> if no further proposals should be added to parent nodes, or the relevance of the next proposal
*
* @since 3.6
*/
private static int addSuppressWarningsProposalIfPossible(ICompilationUnit cu, ASTNode node, String warningToken, int relevance, Collection<ICommandAccess> proposals) {
ChildListPropertyDescriptor property;
String name;
boolean isLocalVariable = false;
switch(node.getNodeType()) {
case ASTNode.SINGLE_VARIABLE_DECLARATION:
property = SingleVariableDeclaration.MODIFIERS2_PROPERTY;
name = ((SingleVariableDeclaration) node).getName().getIdentifier();
isLocalVariable = true;
break;
case ASTNode.VARIABLE_DECLARATION_STATEMENT:
property = VariableDeclarationStatement.MODIFIERS2_PROPERTY;
name = getFirstFragmentName(((VariableDeclarationStatement) node).fragments());
isLocalVariable = true;
break;
case ASTNode.VARIABLE_DECLARATION_EXPRESSION:
property = VariableDeclarationExpression.MODIFIERS2_PROPERTY;
name = getFirstFragmentName(((VariableDeclarationExpression) node).fragments());
isLocalVariable = true;
break;
case ASTNode.TYPE_DECLARATION:
property = TypeDeclaration.MODIFIERS2_PROPERTY;
name = ((TypeDeclaration) node).getName().getIdentifier();
break;
case ASTNode.ANNOTATION_TYPE_DECLARATION:
property = AnnotationTypeDeclaration.MODIFIERS2_PROPERTY;
name = ((AnnotationTypeDeclaration) node).getName().getIdentifier();
break;
case ASTNode.ENUM_DECLARATION:
property = EnumDeclaration.MODIFIERS2_PROPERTY;
name = ((EnumDeclaration) node).getName().getIdentifier();
break;
case ASTNode.FIELD_DECLARATION:
property = FieldDeclaration.MODIFIERS2_PROPERTY;
name = getFirstFragmentName(((FieldDeclaration) node).fragments());
break;
// case ASTNode.INITIALIZER: not used, because Initializer cannot have annotations
case ASTNode.METHOD_DECLARATION:
property = MethodDeclaration.MODIFIERS2_PROPERTY;
//$NON-NLS-1$
name = ((MethodDeclaration) node).getName().getIdentifier() + "()";
break;
case ASTNode.ANNOTATION_TYPE_MEMBER_DECLARATION:
property = AnnotationTypeMemberDeclaration.MODIFIERS2_PROPERTY;
//$NON-NLS-1$
name = ((AnnotationTypeMemberDeclaration) node).getName().getIdentifier() + "()";
break;
case ASTNode.ENUM_CONSTANT_DECLARATION:
property = EnumConstantDeclaration.MODIFIERS2_PROPERTY;
name = ((EnumConstantDeclaration) node).getName().getIdentifier();
break;
default:
return relevance;
}
String label = Messages.format(CorrectionMessages.SuppressWarningsSubProcessor_suppress_warnings_label, new String[] { warningToken, BasicElementLabels.getJavaElementName(name) });
ASTRewriteCorrectionProposal proposal = new SuppressWarningsProposal(warningToken, label, cu, node, property, relevance);
proposals.add(proposal);
return isLocalVariable ? relevance - 1 : 0;
}
use of org.eclipse.jdt.core.dom.EnumConstantDeclaration in project che by eclipse.
the class NewVariableCorrectionProposal method doAddEnumConst.
private ASTRewrite doAddEnumConst(CompilationUnit astRoot) {
SimpleName node = fOriginalNode;
ASTNode newTypeDecl = astRoot.findDeclaringNode(fSenderBinding);
if (newTypeDecl == null) {
astRoot = ASTResolving.createQuickFixAST(getCompilationUnit(), null);
newTypeDecl = astRoot.findDeclaringNode(fSenderBinding.getKey());
}
if (newTypeDecl != null) {
AST ast = newTypeDecl.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
EnumConstantDeclaration constDecl = ast.newEnumConstantDeclaration();
constDecl.setName(ast.newSimpleName(node.getIdentifier()));
ListRewrite listRewriter = rewrite.getListRewrite(newTypeDecl, EnumDeclaration.ENUM_CONSTANTS_PROPERTY);
listRewriter.insertLast(constDecl, null);
addLinkedPosition(rewrite.track(constDecl.getName()), false, KEY_NAME);
return rewrite;
}
return null;
}
use of org.eclipse.jdt.core.dom.EnumConstantDeclaration in project che by eclipse.
the class AddUnimplementedMethodsOperation method rewriteAST.
/**
* {@inheritDoc}
*/
@Override
public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModel model) throws CoreException {
IMethodBinding[] unimplementedMethods = getUnimplementedMethods(fTypeNode);
if (unimplementedMethods.length == 0)
return;
ImportRewriteContext context = new ContextSensitiveImportRewriteContext((CompilationUnit) fTypeNode.getRoot(), fTypeNode.getStartPosition(), cuRewrite.getImportRewrite());
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ICompilationUnit unit = cuRewrite.getCu();
CodeGenerationSettings settings = JavaPreferencesSettings.getCodeGenerationSettings(unit.getJavaProject());
ListRewrite listRewrite;
if (fTypeNode instanceof AnonymousClassDeclaration) {
AnonymousClassDeclaration decl = (AnonymousClassDeclaration) fTypeNode;
listRewrite = rewrite.getListRewrite(decl, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY);
settings.createComments = false;
} else if (fTypeNode instanceof AbstractTypeDeclaration) {
AbstractTypeDeclaration decl = (AbstractTypeDeclaration) fTypeNode;
listRewrite = rewrite.getListRewrite(decl, decl.getBodyDeclarationsProperty());
} else if (fTypeNode instanceof EnumConstantDeclaration) {
EnumConstantDeclaration enumConstantDeclaration = (EnumConstantDeclaration) fTypeNode;
AnonymousClassDeclaration anonymousClassDeclaration = enumConstantDeclaration.getAnonymousClassDeclaration();
if (anonymousClassDeclaration == null) {
anonymousClassDeclaration = rewrite.getAST().newAnonymousClassDeclaration();
rewrite.set(enumConstantDeclaration, EnumConstantDeclaration.ANONYMOUS_CLASS_DECLARATION_PROPERTY, anonymousClassDeclaration, createTextEditGroup(CorrectionMessages.AddUnimplementedMethodsOperation_AddMissingMethod_group, cuRewrite));
}
listRewrite = rewrite.getListRewrite(anonymousClassDeclaration, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY);
settings.createComments = false;
} else {
//$NON-NLS-1$
Assert.isTrue(false, "Unknown type node");
return;
}
ImportRewrite imports = cuRewrite.getImportRewrite();
for (int i = 0; i < unimplementedMethods.length; i++) {
IMethodBinding curr = unimplementedMethods[i];
MethodDeclaration newMethodDecl = StubUtility2.createImplementationStub(unit, rewrite, imports, context, curr, curr.getDeclaringClass().getName(), settings, false);
listRewrite.insertLast(newMethodDecl, createTextEditGroup(CorrectionMessages.AddUnimplementedMethodsOperation_AddMissingMethod_group, cuRewrite));
}
}
use of org.eclipse.jdt.core.dom.EnumConstantDeclaration in project che by eclipse.
the class ASTNodeDeleteUtil method getNodesToDelete.
private static ASTNode[] getNodesToDelete(IJavaElement element, CompilationUnit cuNode) throws JavaModelException {
// fields are different because you don't delete the whole declaration but only a fragment of it
if (element.getElementType() == IJavaElement.FIELD) {
if (JdtFlags.isEnum((IField) element))
return new ASTNode[] { ASTNodeSearchUtil.getEnumConstantDeclaration((IField) element, cuNode) };
else
return new ASTNode[] { ASTNodeSearchUtil.getFieldDeclarationFragmentNode((IField) element, cuNode) };
}
if (element.getElementType() == IJavaElement.TYPE && ((IType) element).isLocal()) {
IType type = (IType) element;
if (type.isAnonymous()) {
if (type.getParent().getElementType() == IJavaElement.FIELD) {
EnumConstantDeclaration enumDecl = ASTNodeSearchUtil.getEnumConstantDeclaration((IField) element.getParent(), cuNode);
if (enumDecl != null && enumDecl.getAnonymousClassDeclaration() != null) {
return new ASTNode[] { enumDecl.getAnonymousClassDeclaration() };
}
}
ClassInstanceCreation creation = ASTNodeSearchUtil.getClassInstanceCreationNode(type, cuNode);
if (creation != null) {
if (creation.getLocationInParent() == ExpressionStatement.EXPRESSION_PROPERTY) {
return new ASTNode[] { creation.getParent() };
} else if (creation.getLocationInParent() == VariableDeclarationFragment.INITIALIZER_PROPERTY) {
return new ASTNode[] { creation };
}
return new ASTNode[] { creation.getAnonymousClassDeclaration() };
}
return new ASTNode[0];
} else {
ASTNode[] nodes = ASTNodeSearchUtil.getDeclarationNodes(element, cuNode);
// we have to delete the TypeDeclarationStatement
nodes[0] = nodes[0].getParent();
return nodes;
}
}
return ASTNodeSearchUtil.getDeclarationNodes(element, cuNode);
}
use of org.eclipse.jdt.core.dom.EnumConstantDeclaration in project eclipse.jdt.ls by eclipse.
the class JavadocTagsSubProcessor method getMissingJavadocCommentProposals.
public static void getMissingJavadocCommentProposals(IInvocationContext context, IProblemLocation problem, Collection<CUCorrectionProposal> proposals) throws CoreException {
ASTNode node = problem.getCoveringNode(context.getASTRoot());
if (node == null) {
return;
}
BodyDeclaration declaration = ASTResolving.findParentBodyDeclaration(node);
if (declaration == null) {
return;
}
ICompilationUnit cu = context.getCompilationUnit();
ITypeBinding binding = Bindings.getBindingOfParentType(declaration);
if (binding == null) {
return;
}
if (declaration instanceof MethodDeclaration) {
MethodDeclaration methodDecl = (MethodDeclaration) declaration;
IMethodBinding methodBinding = methodDecl.resolveBinding();
IMethodBinding overridden = null;
if (methodBinding != null) {
overridden = Bindings.findOverriddenMethod(methodBinding, true);
}
String string = CodeGeneration.getMethodComment(cu, binding.getName(), methodDecl, overridden, String.valueOf('\n'));
if (string != null) {
String label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_method_description;
proposals.add(new AddJavadocCommentProposal(label, cu, IProposalRelevance.ADD_JAVADOC_METHOD, declaration.getStartPosition(), string));
}
} else if (declaration instanceof AbstractTypeDeclaration) {
String typeQualifiedName = Bindings.getTypeQualifiedName(binding);
String[] typeParamNames;
if (declaration instanceof TypeDeclaration) {
List<TypeParameter> typeParams = ((TypeDeclaration) declaration).typeParameters();
typeParamNames = new String[typeParams.size()];
for (int i = 0; i < typeParamNames.length; i++) {
typeParamNames[i] = (typeParams.get(i)).getName().getIdentifier();
}
} else {
typeParamNames = new String[0];
}
String string = CodeGeneration.getTypeComment(cu, typeQualifiedName, typeParamNames, String.valueOf('\n'));
if (string != null) {
String label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_type_description;
proposals.add(new AddJavadocCommentProposal(label, cu, IProposalRelevance.ADD_JAVADOC_TYPE, declaration.getStartPosition(), string));
}
} else if (declaration instanceof FieldDeclaration) {
// $NON-NLS-1$
String comment = "/**\n *\n */\n";
List<VariableDeclarationFragment> fragments = ((FieldDeclaration) declaration).fragments();
if (fragments != null && fragments.size() > 0) {
VariableDeclaration decl = fragments.get(0);
String fieldName = decl.getName().getIdentifier();
String typeName = binding.getName();
comment = CodeGeneration.getFieldComment(cu, typeName, fieldName, String.valueOf('\n'));
}
if (comment != null) {
String label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_field_description;
proposals.add(new AddJavadocCommentProposal(label, cu, IProposalRelevance.ADD_JAVADOC_FIELD, declaration.getStartPosition(), comment));
}
} else if (declaration instanceof EnumConstantDeclaration) {
EnumConstantDeclaration enumDecl = (EnumConstantDeclaration) declaration;
String id = enumDecl.getName().getIdentifier();
String comment = CodeGeneration.getFieldComment(cu, binding.getName(), id, String.valueOf('\n'));
String label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_enumconst_description;
proposals.add(new AddJavadocCommentProposal(label, cu, IProposalRelevance.ADD_JAVADOC_ENUM, declaration.getStartPosition(), comment));
}
}
Aggregations