public static void getTypeParameterMismatchProposals(IInvocationContext context, IProblemLocation problem, Collection proposals) { CompilationUnit astRoot= context.getASTRoot(); ASTNode selectedNode= problem.getCoveredNode(astRoot); if (!(selectedNode instanceof SimpleName)) { return; } ASTNode normalizedNode= ASTNodes.getNormalizedNode(selectedNode); if (!(normalizedNode instanceof ParameterizedType)) { return; } // waiting for result of https://bugs.eclipse.org/bugs/show_bug.cgi?id=81544 }
public String initialize(CompilationUnit root, ASTNode node) { if (!(node instanceof Name)) return SearchMessages.ImplementOccurrencesFinder_invalidTarget; fSelectedNode= ASTNodes.getNormalizedNode(node); if (!(fSelectedNode instanceof Type)) return SearchMessages.ImplementOccurrencesFinder_invalidTarget; ASTNode typeDeclaration= fSelectedNode.getParent(); if (!(typeDeclaration instanceof AbstractTypeDeclaration)) return SearchMessages.ImplementOccurrencesFinder_invalidTarget; fSelectedType= ((Type)fSelectedNode).resolveBinding(); if (fSelectedType == null) return SearchMessages.ImplementOccurrencesFinder_invalidTarget; fStart= typeDeclaration; return null; }
/** * Checks whether the given name belongs to a {@link ClassInstanceCreation} and if so, returns * its constructor binding. * * @param nameNode the name node * @return the constructor binding or <code>null</code> if not found * @since 3.7 */ private IBinding getConstructorBindingIfAvailable(Name nameNode) { ASTNode type= ASTNodes.getNormalizedNode(nameNode); StructuralPropertyDescriptor loc= type.getLocationInParent(); if (loc == ClassInstanceCreation.TYPE_PROPERTY) { return ((ClassInstanceCreation) type.getParent()).resolveConstructorBinding(); } return null; }
/** * Checks whether the given name belongs to a {@link ClassInstanceCreation} and if so, returns * its constructor binding. * * @param nameNode the name node * @return the constructor binding or <code>null</code> if not found * @since 3.7 */ private IBinding getConstructorBindingIfAvailable(Name nameNode) { ASTNode type= ASTNodes.getNormalizedNode(nameNode); StructuralPropertyDescriptor loc= type.getLocationInParent(); if (loc == ClassInstanceCreation.TYPE_PROPERTY) { return ((ClassInstanceCreation) type.getParent()).resolveConstructorBinding(); } return null; }
/** * Extracts the binding from the token's simple name. * Works around bug 62605 to return the correct constructor binding in a ClassInstanceCreation. * * @param token the token to extract the binding from * @return the token's binding, or <code>null</code> */ private static IBinding getBinding(SemanticToken token) { ASTNode node= token.getNode(); ASTNode normalized= ASTNodes.getNormalizedNode(node); if (normalized.getLocationInParent() == ClassInstanceCreation.TYPE_PROPERTY) { // work around: https://bugs.eclipse.org/bugs/show_bug.cgi?id=62605 return ((ClassInstanceCreation) normalized.getParent()).resolveConstructorBinding(); } return token.getBinding(); }
/** * Extracts the binding from the token's simple name. Works around bug 62605 to * return the correct constructor binding in a ClassInstanceCreation. * * @param token * the token to extract the binding from * @return the token's binding, or <code>null</code> */ private static IBinding getBinding(SemanticToken token) { ASTNode node = token.getNode(); ASTNode normalized = ASTNodes.getNormalizedNode(node); if (normalized.getLocationInParent() == ClassInstanceCreation.TYPE_PROPERTY) { // work around: https://bugs.eclipse.org/bugs/show_bug.cgi?id=62605 return ((ClassInstanceCreation) normalized.getParent()).resolveConstructorBinding(); } return token.getBinding(); }
/** * Extracts the binding from the token's simple name. * Works around bug 62605 to return the correct constructor binding in a ClassInstanceCreation. * * @param token the token to extract the binding from * @return the token's binding, or <code>null</code> */ private static IBinding getBinding(SemanticToken token) { ASTNode node= token.getNode(); ASTNode normalized= ASTNodes.getNormalizedNode(node); if (normalized.getLocationInParent() == ClassInstanceCreation.TYPE_PROPERTY) { // work around: https://bugs.eclipse.org/bugs/show_bug.cgi?id=62605 return ((ClassInstanceCreation) normalized.getParent()).resolveConstructorBinding(); } return token.getBinding(); }
private static AnonymousClassDeclaration getAnonymousInnerClass(ASTNode node) { if (node == null) return null; if (node instanceof AnonymousClassDeclaration) return (AnonymousClassDeclaration)node; if (node instanceof ClassInstanceCreation) { AnonymousClassDeclaration anon= ((ClassInstanceCreation)node).getAnonymousClassDeclaration(); if (anon != null) return anon; } node= ASTNodes.getNormalizedNode(node); if (node.getLocationInParent() == ClassInstanceCreation.TYPE_PROPERTY) { AnonymousClassDeclaration anon= ((ClassInstanceCreation)node.getParent()).getAnonymousClassDeclaration(); if (anon != null) return anon; } return (AnonymousClassDeclaration)ASTNodes.getParent(node, AnonymousClassDeclaration.class); }
private static AnonymousClassDeclaration getAnonymousInnerClass(ASTNode node) { if (node == null) return null; if (node instanceof AnonymousClassDeclaration) return (AnonymousClassDeclaration)node; if (node instanceof ClassInstanceCreation) { AnonymousClassDeclaration anon= ((ClassInstanceCreation)node).getAnonymousClassDeclaration(); if (anon != null) return anon; } node= ASTNodes.getNormalizedNode(node); if (node.getLocationInParent() == ClassInstanceCreation.TYPE_PROPERTY) { AnonymousClassDeclaration anon= ((ClassInstanceCreation)node.getParent()).getAnonymousClassDeclaration(); if (anon != null) return anon; } return (AnonymousClassDeclaration)ASTNodes.getParent(node, AnonymousClassDeclaration.class); }
private static AnonymousClassDeclaration getAnonymousInnerClass(ASTNode node) { if (node == null) return null; if (node instanceof AnonymousClassDeclaration) return (AnonymousClassDeclaration)node; if (node instanceof ClassInstanceCreation) { AnonymousClassDeclaration anon= ((ClassInstanceCreation)node).getAnonymousClassDeclaration(); if (anon != null) return anon; } node= ASTNodes.getNormalizedNode(node); if (node.getLocationInParent() == ClassInstanceCreation.TYPE_PROPERTY) { AnonymousClassDeclaration anon= ((ClassInstanceCreation)node.getParent()).getAnonymousClassDeclaration(); if (anon != null) return anon; } return ASTNodes.getParent(node, AnonymousClassDeclaration.class); }
public static void addUnnecessaryThrownExceptionProposal(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) { ASTNode selectedNode= problem.getCoveringNode(context.getASTRoot()); selectedNode= ASTNodes.getNormalizedNode(selectedNode); if (selectedNode == null || selectedNode.getLocationInParent() != MethodDeclaration.THROWN_EXCEPTION_TYPES_PROPERTY) { return; } MethodDeclaration decl= (MethodDeclaration) selectedNode.getParent(); IMethodBinding binding= decl.resolveBinding(); if (binding != null) { List<Type> thrownExceptions= decl.thrownExceptionTypes(); int index= thrownExceptions.indexOf(selectedNode); if (index == -1) { return; } ChangeDescription[] desc= new ChangeDescription[thrownExceptions.size()]; desc[index]= new RemoveDescription(); ICompilationUnit cu= context.getCompilationUnit(); String label= CorrectionMessages.LocalCorrectionsSubProcessor_unnecessarythrow_description; Image image= JavaPluginImages.get(JavaPluginImages.IMG_OBJS_EXCEPTION); proposals.add(new ChangeMethodSignatureProposal(label, cu, selectedNode, binding, null, desc, IProposalRelevance.UNNECESSARY_THROW, image)); } JavadocTagsSubProcessor.getUnusedAndUndocumentedParameterOrExceptionProposals(context, problem, proposals); }
@Override public String initialize(CompilationUnit root, ASTNode node) { if (!(node instanceof Name)) return SearchMessages.ImplementOccurrencesFinder_invalidTarget; fSelectedNode= ASTNodes.getNormalizedNode(node); if (!(fSelectedNode instanceof Type)) return SearchMessages.ImplementOccurrencesFinder_invalidTarget; StructuralPropertyDescriptor location= fSelectedNode.getLocationInParent(); if (location != TypeDeclaration.SUPERCLASS_TYPE_PROPERTY && location != TypeDeclaration.SUPER_INTERFACE_TYPES_PROPERTY && location != EnumDeclaration.SUPER_INTERFACE_TYPES_PROPERTY) return SearchMessages.ImplementOccurrencesFinder_invalidTarget; fSelectedType= ((Type)fSelectedNode).resolveBinding(); if (fSelectedType == null) return SearchMessages.ImplementOccurrencesFinder_invalidTarget; fStart= fSelectedNode.getParent(); // type declaration fASTRoot= root; fDescription= Messages.format(SearchMessages.ImplementOccurrencesFinder_occurrence_description, BasicElementLabels.getJavaElementName(fSelectedType.getName())); return null; }
@Override public String initialize(CompilationUnit root, ASTNode node) { if (!(node instanceof Name)) return SearchMessages.ImplementOccurrencesFinder_invalidTarget; fSelectedNode= ASTNodes.getNormalizedNode(node); if (!(fSelectedNode instanceof Type)) return SearchMessages.ImplementOccurrencesFinder_invalidTarget; StructuralPropertyDescriptor location= fSelectedNode.getLocationInParent(); if (location != TypeDeclaration.SUPERCLASS_TYPE_PROPERTY && location != TypeDeclaration.SUPER_INTERFACE_TYPES_PROPERTY && location != EnumDeclaration.SUPER_INTERFACE_TYPES_PROPERTY) return SearchMessages.ImplementOccurrencesFinder_invalidTarget; fSelectedType= ((Type)fSelectedNode).resolveBinding(); if (fSelectedType == null) return SearchMessages.ImplementOccurrencesFinder_invalidTarget; fStart= fSelectedNode.getParent(); // type declaration fASTRoot= root; fDescription= Messages.format(SearchMessages.ImplementOccurrencesFinder_occurrence_description, BasicElementLabels.getJavaElementName(fSelectedType.getName())); return null; }
public static void addRedundantSuperInterfaceProposal(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) { ASTNode selectedNode= problem.getCoveringNode(context.getASTRoot()); if (!(selectedNode instanceof Name)) { return; } ASTNode node= ASTNodes.getNormalizedNode(selectedNode); ASTRewrite rewrite= ASTRewrite.create(node.getAST()); rewrite.remove(node, null); String label= CorrectionMessages.LocalCorrectionsSubProcessor_remove_redundant_superinterface; Image image= JavaPlugin.getDefault().getWorkbench().getSharedImages().getImage(ISharedImages.IMG_TOOL_DELETE); ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.REMOVE_REDUNDANT_SUPER_INTERFACE, image); proposals.add(proposal); }
public static void addRedundantSuperInterfaceProposal(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) { ASTNode selectedNode= problem.getCoveringNode(context.getASTRoot()); if (!(selectedNode instanceof Name)) { return; } ASTNode node= ASTNodes.getNormalizedNode(selectedNode); ASTRewrite rewrite= ASTRewrite.create(node.getAST()); rewrite.remove(node, null); String label= CorrectionMessages.LocalCorrectionsSubProcessor_remove_redundant_superinterface; Image image= JavaPlugin.getDefault().getWorkbench().getSharedImages().getImage(ISharedImages.IMG_TOOL_DELETE); ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.REMOVE_REDUNDANT_SUPER_INTERFACE, image); proposals.add(proposal); }
public static void removeMismatchedArguments(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals){ ICompilationUnit cu= context.getCompilationUnit(); ASTNode selectedNode= problem.getCoveredNode(context.getASTRoot()); if (!(selectedNode instanceof SimpleName)) { return; } ASTNode normalizedNode=ASTNodes.getNormalizedNode(selectedNode); if (normalizedNode instanceof ParameterizedType) { ASTRewrite rewrite = ASTRewrite.create(normalizedNode.getAST()); ParameterizedType pt = (ParameterizedType) normalizedNode; ASTNode mt = rewrite.createMoveTarget(pt.getType()); rewrite.replace(pt, mt, null); String label= CorrectionMessages.TypeArgumentMismatchSubProcessor_removeTypeArguments; Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE); ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.REMOVE_TYPE_ARGUMENTS, image); proposals.add(proposal); } }
public static void removeMismatchedParameters(IInvocationContext context, IProblemLocation problem, Collection proposals){ ICompilationUnit cu= context.getCompilationUnit(); ASTNode selectedNode= problem.getCoveredNode(context.getASTRoot()); if (!(selectedNode instanceof SimpleName)) { return; } ASTNode normalizedNode=ASTNodes.getNormalizedNode(selectedNode); if (normalizedNode instanceof ParameterizedType) { ASTRewrite rewrite = ASTRewrite.create(normalizedNode.getAST()); ParameterizedType pt = (ParameterizedType) normalizedNode; ASTNode mt = rewrite.createMoveTarget(pt.getType()); rewrite.replace(pt, mt, null); String label= CorrectionMessages.TypeParameterMismatchSubProcessor_removeTypeParameter; Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE); ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, cu, rewrite, 6, image); proposals.add(proposal); } }
public static void removeMismatchedArguments(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals){ ICompilationUnit cu= context.getCompilationUnit(); ASTNode selectedNode= problem.getCoveredNode(context.getASTRoot()); if (!(selectedNode instanceof SimpleName)) { return; } ASTNode normalizedNode=ASTNodes.getNormalizedNode(selectedNode); if (normalizedNode instanceof ParameterizedType) { ASTRewrite rewrite = ASTRewrite.create(normalizedNode.getAST()); ParameterizedType pt = (ParameterizedType) normalizedNode; ASTNode mt = rewrite.createMoveTarget(pt.getType()); rewrite.replace(pt, mt, null); String label= CorrectionMessages.TypeArgumentMismatchSubProcessor_removeTypeArguments; Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE); ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.REMOVE_TYPE_ARGUMENTS, image); proposals.add(proposal); } }
/** * Finds and returns the <code>ASTNode</code> for the given source text * selection, if it is an entire constructor call or the class name portion * of a constructor call or constructor declaration, or null otherwise. * @param unit The compilation unit in which the selection was made * @param offset The textual offset of the start of the selection * @param length The length of the selection in characters * @return ClassInstanceCreation or MethodDeclaration */ private ASTNode getTargetNode(ICompilationUnit unit, int offset, int length) { ASTNode node= ASTNodes.getNormalizedNode(NodeFinder.perform(fCU, offset, length)); if (node.getNodeType() == ASTNode.CLASS_INSTANCE_CREATION) return node; if (node.getNodeType() == ASTNode.METHOD_DECLARATION && ((MethodDeclaration)node).isConstructor()) return node; // we have some sub node. Make sure its the right child of the parent StructuralPropertyDescriptor location= node.getLocationInParent(); ASTNode parent= node.getParent(); if (location == ClassInstanceCreation.TYPE_PROPERTY) { return parent; } else if (location == MethodDeclaration.NAME_PROPERTY && ((MethodDeclaration)parent).isConstructor()) { return parent; } return null; }
/** * Finds and returns the <code>ASTNode</code> for the given source text * selection, if it is an entire constructor call or the class name portion * of a constructor call or constructor declaration, or null otherwise. * @param unit The compilation unit in which the selection was made * @param offset The textual offset of the start of the selection * @param length The length of the selection in characters * @return ClassInstanceCreation or MethodDeclaration */ private ASTNode getTargetNode(ICompilationUnit unit, int offset, int length) { ASTNode node= ASTNodes.getNormalizedNode(NodeFinder.perform(fCU, offset, length)); if (node.getNodeType() == ASTNode.CLASS_INSTANCE_CREATION) return node; if (node.getNodeType() == ASTNode.METHOD_DECLARATION && ((MethodDeclaration)node).isConstructor()) return node; // we have some sub node. Make sure its the right child of the parent StructuralPropertyDescriptor location= node.getLocationInParent(); ASTNode parent= node.getParent(); if (location == ClassInstanceCreation.TYPE_PROPERTY) { return parent; } else if (location == MethodDeclaration.NAME_PROPERTY && ((MethodDeclaration)parent).isConstructor()) { return parent; } return null; }