use of org.eclipse.jdt.core.dom.SingleMemberAnnotation in project eclipse-pmd by acanda.
the class SuppressWarningsQuickFix method createReplacementSuppressWarningsAnnotation.
private Annotation createReplacementSuppressWarningsAnnotation(final Annotation existingAnnotation, final AST ast) {
final Annotation replacement;
if (existingAnnotation == null || existingAnnotation.isMarkerAnnotation()) {
final SingleMemberAnnotation annotation = createAnnotation(ast, SingleMemberAnnotation.class);
annotation.setValue(createPMDLiteralValue(ast));
replacement = annotation;
} else if (existingAnnotation.isSingleMemberAnnotation()) {
final SingleMemberAnnotation existingSingleMemberAnnotation = (SingleMemberAnnotation) existingAnnotation;
final SingleMemberAnnotation annotation = createAnnotation(ast, SingleMemberAnnotation.class);
annotation.setValue(createArrayInitializer(existingSingleMemberAnnotation.getValue()));
replacement = annotation;
} else if (existingAnnotation.isNormalAnnotation()) {
final NormalAnnotation existingNormalAnnotation = (NormalAnnotation) existingAnnotation;
final NormalAnnotation annotation = createAnnotation(ast, NormalAnnotation.class);
createAnnotationValues(existingNormalAnnotation, annotation);
replacement = annotation;
} else {
replacement = existingAnnotation;
}
return replacement;
}
use of org.eclipse.jdt.core.dom.SingleMemberAnnotation in project AutoRefactor by JnRouvignac.
the class ASTNodeFactory method newSingleMemberAnnotation.
/**
* Builds a new {@link SingleMemberAnnotation} instance.
*
* @param typeName the annotation type name
* @param value the annotation single value
* @return a new single member annotation
*/
public SingleMemberAnnotation newSingleMemberAnnotation(final Name typeName, final Expression value) {
SingleMemberAnnotation sma = ast.newSingleMemberAnnotation();
sma.setTypeName(typeName);
sma.setValue(value);
return sma;
}
use of org.eclipse.jdt.core.dom.SingleMemberAnnotation in project sts4 by spring-projects.
the class RequestMappingSymbolProvider method getMethod.
private String[] getMethod(Annotation node) {
String[] methods = null;
if (node.isNormalAnnotation()) {
NormalAnnotation normNode = (NormalAnnotation) node;
List<?> values = normNode.values();
for (Iterator<?> iterator = values.iterator(); iterator.hasNext(); ) {
Object object = iterator.next();
if (object instanceof MemberValuePair) {
MemberValuePair pair = (MemberValuePair) object;
String valueName = pair.getName().getIdentifier();
if (valueName != null && valueName.equals("method")) {
Expression expression = pair.getValue();
methods = ASTUtils.getExpressionValueAsArray(expression);
break;
}
}
}
} else if (node instanceof SingleMemberAnnotation) {
methods = getRequestMethod((SingleMemberAnnotation) node);
}
if (methods == null && node.getParent() instanceof MethodDeclaration) {
Annotation parentAnnotation = getParentAnnotation(node);
if (parentAnnotation != null) {
methods = getMethod(parentAnnotation);
}
}
return methods;
}
use of org.eclipse.jdt.core.dom.SingleMemberAnnotation in project sts4 by spring-projects.
the class RequestMappingSymbolProvider method getPath.
private String[] getPath(Annotation node) {
if (node.isNormalAnnotation()) {
NormalAnnotation normNode = (NormalAnnotation) node;
List<?> values = normNode.values();
for (Iterator<?> iterator = values.iterator(); iterator.hasNext(); ) {
Object object = iterator.next();
if (object instanceof MemberValuePair) {
MemberValuePair pair = (MemberValuePair) object;
String valueName = pair.getName().getIdentifier();
if (valueName != null && (valueName.equals("value") || valueName.equals("path"))) {
Expression expression = pair.getValue();
return ASTUtils.getExpressionValueAsArray(expression);
}
}
}
} else if (node.isSingleMemberAnnotation()) {
SingleMemberAnnotation singleNode = (SingleMemberAnnotation) node;
Expression expression = singleNode.getValue();
return ASTUtils.getExpressionValueAsArray(expression);
}
return new String[] { "" };
}
use of org.eclipse.jdt.core.dom.SingleMemberAnnotation in project eclipse.jdt.ls by eclipse.
the class TypeMismatchSubProcessor method addTypeMismatchProposals.
public static void addTypeMismatchProposals(IInvocationContext context, IProblemLocationCore problem, Collection<ChangeCorrectionProposal> proposals) throws CoreException {
ICompilationUnit cu = context.getCompilationUnit();
CompilationUnit astRoot = context.getASTRoot();
AST ast = astRoot.getAST();
ASTNode selectedNode = problem.getCoveredNode(astRoot);
if (!(selectedNode instanceof Expression)) {
return;
}
Expression nodeToCast = (Expression) selectedNode;
Name receiverNode = null;
ITypeBinding castTypeBinding = null;
int parentNodeType = selectedNode.getParent().getNodeType();
if (parentNodeType == ASTNode.ASSIGNMENT) {
Assignment assign = (Assignment) selectedNode.getParent();
Expression leftHandSide = assign.getLeftHandSide();
if (selectedNode.equals(leftHandSide)) {
nodeToCast = assign.getRightHandSide();
}
castTypeBinding = assign.getLeftHandSide().resolveTypeBinding();
if (leftHandSide instanceof Name) {
receiverNode = (Name) leftHandSide;
} else if (leftHandSide instanceof FieldAccess) {
receiverNode = ((FieldAccess) leftHandSide).getName();
}
} else if (parentNodeType == ASTNode.VARIABLE_DECLARATION_FRAGMENT) {
VariableDeclarationFragment frag = (VariableDeclarationFragment) selectedNode.getParent();
if (selectedNode.equals(frag.getName()) || selectedNode.equals(frag.getInitializer())) {
nodeToCast = frag.getInitializer();
castTypeBinding = ASTNodes.getType(frag).resolveBinding();
receiverNode = frag.getName();
}
} else if (parentNodeType == ASTNode.MEMBER_VALUE_PAIR) {
receiverNode = ((MemberValuePair) selectedNode.getParent()).getName();
castTypeBinding = ASTResolving.guessBindingForReference(nodeToCast);
} else if (parentNodeType == ASTNode.SINGLE_MEMBER_ANNOTATION) {
// use the type name
receiverNode = ((SingleMemberAnnotation) selectedNode.getParent()).getTypeName();
castTypeBinding = ASTResolving.guessBindingForReference(nodeToCast);
} else {
// try to find the binding corresponding to 'castTypeName'
castTypeBinding = ASTResolving.guessBindingForReference(nodeToCast);
}
if (castTypeBinding == null) {
return;
}
ITypeBinding currBinding = nodeToCast.resolveTypeBinding();
if (currBinding == null && nodeToCast instanceof MethodInvocation) {
IMethodBinding methodBinding = ((MethodInvocation) nodeToCast).resolveMethodBinding();
if (methodBinding != null) {
currBinding = methodBinding.getReturnType();
}
}
if (!(nodeToCast instanceof ArrayInitializer)) {
ITypeBinding castFixType = null;
if (currBinding == null || castTypeBinding.isCastCompatible(currBinding) || nodeToCast instanceof CastExpression) {
castFixType = castTypeBinding;
} else if (JavaModelUtil.is50OrHigher(cu.getJavaProject())) {
ITypeBinding boxUnboxedTypeBinding = boxUnboxPrimitives(castTypeBinding, currBinding, ast);
if (boxUnboxedTypeBinding != castTypeBinding && boxUnboxedTypeBinding.isCastCompatible(currBinding)) {
castFixType = boxUnboxedTypeBinding;
}
}
if (castFixType != null) {
proposals.add(createCastProposal(context, castFixType, nodeToCast, IProposalRelevance.CREATE_CAST));
}
}
// $NON-NLS-1$
boolean nullOrVoid = currBinding == null || "void".equals(currBinding.getName());
// change method return statement to actual type
if (!nullOrVoid && parentNodeType == ASTNode.RETURN_STATEMENT) {
BodyDeclaration decl = ASTResolving.findParentBodyDeclaration(selectedNode);
if (decl instanceof MethodDeclaration) {
MethodDeclaration methodDeclaration = (MethodDeclaration) decl;
currBinding = Bindings.normalizeTypeBinding(currBinding);
if (currBinding == null) {
// $NON-NLS-1$
currBinding = ast.resolveWellKnownType("java.lang.Object");
}
if (currBinding.isWildcardType()) {
currBinding = ASTResolving.normalizeWildcardType(currBinding, true, ast);
}
ASTRewrite rewrite = ASTRewrite.create(ast);
String label = Messages.format(CorrectionMessages.TypeMismatchSubProcessor_changereturntype_description, BasicElementLabels.getJavaElementName(currBinding.getName()));
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, CodeActionKind.QuickFix, cu, rewrite, IProposalRelevance.CHANGE_METHOD_RETURN_TYPE);
ImportRewrite imports = proposal.createImportRewrite(astRoot);
ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(decl, imports);
Type newReturnType = imports.addImport(currBinding, ast, importRewriteContext, TypeLocation.RETURN_TYPE);
rewrite.replace(methodDeclaration.getReturnType2(), newReturnType, null);
proposals.add(proposal);
}
}
if (!nullOrVoid && receiverNode != null) {
currBinding = Bindings.normalizeTypeBinding(currBinding);
if (currBinding == null) {
// $NON-NLS-1$
currBinding = ast.resolveWellKnownType("java.lang.Object");
}
if (currBinding.isWildcardType()) {
currBinding = ASTResolving.normalizeWildcardType(currBinding, true, ast);
}
addChangeSenderTypeProposals(context, receiverNode, currBinding, true, IProposalRelevance.CHANGE_TYPE_OF_RECEIVER_NODE, proposals);
}
addChangeSenderTypeProposals(context, nodeToCast, castTypeBinding, false, IProposalRelevance.CHANGE_TYPE_OF_NODE_TO_CAST, proposals);
if (castTypeBinding == ast.resolveWellKnownType("boolean") && currBinding != null && !currBinding.isPrimitive() && !Bindings.isVoidType(currBinding)) {
// $NON-NLS-1$
String label = CorrectionMessages.TypeMismatchSubProcessor_insertnullcheck_description;
ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST());
InfixExpression expression = ast.newInfixExpression();
expression.setLeftOperand((Expression) rewrite.createMoveTarget(nodeToCast));
expression.setRightOperand(ast.newNullLiteral());
expression.setOperator(InfixExpression.Operator.NOT_EQUALS);
rewrite.replace(nodeToCast, expression, null);
proposals.add(new ASTRewriteCorrectionProposal(label, CodeActionKind.QuickFix, context.getCompilationUnit(), rewrite, IProposalRelevance.INSERT_NULL_CHECK));
}
}
Aggregations