Java Code Examples for com.sun.source.tree.MethodTree#getThrows()
The following examples show how to use
com.sun.source.tree.MethodTree#getThrows() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MethodTest3.java From netbeans with Apache License 2.0 | 6 votes |
public Void visitMethod(MethodTree node, Object p) { super.visitMethod(node, p); Element al = model.getElement(node); if ("foo()".equals(al.toString())) { List<ExpressionTree> l = new ArrayList<ExpressionTree>(); int i=0; for (ExpressionTree n: node.getThrows()) { if (i!=index) { l.add(n); } } MethodTree njuMethod = make.Method( node.getModifiers(), node.getName(), (ExpressionTree) node.getReturnType(), node.getTypeParameters(), node.getParameters(), l, node.getBody(), (ExpressionTree) node.getDefaultValue() ); model.setElement(njuMethod, al); copy.rewrite(node, njuMethod); } return null; }
Example 2
Source File: JavadocUtilities.java From netbeans with Apache License 2.0 | 5 votes |
private static boolean isInHeader(CompilationInfo info, MethodTree tree, int offset) { CompilationUnitTree cut = info.getCompilationUnit(); SourcePositions sp = info.getTrees().getSourcePositions(); long lastKnownOffsetInHeader = sp.getStartPosition(cut, tree); List<? extends ExpressionTree> throwz; List<? extends VariableTree> params; List<? extends TypeParameterTree> typeparams; if ((throwz = tree.getThrows()) != null && !throwz.isEmpty()) { lastKnownOffsetInHeader = sp.getEndPosition(cut, throwz.get(throwz.size() - 1)); } else if ((params = tree.getParameters()) != null && !params.isEmpty()) { lastKnownOffsetInHeader = sp.getEndPosition(cut, params.get(params.size() - 1)); } else if ((typeparams = tree.getTypeParameters()) != null && !typeparams.isEmpty()) { lastKnownOffsetInHeader = sp.getEndPosition(cut, typeparams.get(typeparams.size() - 1)); } else if (tree.getReturnType() != null) { lastKnownOffsetInHeader = sp.getEndPosition(cut, tree.getReturnType()); } else if (tree.getModifiers() != null) { lastKnownOffsetInHeader = sp.getEndPosition(cut, tree.getModifiers()); } TokenSequence<JavaTokenId> ts = info.getTreeUtilities().tokensFor(tree); ts.move((int) lastKnownOffsetInHeader); while (ts.moveNext()) { if (ts.token().id() == JavaTokenId.LBRACE || ts.token().id() == JavaTokenId.SEMICOLON) { return offset < ts.offset(); } } return false; }
Example 3
Source File: CreateElementUtilities.java From netbeans with Apache License 2.0 | 4 votes |
private static List<? extends TypeMirror> computeMethod(Set<ElementKind> types, CompilationInfo info, TreePath parent, TypeMirror[] typeParameterBound, Tree error, int offset) { //class or field: //check the error is in the body: //#92419: check for abstract method/method without body: MethodTree mt = (MethodTree) parent.getLeaf(); if (mt.getReturnType() == error) { types.add(ElementKind.CLASS); types.add(ElementKind.INTERFACE); types.add(ElementKind.ENUM); } List<? extends ExpressionTree> throwList = mt.getThrows(); if (throwList != null && !throwList.isEmpty()) { for (ExpressionTree t : throwList) { if (t == error) { types.add(ElementKind.CLASS); typeParameterBound[0] = info.getElements().getTypeElement("java.lang.Exception").asType(); break; } } } if (mt.getBody() == null) { return null; } try { Document doc = info.getDocument(); if (doc != null) {//XXX int bodyStart = findBodyStart(parent.getLeaf(), info.getCompilationUnit(), info.getTrees().getSourcePositions(), doc); int bodyEnd = (int) info.getTrees().getSourcePositions().getEndPosition(info.getCompilationUnit(), parent.getLeaf()); types.add(ElementKind.PARAMETER); types.add(ElementKind.LOCAL_VARIABLE); types.add(ElementKind.FIELD); if (bodyStart <= offset && offset <= bodyEnd) return Collections.singletonList(info.getElements().getTypeElement("java.lang.Object").asType()); } } catch (IOException ex) { Logger.getLogger("global").log(Level.INFO, ex.getMessage(), ex); } return null; }
Example 4
Source File: CreateElementUtilities.java From netbeans with Apache License 2.0 | 4 votes |
private static List<? extends TypeMirror> computeMethod(Set<ElementKind> types, CompilationInfo info, TreePath parent, TypeMirror[] typeParameterBound, Tree error, int offset) { //class or field: //check the error is in the body: //#92419: check for abstract method/method without body: MethodTree mt = (MethodTree) parent.getLeaf(); if (mt.getReturnType() == error) { types.add(ElementKind.CLASS); types.add(ElementKind.INTERFACE); types.add(ElementKind.ENUM); } List<? extends ExpressionTree> throwList = mt.getThrows(); if (throwList != null && !throwList.isEmpty()) { for (ExpressionTree t : throwList) { if (t == error) { types.add(ElementKind.CLASS); TypeElement tel = info.getElements().getTypeElement("java.lang.Exception"); if (tel == null) { return null; } typeParameterBound[0] = tel.asType(); break; } } } if (mt.getBody() == null) { return null; } try { Document doc = info.getDocument(); if (doc != null) {//XXX int bodyStart = Utilities.findBodyStart(info, parent.getLeaf(), info.getCompilationUnit(), info.getTrees().getSourcePositions(), doc); int bodyEnd = (int) info.getTrees().getSourcePositions().getEndPosition(info.getCompilationUnit(), parent.getLeaf()); types.add(ElementKind.PARAMETER); types.add(ElementKind.LOCAL_VARIABLE); types.add(ElementKind.FIELD); if (bodyStart <= offset && offset <= bodyEnd) { return typeMirrorCollection(info, "java.lang.Object"); } } } catch (IOException ex) { Logger.getLogger("global").log(Level.INFO, ex.getMessage(), ex); } return null; }
Example 5
Source File: ExpectedTypeResolver.java From netbeans with Apache License 2.0 | 4 votes |
/** * Computes possible types for throw expression. Throw can safely throw an exception, which is * either declared by method as a thrown type, or catched within method, by an upper try-catch block. * Unchecked exceptions are permitted (derivatives of RuntimeException or Error). */ @Override public List<? extends TypeMirror> visitThrow(ThrowTree node, Object p) { List<TypeMirror> result = new ArrayList<TypeMirror>(); TreePath parents = getCurrentPath(); Tree prev = null; while (parents != null && parents.getLeaf().getKind() != Tree.Kind.METHOD) { Tree l = parents.getLeaf(); if (l.getKind() == Tree.Kind.TRY) { TryTree tt = (TryTree) l; if (prev == tt.getBlock()) { for (CatchTree ct : tt.getCatches()) { TypeMirror ex = info.getTrees().getTypeMirror(new TreePath(getCurrentPath(), ct.getParameter().getType())); if (ex != null) { switch (ex.getKind()) { case DECLARED: if (!result.contains(ex)) { result.add(ex); } break; case UNION: for (TypeMirror t : ((UnionType) ex).getAlternatives()) { if (!result.contains(t)) { result.add(t); } } break; } } } } } prev = l; parents = parents.getParentPath(); } if (parents != null) { MethodTree mt = (MethodTree) parents.getLeaf(); for (ExpressionTree etree : mt.getThrows()) { TypeMirror m = info.getTrees().getTypeMirror(new TreePath(parents, etree)); if (m != null && !result.contains(m)) { result.add(m); } } } TypeMirror jlre = info.getElements().getTypeElement("java.lang.RuntimeException").asType(); // NOI18N TypeMirror jler = info.getElements().getTypeElement("java.lang.Error").asType(); // NOI18N for (TypeMirror em : result) { if (jlre != null && info.getTypes().isAssignable(jlre, em)) { jlre = null; } if (jler != null && info.getTypes().isAssignable(jler, em)) { jler = null; } if (jlre == null && jler == null) { break; } } if (jlre != null) { result.add(jlre); } if (jler != null) { result.add(jler); } return result; }