use of org.eclipse.jdt.core.dom.MethodDeclaration in project che by eclipse.
the class NullAnnotationsRewriteOperations method createAddAnnotationOperation.
private static SignatureAnnotationRewriteOperation createAddAnnotationOperation(CompilationUnit compilationUnit, IProblemLocation problem, String annotationToAdd, String annotationToRemove, boolean changeTargetMethod, boolean thisUnitOnly, boolean allowRemove, boolean isArgumentProblem) {
ICompilationUnit cu = (ICompilationUnit) compilationUnit.getJavaElement();
if (!JavaModelUtil.is50OrHigher(cu.getJavaProject()))
return null;
ASTNode selectedNode = problem.getCoveringNode(compilationUnit);
if (selectedNode == null)
return null;
ASTNode declaringNode = getDeclaringNode(selectedNode);
switch(problem.getProblemId()) {
case IProblem.IllegalDefinitionToNonNullParameter:
// these affect another method
break;
case IProblem.IllegalReturnNullityRedefinition:
if (declaringNode == null)
declaringNode = selectedNode;
// do propose changes even if we already have an annotation
break;
default:
// if this method has annotations, don't change'em
if (!allowRemove && NullAnnotationsFix.hasExplicitNullAnnotation(cu, problem.getOffset()))
return null;
}
String annotationNameLabel = annotationToAdd;
int lastDot = annotationToAdd.lastIndexOf('.');
if (lastDot != -1)
annotationNameLabel = annotationToAdd.substring(lastDot + 1);
annotationNameLabel = BasicElementLabels.getJavaElementName(annotationNameLabel);
if (changeTargetMethod) {
MethodInvocation methodInvocation = null;
if (isArgumentProblem) {
if (selectedNode.getParent() instanceof MethodInvocation)
methodInvocation = (MethodInvocation) selectedNode.getParent();
} else {
if (selectedNode instanceof MethodInvocation)
methodInvocation = (MethodInvocation) selectedNode;
}
if (methodInvocation != null) {
// DefiniteNullToNonNullParameter || PotentialNullToNonNullParameter
int paramIdx = methodInvocation.arguments().indexOf(selectedNode);
IMethodBinding methodBinding = methodInvocation.resolveMethodBinding();
compilationUnit = findCUForMethod(compilationUnit, cu, methodBinding);
if (compilationUnit == null)
return null;
if (thisUnitOnly && !compilationUnit.getJavaElement().equals(cu))
return null;
ASTNode methodDecl = compilationUnit.findDeclaringNode(methodBinding.getKey());
if (methodDecl == null)
return null;
if (isArgumentProblem) {
String message = Messages.format(FixMessages.NullAnnotationsRewriteOperations_change_target_method_parameter_nullness, new Object[] { methodInvocation.getName(), annotationNameLabel });
return new ParameterAnnotationRewriteOperation(compilationUnit, (MethodDeclaration) methodDecl, annotationToAdd, annotationToRemove, paramIdx, allowRemove, message);
} else {
MethodDeclaration declaration = (MethodDeclaration) methodDecl;
String message = Messages.format(FixMessages.NullAnnotationsRewriteOperations_change_method_return_nullness, new String[] { declaration.getName().getIdentifier(), annotationNameLabel });
return new ReturnAnnotationRewriteOperation(compilationUnit, declaration, annotationToAdd, annotationToRemove, allowRemove, message);
}
}
} else if (declaringNode instanceof MethodDeclaration) {
// complaint is in signature of this method
MethodDeclaration declaration = (MethodDeclaration) declaringNode;
switch(problem.getProblemId()) {
case IProblem.ParameterLackingNonNullAnnotation:
case IProblem.ParameterLackingNullableAnnotation:
case IProblem.IllegalDefinitionToNonNullParameter:
case IProblem.IllegalRedefinitionToNonNullParameter:
case IProblem.SpecdNonNullLocalVariableComparisonYieldsFalse:
case IProblem.RedundantNullCheckOnSpecdNonNullLocalVariable:
// problems regarding the argument declaration:
if (declaration.getNodeType() == ASTNode.METHOD_DECLARATION) {
String paramName = findAffectedParameterName(selectedNode);
if (paramName != null) {
String message = Messages.format(FixMessages.NullAnnotationsRewriteOperations_change_method_parameter_nullness, new Object[] { paramName, annotationNameLabel });
return new ParameterAnnotationRewriteOperation(compilationUnit, declaration, annotationToAdd, annotationToRemove, paramName, allowRemove, message);
}
}
break;
case IProblem.RequiredNonNullButProvidedNull:
case IProblem.RequiredNonNullButProvidedPotentialNull:
case IProblem.RequiredNonNullButProvidedSpecdNullable:
case IProblem.RequiredNonNullButProvidedUnknown:
case IProblem.ConflictingNullAnnotations:
case IProblem.ConflictingInheritedNullAnnotations:
if (isArgumentProblem) {
// statement suggests changing parameters:
if (declaration.getNodeType() == ASTNode.METHOD_DECLARATION && selectedNode instanceof SimpleName) {
// don't call findAffectedParameterName(), in this branch we're not interested in any target method
String paramName = ((SimpleName) selectedNode).getIdentifier();
if (paramName != null) {
String message = Messages.format(FixMessages.NullAnnotationsRewriteOperations_change_method_parameter_nullness, new Object[] { paramName, annotationNameLabel });
return new ParameterAnnotationRewriteOperation(compilationUnit, declaration, annotationToAdd, annotationToRemove, paramName, allowRemove, message);
}
}
break;
}
//$FALL-THROUGH$
case IProblem.IllegalReturnNullityRedefinition:
String message = Messages.format(FixMessages.NullAnnotationsRewriteOperations_change_method_return_nullness, new String[] { declaration.getName().getIdentifier(), annotationNameLabel });
return new ReturnAnnotationRewriteOperation(compilationUnit, declaration, annotationToAdd, annotationToRemove, allowRemove, message);
}
}
return null;
}
use of org.eclipse.jdt.core.dom.MethodDeclaration in project che by eclipse.
the class ASTNodeFactory method newTypeParameter.
public static TypeParameter newTypeParameter(AST ast, String content) {
StringBuffer buffer = new StringBuffer(TYPEPARAM_HEADER);
buffer.append(content);
buffer.append(TYPEPARAM_FOOTER);
CheASTParser p = CheASTParser.newParser(ast.apiLevel());
p.setSource(buffer.toString().toCharArray());
CompilationUnit root = (CompilationUnit) p.createAST(null);
List<AbstractTypeDeclaration> list = root.types();
TypeDeclaration typeDecl = (TypeDeclaration) list.get(0);
MethodDeclaration methodDecl = typeDecl.getMethods()[0];
TypeParameter tp = (TypeParameter) methodDecl.typeParameters().get(0);
ASTNode result = ASTNode.copySubtree(ast, tp);
result.accept(new PositionClearer());
return (TypeParameter) result;
}
use of org.eclipse.jdt.core.dom.MethodDeclaration in project che by eclipse.
the class ScopeAnalyzer method addLocalDeclarations.
private boolean addLocalDeclarations(ASTNode node, int offset, int flags, IBindingRequestor requestor) {
if (hasFlag(VARIABLES, flags) || hasFlag(TYPES, flags)) {
BodyDeclaration declaration = ASTResolving.findParentBodyDeclaration(node);
if (declaration instanceof MethodDeclaration || declaration instanceof Initializer || declaration instanceof FieldDeclaration) {
ScopeAnalyzerVisitor visitor = new ScopeAnalyzerVisitor(offset, flags, requestor);
declaration.accept(visitor);
return visitor.fBreak;
}
}
return false;
}
use of org.eclipse.jdt.core.dom.MethodDeclaration in project che by eclipse.
the class JavadocTagsSubProcessor method getMissingJavadocCommentProposals.
public static void getMissingJavadocCommentProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> 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));
}
}
use of org.eclipse.jdt.core.dom.MethodDeclaration in project flux by eclipse.
the class ASTNodeFactory method newType.
public static Type newType(AST ast, String content) {
StringBuffer buffer = new StringBuffer(TYPE_HEADER);
buffer.append(content);
buffer.append(TYPE_FOOTER);
ASTParser p = ASTParser.newParser(ast.apiLevel());
p.setSource(buffer.toString().toCharArray());
CompilationUnit root = (CompilationUnit) p.createAST(null);
List<AbstractTypeDeclaration> list = root.types();
TypeDeclaration typeDecl = (TypeDeclaration) list.get(0);
MethodDeclaration methodDecl = typeDecl.getMethods()[0];
ASTNode type = methodDecl.getReturnType2();
ASTNode result = ASTNode.copySubtree(ast, type);
result.accept(new PositionClearer());
return (Type) result;
}
Aggregations