/** * Returns the topmost ancestor of <code>node</code> that is a {@link Type} (but not a {@link UnionType}). * <p> * <b>Note:</b> The returned node often resolves to a different binding than the given <code>node</code>! * * @param node the starting node, can be <code>null</code> * @return the topmost type or <code>null</code> if the node is not a descendant of a type node * @see #getNormalizedNode(ASTNode) */ public static Type getTopMostType(ASTNode node) { ASTNode result= null; while (node instanceof Type && !(node instanceof UnionType) || node instanceof Name || node instanceof Annotation || node instanceof MemberValuePair || node instanceof Expression) { // Expression could maybe be reduced to expression node types that can appear in an annotation result= node; node= node.getParent(); } if (result instanceof Type) { return (Type) result; } return null; }
/** * Returns the topmost ancestor of <code>node</code> that is a {@link Type} (but not a {@link UnionType}). * <p> * <b>Note:</b> The returned node often resolves to a different binding than the given <code>node</code>! * * @param node the starting node, can be <code>null</code> * @return the topmost type or <code>null</code> if the node is not a descendant of a type node * @see #getNormalizedNode(ASTNode) */ public static Type getTopMostType(ASTNode node) { ASTNode result= null; while (node instanceof Type && !(node instanceof UnionType) || node instanceof Name || node instanceof Annotation || node instanceof MemberValuePair || node instanceof Expression) { // Expression could maybe be reduced to expression node types that can appear in an annotation result= node; node= node.getParent(); } if (result instanceof Type) return (Type) result; return null; }
private static void removeException(ASTRewrite rewrite, UnionType unionType, Type exception) { ListRewrite listRewrite = rewrite.getListRewrite(unionType, UnionType.TYPES_PROPERTY); List<Type> types = unionType.types(); for (Iterator<Type> iterator = types.iterator(); iterator.hasNext();) { Type type = iterator.next(); if (type.equals(exception)) { listRewrite.remove(type, null); } } }
private void handleCatchArguments(List<CatchClause> catchClauses) { for (Iterator<CatchClause> iter = catchClauses.iterator(); iter.hasNext();) { Type type = iter.next().getException().getType(); if (type instanceof UnionType) { List<Type> types = ((UnionType) type).types(); for (Iterator<Type> iterator = types.iterator(); iterator.hasNext();) { removeCaughtExceptions(iterator.next().resolveBinding()); } } else { removeCaughtExceptions(type.resolveBinding()); } } }
private void handleCatchArguments(List<CatchClause> catchClauses) { for (Iterator<CatchClause> iter = catchClauses.iterator(); iter.hasNext(); ) { Type type = iter.next().getException().getType(); if (type instanceof UnionType) { List<Type> types = ((UnionType) type).types(); for (Iterator<Type> iterator = types.iterator(); iterator.hasNext(); ) { removeCaughtExceptions(iterator.next().resolveBinding()); } } else { removeCaughtExceptions(type.resolveBinding()); } } }
private void handleCatchArguments(List<CatchClause> catchClauses) { for (Iterator<CatchClause> iter= catchClauses.iterator(); iter.hasNext(); ) { Type type= iter.next().getException().getType(); if (type instanceof UnionType) { List<Type> types= ((UnionType) type).types(); for (Iterator<Type> iterator= types.iterator(); iterator.hasNext();) { removeCaughtExceptions(iterator.next().resolveBinding()); } } else { removeCaughtExceptions(type.resolveBinding()); } } }
@Override public boolean visit(TryStatement node) { int currentSize= fCaughtExceptions.size(); List<CatchClause> catchClauses= node.catchClauses(); for (Iterator<CatchClause> iter= catchClauses.iterator(); iter.hasNext();) { Type type= iter.next().getException().getType(); if (type instanceof UnionType) { List<Type> types= ((UnionType) type).types(); for (Iterator<Type> iterator= types.iterator(); iterator.hasNext();) { addCaughtException(iterator.next()); } } else { addCaughtException(type); } } node.getBody().accept(this); handleResourceDeclarations(node); int toRemove= fCaughtExceptions.size() - currentSize; for (int i= toRemove; i > 0; i--) { fCaughtExceptions.remove(currentSize); } // visit catch and finally for (Iterator<CatchClause> iter= catchClauses.iterator(); iter.hasNext();) { iter.next().accept(this); } if (node.getFinally() != null) node.getFinally().accept(this); // return false. We have visited the body by ourselves. return false; }
private static void removeException(ASTRewrite rewrite, UnionType unionType, Type exception) { ListRewrite listRewrite= rewrite.getListRewrite(unionType, UnionType.TYPES_PROPERTY); List<Type> types= unionType.types(); for (Iterator<Type> iterator= types.iterator(); iterator.hasNext();) { Type type= iterator.next(); if (type.equals(exception)) { listRewrite.remove(type, null); } } }
@Override public boolean visit(UnionType node) { for (Iterator<Type> it= node.types().iterator(); it.hasNext();) { Type t= it.next(); t.accept(this); if (it.hasNext()) { this.fBuffer.append("|");//$NON-NLS-1$ } } return false; }
/** * Returns the new type node corresponding to the type of the given declaration * including the extra dimensions. If the type is a {@link UnionType}, use the LUB type. * If the <code>importRewrite</code> is <code>null</code>, the type may be fully-qualified. * * @param ast The AST to create the resulting type with. * @param declaration The variable declaration to get the type from * @param importRewrite the import rewrite to use, or <code>null</code> * @param context the import rewrite context, or <code>null</code> * @return a new type node created with the given AST. * * @since 3.7.1 */ public static Type newType(AST ast, VariableDeclaration declaration, ImportRewrite importRewrite, ImportRewriteContext context) { Type type= ASTNodes.getType(declaration); if (declaration instanceof SingleVariableDeclaration) { Type type2= ((SingleVariableDeclaration) declaration).getType(); if (type2 instanceof UnionType) { ITypeBinding typeBinding= type2.resolveBinding(); if (typeBinding != null) { if (importRewrite != null) { type= importRewrite.addImport(typeBinding, ast, context); return type; } else { String qualifiedName= typeBinding.getQualifiedName(); if (qualifiedName.length() > 0) { type= ast.newSimpleType(ast.newName(qualifiedName)); return type; } } } // XXX: fallback for intersection types or unresolved types: take first type of union type= (Type) ((UnionType) type2).types().get(0); return type; } } int extraDim= declaration.getExtraDimensions(); type= (Type) ASTNode.copySubtree(ast, type); for (int i= 0; i < extraDim; i++) { type= ast.newArrayType(type); } return type; }
public String initialize(CompilationUnit root, ASTNode node) { fASTRoot= root; if (!(node instanceof Name)) { return SearchMessages.ExceptionOccurrencesFinder_no_exception; } fSelectedName= ASTNodes.getTopMostName((Name)node); ASTNode parent= fSelectedName.getParent(); MethodDeclaration decl= resolveMethodDeclaration(parent); if (decl != null && methodThrowsException(decl, fSelectedName)) { fException= fSelectedName.resolveTypeBinding(); fStart= decl.getBody(); } else if (parent instanceof Type) { parent= parent.getParent(); if (parent instanceof UnionType) { parent= parent.getParent(); } if (parent instanceof SingleVariableDeclaration && parent.getParent() instanceof CatchClause) { CatchClause catchClause= (CatchClause)parent.getParent(); fTryStatement= (TryStatement)catchClause.getParent(); if (fTryStatement != null) { fException= fSelectedName.resolveTypeBinding(); fStart= fTryStatement.getBody(); } } } if (fException == null || fStart == null) return SearchMessages.ExceptionOccurrencesFinder_no_exception; fDescription= Messages.format(SearchMessages.ExceptionOccurrencesFinder_occurrence_description, BasicElementLabels.getJavaElementName(fException.getName())); return null; }
public boolean visit(UnionType node) { for (Iterator it = node.types().iterator(); it.hasNext(); ) { Type t = (Type) it.next(); t.accept(this); if (it.hasNext()) { this.buffer.append('|'); } } return false; }
@Override public boolean visit(CatchClause node) { //System.out.println("Found: " + node.getClass()); ArrayList<Type> types = new ArrayList<>(); // Handle union types in catch clauses if (node.getException().getType() instanceof UnionType) { //println("UNIONUNIONUNION"); UnionType ut = (UnionType)node.getException().getType(); for (Object o : ut.types()) { types.add((Type)o); } } else { types.add(node.getException().getType()); } for (Type t : types) { print("catch ("); t.accept(this); print(" "); node.getException().getName().accept(this); //node.getException().accept(this); println(") {"); indent++; node.getBody().accept(this); indent--; print("} "); } return false; }
@Override public boolean visit(final UnionType node) { return false; }
@Override public boolean visit(final UnionType node) { // not instrumentable, contains no instrumentable nodes return false; }
@Override public void endVisit(UnionType node) { endVisitNode(node); }
@Override public boolean visit(UnionType node) { return visitNode(node); }
public String initialize(CompilationUnit root, ASTNode node) { fASTRoot= root; if (node == null) return SearchMessages.ExceptionOccurrencesFinder_no_exception; MethodDeclaration method= ASTResolving.findParentMethodDeclaration(node); if (method == null) return SearchMessages.ExceptionOccurrencesFinder_no_exception; // The ExceptionOccurrencesFinder selects the whole type, no matter what part of it was selected. MethodExitsFinder behaves similar. if (node instanceof Name) { node= ASTNodes.getTopMostName((Name) node); } ASTNode parent= node.getParent(); if (node.getLocationInParent() == TagElement.FRAGMENTS_PROPERTY) { // in Javadoc tag: TagElement tagElement= (TagElement) parent; String tagName= tagElement.getTagName(); if (node instanceof Name && node == tagElement.fragments().get(0) && (TagElement.TAG_EXCEPTION.equals(tagName) || TagElement.TAG_THROWS.equals(tagName))) { fSelectedNode= node; fException= ((Name) node).resolveTypeBinding(); fStart= method; } } else { Type type= ASTNodes.getTopMostType(node); if (type == null) { return SearchMessages.ExceptionOccurrencesFinder_no_exception; } // in method's "throws" list: if (type.getLocationInParent() == MethodDeclaration.THROWN_EXCEPTION_TYPES_PROPERTY) { fSelectedNode= type; fException= type.resolveBinding(); fStart= method; } // in catch clause: Type topType= type; if (type.getLocationInParent() == UnionType.TYPES_PROPERTY) { topType= (Type) type.getParent(); } if (topType.getLocationInParent() == SingleVariableDeclaration.TYPE_PROPERTY && topType.getParent().getLocationInParent() == CatchClause.EXCEPTION_PROPERTY) { fSelectedNode= type; fException= type.resolveBinding(); fTryStatement= (TryStatement) topType.getParent().getParent().getParent(); fStart= fTryStatement.getBody(); } } if (fException == null || fStart == null) return SearchMessages.ExceptionOccurrencesFinder_no_exception; fDescription= Messages.format(SearchMessages.ExceptionOccurrencesFinder_occurrence_description, BasicElementLabels.getJavaElementName(fException.getName())); return null; }
private static boolean getPickoutTypeFromMulticatchProposals(IInvocationContext context, ASTNode node, ArrayList<ASTNode> coveredNodes, Collection<ICommandAccess> resultingCollections) { CatchClause catchClause= (CatchClause) ASTResolving.findAncestor(node, ASTNode.CATCH_CLAUSE); if (catchClause == null) { return false; } Statement statement= ASTResolving.findParentStatement(node); if (statement != catchClause.getParent() && statement != catchClause.getBody()) { return false; // selection is in a statement inside the body } Type type= catchClause.getException().getType(); if (!type.isUnionType()) { return false; } Type selectedMultiCatchType= null; if (type.isUnionType() && node instanceof Name) { Name topMostName= ASTNodes.getTopMostName((Name) node); ASTNode parent= topMostName.getParent(); if (parent instanceof SimpleType || parent instanceof NameQualifiedType) { selectedMultiCatchType= (Type) parent; } } boolean multipleExceptions= coveredNodes.size() > 1; if ((selectedMultiCatchType == null) && (!(node instanceof UnionType) || !multipleExceptions)) { return false; } if (!multipleExceptions) { coveredNodes.add(selectedMultiCatchType); } BodyDeclaration bodyDeclaration= ASTResolving.findParentBodyDeclaration(catchClause); if (!(bodyDeclaration instanceof MethodDeclaration) && !(bodyDeclaration instanceof Initializer)) { return false; } if (resultingCollections == null) { return true; } AST ast= bodyDeclaration.getAST(); ASTRewrite rewrite= ASTRewrite.create(ast); CatchClause newCatchClause= ast.newCatchClause(); SingleVariableDeclaration newSingleVariableDeclaration= ast.newSingleVariableDeclaration(); UnionType newUnionType= ast.newUnionType(); List<Type> types= newUnionType.types(); for (int i= 0; i < coveredNodes.size(); i++) { ASTNode typeNode= coveredNodes.get(i); types.add((Type) rewrite.createCopyTarget(typeNode)); rewrite.remove(typeNode, null); } newSingleVariableDeclaration.setType(newUnionType); newSingleVariableDeclaration.setName((SimpleName) rewrite.createCopyTarget(catchClause.getException().getName())); newCatchClause.setException(newSingleVariableDeclaration); setCatchClauseBody(newCatchClause, rewrite, catchClause); TryStatement tryStatement= (TryStatement)catchClause.getParent(); ListRewrite listRewrite= rewrite.getListRewrite(tryStatement, TryStatement.CATCH_CLAUSES_PROPERTY); listRewrite.insertAfter(newCatchClause, catchClause, null); Image image= JavaPluginImages.get(JavaPluginImages.IMG_OBJS_EXCEPTION); String label= !multipleExceptions ? CorrectionMessages.QuickAssistProcessor_move_exception_to_separate_catch_block : CorrectionMessages.QuickAssistProcessor_move_exceptions_to_separate_catch_block; ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.MOVE_EXCEPTION_TO_SEPERATE_CATCH_BLOCK, image); resultingCollections.add(proposal); return true; }
private static boolean getUnrollMultiCatchProposals(IInvocationContext context, ASTNode covering, Collection<ICommandAccess> resultingCollections) { if (!JavaModelUtil.is17OrHigher(context.getCompilationUnit().getJavaProject())) return false; CatchClause catchClause= (CatchClause) ASTResolving.findAncestor(covering, ASTNode.CATCH_CLAUSE); if (catchClause == null) { return false; } Statement statement= ASTResolving.findParentStatement(covering); if (statement != catchClause.getParent() && statement != catchClause.getBody()) { return false; // selection is in a statement inside the body } Type type1= catchClause.getException().getType(); Type selectedMultiCatchType= null; if (type1.isUnionType() && covering instanceof Name) { Name topMostName= ASTNodes.getTopMostName((Name) covering); ASTNode parent= topMostName.getParent(); if (parent instanceof SimpleType || parent instanceof NameQualifiedType) { selectedMultiCatchType= (Type) parent; } } if (selectedMultiCatchType != null) return false; SingleVariableDeclaration singleVariableDeclaration= catchClause.getException(); Type type= singleVariableDeclaration.getType(); if (!(type instanceof UnionType)) return false; if (resultingCollections == null) return true; AST ast= covering.getAST(); ASTRewrite rewrite= ASTRewrite.create(ast); TryStatement tryStatement= (TryStatement) catchClause.getParent(); ListRewrite listRewrite= rewrite.getListRewrite(tryStatement, TryStatement.CATCH_CLAUSES_PROPERTY); UnionType unionType= (UnionType) type; List<Type> types= unionType.types(); for (int i= types.size() - 1; i >= 0; i--) { Type type2= types.get(i); CatchClause newCatchClause= ast.newCatchClause(); SingleVariableDeclaration newSingleVariableDeclaration= ast.newSingleVariableDeclaration(); newSingleVariableDeclaration.setType((Type) rewrite.createCopyTarget(type2)); newSingleVariableDeclaration.setName((SimpleName) rewrite.createCopyTarget(singleVariableDeclaration.getName())); newCatchClause.setException(newSingleVariableDeclaration); setCatchClauseBody(newCatchClause, rewrite, catchClause); listRewrite.insertAfter(newCatchClause, catchClause, null); } rewrite.remove(catchClause, null); Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE); String label= CorrectionMessages.QuickAssistProcessor_convert_to_multiple_singletype_catch_blocks; ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.USE_SEPARATE_CATCH_BLOCKS, image); resultingCollections.add(proposal); return true; }
@Override public boolean visit(UnionType node) { return visit((Type)node); }
@Override public void endVisit(UnionType node) { endVisit((Type)node); }
@Override public boolean visit(UnionType node) { System.out.println("Found: " + node.getClass()); return super.visit(node); }