use of org.eclipse.jdt.core.dom.MethodDeclaration in project che by eclipse.
the class ModifierCorrectionSubProcessor method addMethodRequiresBodyProposals.
public static void addMethodRequiresBodyProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
ICompilationUnit cu = context.getCompilationUnit();
AST ast = context.getASTRoot().getAST();
ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
if (!(selectedNode instanceof MethodDeclaration)) {
return;
}
MethodDeclaration decl = (MethodDeclaration) selectedNode;
Modifier modifierNode;
{
ASTRewrite rewrite = ASTRewrite.create(ast);
modifierNode = removeModifier(decl, rewrite, Modifier.ABSTRACT);
Block body = ast.newBlock();
rewrite.set(decl, MethodDeclaration.BODY_PROPERTY, body, null);
if (!decl.isConstructor()) {
Type returnType = decl.getReturnType2();
if (returnType != null) {
Expression expression = ASTNodeFactory.newDefaultExpression(ast, returnType, decl.getExtraDimensions());
if (expression != null) {
ReturnStatement returnStatement = ast.newReturnStatement();
returnStatement.setExpression(expression);
body.statements().add(returnStatement);
}
}
}
String label = CorrectionMessages.ModifierCorrectionSubProcessor_addmissingbody_description;
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.ADD_MISSING_BODY, image);
proposals.add(proposal);
}
IMethodBinding binding = decl.resolveBinding();
if (modifierNode == null && binding != null) {
String label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemodifiertoabstract_description, getMethodLabel(binding));
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
int included = binding.getDeclaringClass().isInterface() ? Modifier.NONE : Modifier.ABSTRACT;
int excluded = Modifier.STATIC | Modifier.DEFAULT;
ModifierChangeCorrectionProposal proposal = new ModifierChangeCorrectionProposal(label, cu, binding, decl, included, excluded, IProposalRelevance.ADD_ABSTRACT_MODIFIER, image);
proposals.add(proposal);
}
}
use of org.eclipse.jdt.core.dom.MethodDeclaration 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.MethodDeclaration in project che by eclipse.
the class Java50Fix method getRawReference.
private static SimpleType getRawReference(SimpleName name, CompilationUnit compilationUnit) {
SimpleName[] names = LinkedNodeFinder.findByNode(compilationUnit, name);
for (int j = 0; j < names.length; j++) {
if (names[j].getParent() instanceof VariableDeclarationFragment) {
VariableDeclarationFragment fragment = (VariableDeclarationFragment) names[j].getParent();
if (fragment.getParent() instanceof VariableDeclarationStatement) {
VariableDeclarationStatement statement = (VariableDeclarationStatement) fragment.getParent();
ASTNode result = (ASTNode) statement.getStructuralProperty(VariableDeclarationStatement.TYPE_PROPERTY);
if (isRawTypeReference(result))
return (SimpleType) result;
} else if (fragment.getParent() instanceof FieldDeclaration) {
FieldDeclaration declaration = (FieldDeclaration) fragment.getParent();
ASTNode result = (ASTNode) declaration.getStructuralProperty(FieldDeclaration.TYPE_PROPERTY);
if (isRawTypeReference(result))
return (SimpleType) result;
}
} else if (names[j].getParent() instanceof SingleVariableDeclaration) {
SingleVariableDeclaration declaration = (SingleVariableDeclaration) names[j].getParent();
ASTNode result = (ASTNode) declaration.getStructuralProperty(SingleVariableDeclaration.TYPE_PROPERTY);
if (isRawTypeReference(result))
return (SimpleType) result;
} else if (names[j].getParent() instanceof MethodDeclaration) {
MethodDeclaration methodDecl = (MethodDeclaration) names[j].getParent();
ASTNode result = (ASTNode) methodDecl.getStructuralProperty(MethodDeclaration.RETURN_TYPE2_PROPERTY);
if (isRawTypeReference(result))
return (SimpleType) result;
}
}
return null;
}
use of org.eclipse.jdt.core.dom.MethodDeclaration in project che by eclipse.
the class NullAnnotationsRewriteOperations method createAddAnnotationToOverriddenOperation.
private static SignatureAnnotationRewriteOperation createAddAnnotationToOverriddenOperation(CompilationUnit compilationUnit, IProblemLocation problem, String annotationToAdd, String annotationToRemove, boolean allowRemove) {
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:
case IProblem.IllegalRedefinitionToNonNullParameter:
break;
case IProblem.IllegalReturnNullityRedefinition:
if (declaringNode == null)
declaringNode = selectedNode;
break;
default:
return null;
}
String annotationNameLabel = annotationToAdd;
int lastDot = annotationToAdd.lastIndexOf('.');
if (lastDot != -1)
annotationNameLabel = annotationToAdd.substring(lastDot + 1);
annotationNameLabel = BasicElementLabels.getJavaElementName(annotationNameLabel);
if (declaringNode instanceof MethodDeclaration) {
// complaint is in signature of this method
MethodDeclaration declaration = (MethodDeclaration) declaringNode;
switch(problem.getProblemId()) {
case IProblem.IllegalDefinitionToNonNullParameter:
case IProblem.IllegalRedefinitionToNonNullParameter:
return createChangeOverriddenParameterOperation(compilationUnit, cu, declaration, selectedNode, allowRemove, annotationToAdd, annotationToRemove, annotationNameLabel);
case IProblem.IllegalReturnNullityRedefinition:
if (hasNullAnnotation(declaration)) {
// don't adjust super if local has no explicit annotation (?)
return createChangeOverriddenReturnOperation(compilationUnit, cu, declaration, allowRemove, annotationToAdd, annotationToRemove, annotationNameLabel);
}
}
}
return null;
}
use of org.eclipse.jdt.core.dom.MethodDeclaration in project che by eclipse.
the class LambdaExpressionsFix method isFunctionalAnonymous.
static boolean isFunctionalAnonymous(ClassInstanceCreation node) {
ITypeBinding typeBinding = node.resolveTypeBinding();
if (typeBinding == null)
return false;
ITypeBinding[] interfaces = typeBinding.getInterfaces();
if (interfaces.length != 1)
return false;
if (interfaces[0].getFunctionalInterfaceMethod() == null)
return false;
AnonymousClassDeclaration anonymTypeDecl = node.getAnonymousClassDeclaration();
if (anonymTypeDecl == null || anonymTypeDecl.resolveBinding() == null)
return false;
List<BodyDeclaration> bodyDeclarations = anonymTypeDecl.bodyDeclarations();
// cannot convert if there are fields or additional methods
if (bodyDeclarations.size() != 1)
return false;
BodyDeclaration bodyDeclaration = bodyDeclarations.get(0);
if (!(bodyDeclaration instanceof MethodDeclaration))
return false;
MethodDeclaration methodDecl = (MethodDeclaration) bodyDeclaration;
IMethodBinding methodBinding = methodDecl.resolveBinding();
if (methodBinding == null)
return false;
// generic lambda expressions are not allowed
if (methodBinding.isGenericMethod())
return false;
// lambda cannot refer to 'this'/'super' literals
if (SuperThisReferenceFinder.hasReference(methodDecl))
return false;
if (!isInTargetTypeContext(node))
return false;
return true;
}
Aggregations