本文整理汇总了Java中com.sun.tools.javac.tree.JCTree.JCMethodDecl类的典型用法代码示例。如果您正苦于以下问题:Java JCMethodDecl类的具体用法?Java JCMethodDecl怎么用?Java JCMethodDecl使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
JCMethodDecl类属于com.sun.tools.javac.tree.JCTree包,在下文中一共展示了JCMethodDecl类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: isSynthetic
import com.sun.tools.javac.tree.JCTree.JCMethodDecl; //导入依赖的package包/类
private boolean isSynthetic(CompilationUnitTree cut, Tree leaf) {
JCTree tree = (JCTree) leaf;
if (tree.pos == (-1))
return true;
if (leaf.getKind() == Tree.Kind.METHOD) {
//check for synthetic constructor:
return (((JCMethodDecl)leaf).mods.flags & Flags.GENERATEDCONSTR) != 0L;
}
//check for synthetic superconstructor call:
if (leaf.getKind() == Tree.Kind.EXPRESSION_STATEMENT) {
ExpressionStatementTree est = (ExpressionStatementTree) leaf;
if (est.getExpression().getKind() == Tree.Kind.METHOD_INVOCATION) {
MethodInvocationTree mit = (MethodInvocationTree) est.getExpression();
if (mit.getMethodSelect().getKind() == Tree.Kind.IDENTIFIER) {
IdentifierTree it = (IdentifierTree) mit.getMethodSelect();
if ("super".equals(it.getName().toString())) {
return sp.getEndPosition(cut, leaf) == (-1);
}
}
}
}
return false;
}
示例2: findNameSpan
import com.sun.tools.javac.tree.JCTree.JCMethodDecl; //导入依赖的package包/类
/**Find span of the {@link MethodTree#getName()} identifier in the source.
* Returns starting and ending offset of the name in the source code that was parsed
* (ie. {@link CompilationInfo.getText()}, which may differ from the positions in the source
* document if it has been already altered.
*
* @param method method which name should be searched for
* @return the span of the name, or null if cannot be found
* @since 0.25
*/
public int[] findNameSpan(MethodTree method) {
if (isSynthetic(info.getCompilationUnit(), method)) {
return null;
}
JCMethodDecl jcm = (JCMethodDecl) method;
String name;
if (jcm.name == jcm.name.table.names.init) {
TreePath path = info.getTrees().getPath(info.getCompilationUnit(), jcm);
if (path == null) {
return null;
}
Element em = info.getTrees().getElement(path);
Element clazz;
if (em == null || (clazz = em.getEnclosingElement()) == null || !clazz.getKind().isClass()) {
return null;
}
name = clazz.getSimpleName().toString();
} else {
name = method.getName().toString();
}
return findNameSpan(name, method);
}
示例3: visitMethodDef
import com.sun.tools.javac.tree.JCTree.JCMethodDecl; //导入依赖的package包/类
@Override
public void visitMethodDef(JCMethodDecl tree) {
cancelService.abortIfCanceled();
JCBlock body = tree.body;
try {
super.visitMethodDef(tree);
} finally {
//reinstall body:
tree.body = body;
}
if (trees instanceof NBJavacTrees && !env.enclClass.defs.contains(tree)) {
TreePath path = trees.getPath(env.toplevel, env.enclClass);
if (path != null) {
((NBJavacTrees)trees).addPathForElement(tree.sym, new TreePath(path, tree));
}
}
}
示例4: methodParamIndex
import com.sun.tools.javac.tree.JCTree.JCMethodDecl; //导入依赖的package包/类
private int methodParamIndex(List<JCTree> path, JCTree param) {
List<JCTree> curr = path;
while (curr.head.getTag() != Tag.METHODDEF &&
curr.head.getTag() != Tag.LAMBDA) {
curr = curr.tail;
}
if (curr.head.getTag() == Tag.METHODDEF) {
JCMethodDecl method = (JCMethodDecl)curr.head;
return method.params.indexOf(param);
} else if (curr.head.getTag() == Tag.LAMBDA) {
JCLambda lambda = (JCLambda)curr.head;
return lambda.params.indexOf(param);
} else {
Assert.error("methodParamIndex expected to find method or lambda for param: " + param);
return -1;
}
}
示例5: visitMethodDef
import com.sun.tools.javac.tree.JCTree.JCMethodDecl; //导入依赖的package包/类
/**
* methods: remove method bodies, make methods native
*/
@Override
public void visitMethodDef(JCMethodDecl tree) {
tree.mods = translate(tree.mods);
tree.restype = translate(tree.restype);
tree.typarams = translateTypeParams(tree.typarams);
tree.params = translateVarDefs(tree.params);
tree.thrown = translate(tree.thrown);
if (tree.body != null) {
if ((currClassMods & Flags.INTERFACE) != 0) {
tree.mods.flags &= ~(Flags.DEFAULT | Flags.STATIC);
} else {
tree.mods.flags |= Flags.NATIVE;
}
tree.body = null;
}
result = tree;
}
示例6: translateTopLevelClass
import com.sun.tools.javac.tree.JCTree.JCMethodDecl; //导入依赖的package包/类
@Override
public List<JCTree> translateTopLevelClass(Env<AttrContext> env, JCTree cdef, TreeMaker make) {
List<JCTree> result = super.translateTopLevelClass(env, cdef, make);
Map<Symbol, JCMethodDecl> declarations = new HashMap<>();
Set<Symbol> toDump = new TreeSet<>(symbolComparator);
new TreeScanner() {
@Override
public void visitMethodDef(JCMethodDecl tree) {
if (tree.name.toString().startsWith("dump")) {
toDump.add(tree.sym);
}
declarations.put(tree.sym, tree);
super.visitMethodDef(tree);
}
}.scan(result);
for (Symbol d : toDump) {
dump(d, declarations, new HashSet<>());
}
return result;
}
示例7: buildTree
import com.sun.tools.javac.tree.JCTree.JCMethodDecl; //导入依赖的package包/类
/** {@inheritDoc} */
@Override protected JavacNode buildTree(JCTree node, Kind kind) {
switch (kind) {
case COMPILATION_UNIT:
return buildCompilationUnit((JCCompilationUnit) node);
case TYPE:
return buildType((JCClassDecl) node);
case FIELD:
return buildField((JCVariableDecl) node);
case INITIALIZER:
return buildInitializer((JCBlock) node);
case METHOD:
return buildMethod((JCMethodDecl) node);
case ARGUMENT:
return buildLocalVar((JCVariableDecl) node, kind);
case LOCAL:
return buildLocalVar((JCVariableDecl) node, kind);
case STATEMENT:
return buildStatementOrExpression(node);
case ANNOTATION:
return buildAnnotation((JCAnnotation) node, false);
default:
throw new AssertionError("Did not expect: " + kind);
}
}
示例8: buildType
import com.sun.tools.javac.tree.JCTree.JCMethodDecl; //导入依赖的package包/类
private JavacNode buildType(JCClassDecl type) {
if (setAndGetAsHandled(type)) return null;
List<JavacNode> childNodes = new ArrayList<JavacNode>();
for (JCAnnotation annotation : type.mods.annotations) addIfNotNull(childNodes, buildAnnotation(annotation, false));
for (JCTree def : type.defs) {
/* A def can be:
* JCClassDecl for inner types
* JCMethodDecl for constructors and methods
* JCVariableDecl for fields
* JCBlock for (static) initializers
*/
if (def instanceof JCMethodDecl) addIfNotNull(childNodes, buildMethod((JCMethodDecl)def));
else if (def instanceof JCClassDecl) addIfNotNull(childNodes, buildType((JCClassDecl)def));
else if (def instanceof JCVariableDecl) addIfNotNull(childNodes, buildField((JCVariableDecl)def));
else if (def instanceof JCBlock) addIfNotNull(childNodes, buildInitializer((JCBlock)def));
}
return putInMap(new JavacNode(this, type, childNodes, Kind.TYPE));
}
示例9: constructorExists
import com.sun.tools.javac.tree.JCTree.JCMethodDecl; //导入依赖的package包/类
/**
* Checks if there is a (non-default) constructor. In case of multiple constructors (overloading), only
* the first constructor decides if EXISTS_BY_USER or EXISTS_BY_LOMBOK is returned.
*
* @param node Any node that represents the Type (JCClassDecl) to look in, or any child node thereof.
*/
public static MemberExistsResult constructorExists(JavacNode node) {
node = upToTypeNode(node);
if (node != null && node.get() instanceof JCClassDecl) {
top: for (JCTree def : ((JCClassDecl)node.get()).defs) {
if (def instanceof JCMethodDecl) {
JCMethodDecl md = (JCMethodDecl) def;
if (md.name.contentEquals("<init>")) {
if ((md.mods.flags & Flags.GENERATEDCONSTR) != 0) continue;
List<JCAnnotation> annotations = md.getModifiers().getAnnotations();
if (annotations != null) for (JCAnnotation anno : annotations) {
if (typeMatches(Tolerate.class, node, anno.getAnnotationType())) continue top;
}
return getGeneratedBy(def) == null ? MemberExistsResult.EXISTS_BY_USER : MemberExistsResult.EXISTS_BY_LOMBOK;
}
}
}
}
return MemberExistsResult.NOT_EXISTS;
}
示例10: handle
import com.sun.tools.javac.tree.JCTree.JCMethodDecl; //导入依赖的package包/类
@Override public void handle(AnnotationValues<SneakyThrows> annotation, JCAnnotation ast, JavacNode annotationNode) {
handleFlagUsage(annotationNode, ConfigurationKeys.SNEAKY_THROWS_FLAG_USAGE, "@SneakyThrows");
deleteAnnotationIfNeccessary(annotationNode, SneakyThrows.class);
Collection<String> exceptionNames = annotation.getRawExpressions("value");
if (exceptionNames.isEmpty()) {
exceptionNames = Collections.singleton("java.lang.Throwable");
}
java.util.List<String> exceptions = new ArrayList<String>();
for (String exception : exceptionNames) {
if (exception.endsWith(".class")) exception = exception.substring(0, exception.length() - 6);
exceptions.add(exception);
}
JavacNode owner = annotationNode.up();
switch (owner.getKind()) {
case METHOD:
handleMethod(annotationNode, (JCMethodDecl)owner.get(), exceptions);
break;
default:
annotationNode.addError("@SneakyThrows is legal only on methods and constructors.");
break;
}
}
示例11: generateCleanMethod
import com.sun.tools.javac.tree.JCTree.JCMethodDecl; //导入依赖的package包/类
private JCMethodDecl generateCleanMethod(java.util.List<BuilderFieldData> builderFields, JavacNode type, JCTree source) {
JavacTreeMaker maker = type.getTreeMaker();
ListBuffer<JCStatement> statements = new ListBuffer<JCStatement>();
for (BuilderFieldData bfd : builderFields) {
if (bfd.singularData != null && bfd.singularData.getSingularizer() != null) {
bfd.singularData.getSingularizer().appendCleaningCode(bfd.singularData, type, source, statements);
}
}
statements.append(maker.Exec(maker.Assign(maker.Select(maker.Ident(type.toName("this")), type.toName("$lombokUnclean")), maker.Literal(CTC_BOOLEAN, false))));
JCBlock body = maker.Block(0, statements.toList());
return maker.MethodDef(maker.Modifiers(Flags.PUBLIC), type.toName("$lombokClean"), maker.Type(Javac.createVoidType(maker, CTC_VOID)), List.<JCTypeParameter>nil(), List.<JCVariableDecl>nil(), List.<JCExpression>nil(), body, null);
/*
* if (shouldReturnThis) {
methodType = cloneSelfType(field);
}
if (methodType == null) {
//WARNING: Do not use field.getSymbolTable().voidType - that field has gone through non-backwards compatible API changes within javac1.6.
methodType = treeMaker.Type(Javac.createVoidType(treeMaker, CTC_VOID));
shouldReturnThis = false;
}
*/
}
示例12: generateBuilderMethod
import com.sun.tools.javac.tree.JCTree.JCMethodDecl; //导入依赖的package包/类
public JCMethodDecl generateBuilderMethod(boolean isStatic, String builderMethodName, String builderClassName, JavacNode type, List<JCTypeParameter> typeParams) {
JavacTreeMaker maker = type.getTreeMaker();
ListBuffer<JCExpression> typeArgs = new ListBuffer<JCExpression>();
for (JCTypeParameter typeParam : typeParams) {
typeArgs.append(maker.Ident(typeParam.name));
}
JCExpression call = maker.NewClass(null, List.<JCExpression>nil(), namePlusTypeParamsToTypeReference(maker, type.toName(builderClassName), typeParams), List.<JCExpression>nil(), null);
JCStatement statement = maker.Return(call);
JCBlock body = maker.Block(0, List.<JCStatement>of(statement));
int modifiers = Flags.PUBLIC;
if (isStatic) modifiers |= Flags.STATIC;
return maker.MethodDef(maker.Modifiers(modifiers), type.toName(builderMethodName), namePlusTypeParamsToTypeReference(maker, type.toName(builderClassName), typeParams), copyTypeParams(maker, typeParams), List.<JCVariableDecl>nil(), List.<JCExpression>nil(), body, null);
}
示例13: createCanEqual
import com.sun.tools.javac.tree.JCTree.JCMethodDecl; //导入依赖的package包/类
public JCMethodDecl createCanEqual(JavacNode typeNode, JCTree source, List<JCAnnotation> onParam) {
/* protected boolean canEqual(final java.lang.Object other) {
* return other instanceof Outer.Inner.MyType;
* }
*/
JavacTreeMaker maker = typeNode.getTreeMaker();
JCModifiers mods = maker.Modifiers(Flags.PROTECTED, List.<JCAnnotation>nil());
JCExpression returnType = maker.TypeIdent(CTC_BOOLEAN);
Name canEqualName = typeNode.toName("canEqual");
JCExpression objectType = genJavaLangTypeRef(typeNode, "Object");
Name otherName = typeNode.toName("other");
long flags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, typeNode.getContext());
List<JCVariableDecl> params = List.of(maker.VarDef(maker.Modifiers(flags, onParam), otherName, objectType, null));
JCBlock body = maker.Block(0, List.<JCStatement>of(
maker.Return(maker.TypeTest(maker.Ident(otherName), createTypeReference(typeNode)))));
return recursiveSetGeneratedBy(maker.MethodDef(mods, canEqualName, returnType, List.<JCTypeParameter>nil(), params, List.<JCExpression>nil(), body, null), source, typeNode.getContext());
}
示例14: generateClearMethod
import com.sun.tools.javac.tree.JCTree.JCMethodDecl; //导入依赖的package包/类
private void generateClearMethod(JavacTreeMaker maker, JCExpression returnType, JCStatement returnStatement, SingularData data, JavacNode builderType, JCTree source) {
JCModifiers mods = maker.Modifiers(Flags.PUBLIC);
List<JCTypeParameter> typeParams = List.nil();
List<JCExpression> thrown = List.nil();
List<JCVariableDecl> params = List.nil();
List<JCExpression> jceBlank = List.nil();
JCExpression thisDotKeyField = chainDots(builderType, "this", data.getPluralName() + "$key");
JCExpression thisDotKeyFieldDotClear = chainDots(builderType, "this", data.getPluralName() + "$key", "clear");
JCExpression thisDotValueFieldDotClear = chainDots(builderType, "this", data.getPluralName() + "$value", "clear");
JCStatement clearKeyCall = maker.Exec(maker.Apply(jceBlank, thisDotKeyFieldDotClear, jceBlank));
JCStatement clearValueCall = maker.Exec(maker.Apply(jceBlank, thisDotValueFieldDotClear, jceBlank));
JCExpression cond = maker.Binary(CTC_NOT_EQUAL, thisDotKeyField, maker.Literal(CTC_BOT, null));
JCBlock clearCalls = maker.Block(0, List.of(clearKeyCall, clearValueCall));
JCStatement ifSetCallClear = maker.If(cond, clearCalls, null);
List<JCStatement> statements = returnStatement != null ? List.of(ifSetCallClear, returnStatement) : List.of(ifSetCallClear);
JCBlock body = maker.Block(0, statements);
Name methodName = builderType.toName(HandlerUtil.buildAccessorName("clear", data.getPluralName().toString()));
JCMethodDecl method = maker.MethodDef(mods, methodName, returnType, typeParams, params, thrown, body, null);
injectMethod(builderType, method);
}
示例15: generateClearMethod
import com.sun.tools.javac.tree.JCTree.JCMethodDecl; //导入依赖的package包/类
private void generateClearMethod(JavacTreeMaker maker, JCExpression returnType, JCStatement returnStatement, SingularData data, JavacNode builderType, JCTree source) {
JCModifiers mods = maker.Modifiers(Flags.PUBLIC);
List<JCTypeParameter> typeParams = List.nil();
List<JCExpression> thrown = List.nil();
List<JCVariableDecl> params = List.nil();
List<JCExpression> jceBlank = List.nil();
JCExpression thisDotField = maker.Select(maker.Ident(builderType.toName("this")), data.getPluralName());
JCExpression thisDotFieldDotClear = maker.Select(maker.Select(maker.Ident(builderType.toName("this")), data.getPluralName()), builderType.toName("clear"));
JCStatement clearCall = maker.Exec(maker.Apply(jceBlank, thisDotFieldDotClear, jceBlank));
JCExpression cond = maker.Binary(CTC_NOT_EQUAL, thisDotField, maker.Literal(CTC_BOT, null));
JCStatement ifSetCallClear = maker.If(cond, clearCall, null);
List<JCStatement> statements = returnStatement != null ? List.of(ifSetCallClear, returnStatement) : List.of(ifSetCallClear);
JCBlock body = maker.Block(0, statements);
Name methodName = builderType.toName(HandlerUtil.buildAccessorName("clear", data.getPluralName().toString()));
JCMethodDecl method = maker.MethodDef(mods, methodName, returnType, typeParams, params, thrown, body, null);
injectMethod(builderType, method);
}