本文整理匯總了Java中com.sun.source.tree.MethodTree.getThrows方法的典型用法代碼示例。如果您正苦於以下問題:Java MethodTree.getThrows方法的具體用法?Java MethodTree.getThrows怎麽用?Java MethodTree.getThrows使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.sun.source.tree.MethodTree
的用法示例。
在下文中一共展示了MethodTree.getThrows方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: visitMethod
import com.sun.source.tree.MethodTree; //導入方法依賴的package包/類
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;
}
示例2: isInHeader
import com.sun.source.tree.MethodTree; //導入方法依賴的package包/類
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;
}
示例3: computeMethod
import com.sun.source.tree.MethodTree; //導入方法依賴的package包/類
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;
}
示例4: computeMethod
import com.sun.source.tree.MethodTree; //導入方法依賴的package包/類
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;
}
示例5: visitThrow
import com.sun.source.tree.MethodTree; //導入方法依賴的package包/類
/**
* 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;
}