use of org.eclipse.jdt.internal.ui.text.correction.proposals.NewMethodCorrectionProposal in project che by eclipse.
the class UnresolvedElementsSubProcessor method addNewMethodProposals.
private static void addNewMethodProposals(ICompilationUnit cu, CompilationUnit astRoot, Expression sender, List<Expression> arguments, boolean isSuperInvocation, ASTNode invocationNode, String methodName, Collection<ICommandAccess> proposals) throws JavaModelException {
ITypeBinding nodeParentType = Bindings.getBindingOfParentType(invocationNode);
ITypeBinding binding = null;
if (sender != null) {
binding = sender.resolveTypeBinding();
} else {
binding = nodeParentType;
if (isSuperInvocation && binding != null) {
binding = binding.getSuperclass();
}
}
if (binding != null && binding.isFromSource()) {
ITypeBinding senderDeclBinding = binding.getTypeDeclaration();
ICompilationUnit targetCU = ASTResolving.findCompilationUnitForBinding(cu, astRoot, senderDeclBinding);
if (targetCU != null) {
String label;
Image image;
ITypeBinding[] parameterTypes = getParameterTypes(arguments);
if (parameterTypes != null) {
String sig = ASTResolving.getMethodSignature(methodName, parameterTypes, false);
if (ASTResolving.isUseableTypeInContext(parameterTypes, senderDeclBinding, false)) {
if (nodeParentType == senderDeclBinding) {
label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_createmethod_description, sig);
image = JavaPluginImages.get(JavaPluginImages.IMG_MISC_PRIVATE);
} else {
label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_createmethod_other_description, new Object[] { sig, BasicElementLabels.getJavaElementName(senderDeclBinding.getName()) });
image = JavaPluginImages.get(JavaPluginImages.IMG_MISC_PUBLIC);
}
proposals.add(new NewMethodCorrectionProposal(label, targetCU, invocationNode, arguments, senderDeclBinding, IProposalRelevance.CREATE_METHOD, image));
}
if (senderDeclBinding.isNested() && cu.equals(targetCU) && sender == null && Bindings.findMethodInHierarchy(senderDeclBinding, methodName, (ITypeBinding[]) null) == null) {
// no covering method
ASTNode anonymDecl = astRoot.findDeclaringNode(senderDeclBinding);
if (anonymDecl != null) {
senderDeclBinding = Bindings.getBindingOfParentType(anonymDecl.getParent());
if (!senderDeclBinding.isAnonymous() && ASTResolving.isUseableTypeInContext(parameterTypes, senderDeclBinding, false)) {
String[] args = new String[] { sig, ASTResolving.getTypeSignature(senderDeclBinding) };
label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_createmethod_other_description, args);
image = JavaPluginImages.get(JavaPluginImages.IMG_MISC_PROTECTED);
proposals.add(new NewMethodCorrectionProposal(label, targetCU, invocationNode, arguments, senderDeclBinding, IProposalRelevance.CREATE_METHOD, image));
}
}
}
}
}
}
}
use of org.eclipse.jdt.internal.ui.text.correction.proposals.NewMethodCorrectionProposal in project che by eclipse.
the class UnresolvedElementsSubProcessor method getConstructorProposals.
public static void getConstructorProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) throws CoreException {
ICompilationUnit cu = context.getCompilationUnit();
CompilationUnit astRoot = context.getASTRoot();
ASTNode selectedNode = problem.getCoveringNode(astRoot);
if (selectedNode == null) {
return;
}
ITypeBinding targetBinding = null;
List<Expression> arguments = null;
IMethodBinding recursiveConstructor = null;
int type = selectedNode.getNodeType();
if (type == ASTNode.CLASS_INSTANCE_CREATION) {
ClassInstanceCreation creation = (ClassInstanceCreation) selectedNode;
IBinding binding = creation.getType().resolveBinding();
if (binding instanceof ITypeBinding) {
targetBinding = (ITypeBinding) binding;
arguments = creation.arguments();
}
} else if (type == ASTNode.SUPER_CONSTRUCTOR_INVOCATION) {
ITypeBinding typeBinding = Bindings.getBindingOfParentType(selectedNode);
if (typeBinding != null && !typeBinding.isAnonymous()) {
targetBinding = typeBinding.getSuperclass();
arguments = ((SuperConstructorInvocation) selectedNode).arguments();
}
} else if (type == ASTNode.CONSTRUCTOR_INVOCATION) {
ITypeBinding typeBinding = Bindings.getBindingOfParentType(selectedNode);
if (typeBinding != null && !typeBinding.isAnonymous()) {
targetBinding = typeBinding;
arguments = ((ConstructorInvocation) selectedNode).arguments();
recursiveConstructor = ASTResolving.findParentMethodDeclaration(selectedNode).resolveBinding();
}
}
if (targetBinding == null) {
return;
}
IMethodBinding[] methods = targetBinding.getDeclaredMethods();
ArrayList<IMethodBinding> similarElements = new ArrayList<IMethodBinding>();
for (int i = 0; i < methods.length; i++) {
IMethodBinding curr = methods[i];
if (curr.isConstructor() && recursiveConstructor != curr) {
// similar elements can contain a implicit default constructor
similarElements.add(curr);
}
}
addParameterMissmatchProposals(context, problem, similarElements, selectedNode, arguments, proposals);
if (targetBinding.isFromSource()) {
ITypeBinding targetDecl = targetBinding.getTypeDeclaration();
ICompilationUnit targetCU = ASTResolving.findCompilationUnitForBinding(cu, astRoot, targetDecl);
if (targetCU != null) {
String[] args = new String[] { ASTResolving.getMethodSignature(ASTResolving.getTypeSignature(targetDecl), getParameterTypes(arguments), false) };
String label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_createconstructor_description, args);
Image image = JavaElementImageProvider.getDecoratedImage(JavaPluginImages.DESC_MISC_PUBLIC, JavaElementImageDescriptor.CONSTRUCTOR, JavaElementImageProvider.SMALL_SIZE);
proposals.add(new NewMethodCorrectionProposal(label, targetCU, selectedNode, arguments, targetDecl, IProposalRelevance.CREATE_CONSTRUCTOR, image));
}
}
}
Aggregations