本文整理汇总了Java中com.sun.tools.javac.tree.JCTree.JCFieldAccess类的典型用法代码示例。如果您正苦于以下问题:Java JCFieldAccess类的具体用法?Java JCFieldAccess怎么用?Java JCFieldAccess使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
JCFieldAccess类属于com.sun.tools.javac.tree.JCTree包,在下文中一共展示了JCFieldAccess类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createField
import com.sun.tools.javac.tree.JCTree.JCFieldAccess; //导入依赖的package包/类
private static boolean createField(LoggingFramework framework, JavacNode typeNode, JCFieldAccess loggingType, JCTree source, String logFieldName, boolean useStatic, String loggerTopic) {
JavacTreeMaker maker = typeNode.getTreeMaker();
// private static final <loggerType> log = <factoryMethod>(<parameter>);
JCExpression loggerType = chainDotsString(typeNode, framework.getLoggerTypeName());
JCExpression factoryMethod = chainDotsString(typeNode, framework.getLoggerFactoryMethodName());
JCExpression loggerName;
if (loggerTopic == null || loggerTopic.trim().length() == 0) {
loggerName = framework.createFactoryParameter(typeNode, loggingType);
} else {
loggerName = maker.Literal(loggerTopic);
}
JCMethodInvocation factoryMethodCall = maker.Apply(List.<JCExpression>nil(), factoryMethod, List.<JCExpression>of(loggerName));
JCVariableDecl fieldDecl = recursiveSetGeneratedBy(maker.VarDef(
maker.Modifiers(Flags.PRIVATE | Flags.FINAL | (useStatic ? Flags.STATIC : 0)),
typeNode.toName(logFieldName), loggerType, factoryMethodCall), source, typeNode.getContext());
injectFieldAndMarkGenerated(typeNode, fieldDecl);
return true;
}
示例2: calculateGuess
import com.sun.tools.javac.tree.JCTree.JCFieldAccess; //导入依赖的package包/类
/**
* Turns an expression into a guessed intended literal. Only works for
* literals, as you can imagine.
*
* Will for example turn a TrueLiteral into 'Boolean.valueOf(true)'.
*/
public static Object calculateGuess(JCExpression expr) {
if (expr instanceof JCLiteral) {
JCLiteral lit = (JCLiteral) expr;
if (lit.getKind() == com.sun.source.tree.Tree.Kind.BOOLEAN_LITERAL) {
return ((Number) lit.value).intValue() == 0 ? false : true;
}
return lit.value;
} else if (expr instanceof JCIdent || expr instanceof JCFieldAccess) {
String x = expr.toString();
if (x.endsWith(".class")) x = x.substring(0, x.length() - 6);
else {
int idx = x.lastIndexOf('.');
if (idx > -1) x = x.substring(idx + 1);
}
return x;
} else
return null;
}
示例3: visitApply
import com.sun.tools.javac.tree.JCTree.JCFieldAccess; //导入依赖的package包/类
@Override public void visitApply(JCMethodInvocation tree) {
if (tree.typeargs.nonEmpty()) {
if (tree.meth instanceof JCFieldAccess) {
JCFieldAccess fa = (JCFieldAccess) tree.meth;
print(fa.selected);
print(".<");
print(tree.typeargs, ", ");
print(">");
print(fa.name);
} else {
print("<");
print(tree.typeargs, ", ");
print(">");
print(tree.meth);
}
} else {
print(tree.meth);
}
print("(");
print(tree.args, ", ");
print(")");
}
示例4: isConstructorCall
import com.sun.tools.javac.tree.JCTree.JCFieldAccess; //导入依赖的package包/类
public static boolean isConstructorCall(final JCStatement statement) {
if (!(statement instanceof JCExpressionStatement)) return false;
JCExpression expr = ((JCExpressionStatement) statement).expr;
if (!(expr instanceof JCMethodInvocation)) return false;
JCExpression invocation = ((JCMethodInvocation) expr).meth;
String name;
if (invocation instanceof JCFieldAccess) {
name = ((JCFieldAccess) invocation).name.toString();
} else if (invocation instanceof JCIdent) {
name = ((JCIdent) invocation).name.toString();
} else {
name = "";
}
return "super".equals(name) || "this".equals(name);
}
示例5: unpack
import com.sun.tools.javac.tree.JCTree.JCFieldAccess; //导入依赖的package包/类
private static void unpack(StringBuilder sb, JCExpression expr) {
if (expr instanceof JCIdent) {
sb.append(((JCIdent) expr).name.toString());
return;
}
if (expr instanceof JCFieldAccess) {
JCFieldAccess jcfa = (JCFieldAccess) expr;
unpack(sb, jcfa.selected);
sb.append(".").append(jcfa.name.toString());
return;
}
if (expr instanceof JCTypeApply) {
sb.setLength(0);
sb.append("ERR:");
sb.append("@Builder(toBuilder=true) is not supported if returning a type with generics applied to an intermediate.");
sb.append("__ERR__");
return;
}
sb.setLength(0);
sb.append("ERR:");
sb.append("Expected a type of some sort, not a " + expr.getClass().getName());
sb.append("__ERR__");
}
示例6: visit
import com.sun.tools.javac.tree.JCTree.JCFieldAccess; //导入依赖的package包/类
@Override
public JCTree visit(final PackageDeclaration n, final Object arg) {
//ARG0: JCExpression
// It returns a full qualified name
JCExpression arg0 = (JCExpression) n.getName().accept(this, arg);
/* TODO - Not supporting annotations
if (n.getAnnotations() != null) {
for (final AnnotationExpr a : n.getAnnotations()) {
JCTree result = a.accept(this, arg);
}
}
*/
if (arg0 instanceof JCIdent) {
return new AJCIdent((JCIdent) arg0, ((n.getComment() != null) ? n.getComment().getContent() : null));
}
return new AJCFieldAccess((JCFieldAccess) arg0, ((n.getComment() != null) ? n.getComment().getContent() : null));
}
示例7: visitApply
import com.sun.tools.javac.tree.JCTree.JCFieldAccess; //导入依赖的package包/类
public void visitApply(JCMethodInvocation tree) {
try {
if (!tree.typeargs.isEmpty()) {
if (SELECT.equals(treeTag(tree.meth))) {
JCFieldAccess left = (JCFieldAccess)tree.meth;
printExpr(left.selected);
print(".<");
printExprs(tree.typeargs);
print(">" + left.name);
} else {
print("<");
printExprs(tree.typeargs);
print(">");
printExpr(tree.meth);
}
} else {
printExpr(tree.meth);
}
print("(");
printExprs(tree.args);
print(")");
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
示例8: visitNewClass
import com.sun.tools.javac.tree.JCTree.JCFieldAccess; //导入依赖的package包/类
@Override
public Pair<ASTRecord, Integer> visitNewClass(NewClassTree node, Insertion ins) {
JCNewClass na = (JCNewClass) node;
JCExpression className = na.clazz;
// System.out.printf("classname %s (%s)%n", className, className.getClass());
while (! (className.getKind() == Tree.Kind.IDENTIFIER)) { // IdentifierTree
if (className instanceof JCAnnotatedType) {
className = ((JCAnnotatedType) className).underlyingType;
} else if (className instanceof JCTypeApply) {
className = ((JCTypeApply) className).clazz;
} else if (className instanceof JCFieldAccess) {
// This occurs for fully qualified names, e.g. "new java.lang.Object()".
// I'm not quite sure why the field "selected" is taken, but "name" would
// be a type mismatch. It seems to work, see NewPackage test case.
className = ((JCFieldAccess) className).selected;
} else {
throw new Error(String.format("unrecognized JCNewClass.clazz (%s): %s%n" +
" surrounding new class tree: %s%n", className.getClass(), className, node));
}
// System.out.printf("classname %s (%s)%n", className, className.getClass());
}
return visitIdentifier((IdentifierTree) className, ins);
}
示例9: receiverSameAsArgument
import com.sun.tools.javac.tree.JCTree.JCFieldAccess; //导入依赖的package包/类
/**
* Matches when the receiver of an instance method is the same reference as a particular argument
* to the method. For example, receiverSameAsArgument(1) would match {@code obj.method("", obj)}
*
* @param argNum The number of the argument to compare against (zero-based.
*/
public static Matcher<? super MethodInvocationTree> receiverSameAsArgument(final int argNum) {
return new Matcher<MethodInvocationTree>() {
@Override
public boolean matches(MethodInvocationTree t, VisitorState state) {
List<? extends ExpressionTree> args = t.getArguments();
if (args.size() <= argNum) {
return false;
}
ExpressionTree arg = args.get(argNum);
JCExpression methodSelect = (JCExpression) t.getMethodSelect();
if (methodSelect instanceof JCFieldAccess) {
JCFieldAccess fieldAccess = (JCFieldAccess) methodSelect;
return ASTHelpers.sameVariable(fieldAccess.getExpression(), arg);
} else if (methodSelect instanceof JCIdent) {
// A bare method call: "equals(foo)". Receiver is implicitly "this".
return "this".equals(arg.toString());
}
return false;
}
};
}
示例10: booleanConstant
import com.sun.tools.javac.tree.JCTree.JCFieldAccess; //导入依赖的package包/类
/**
* Matches the boolean constant ({@link Boolean#TRUE} or {@link Boolean#FALSE}) corresponding to
* the given value.
*/
public static Matcher<ExpressionTree> booleanConstant(final boolean value) {
return new Matcher<ExpressionTree>() {
@Override
public boolean matches(ExpressionTree expressionTree, VisitorState state) {
if (expressionTree instanceof JCFieldAccess) {
Symbol symbol = ASTHelpers.getSymbol(expressionTree);
if (symbol.isStatic()
&& state.getTypes().unboxedTypeOrType(symbol.type).getTag() == TypeTag.BOOLEAN) {
return ((value && symbol.getSimpleName().contentEquals("TRUE"))
|| symbol.getSimpleName().contentEquals("FALSE"));
}
}
return false;
}
};
}
示例11: selectedIsInstance
import com.sun.tools.javac.tree.JCTree.JCFieldAccess; //导入依赖的package包/类
/**
* Returns true if the expression is a member access on an instance, rather than a static type.
* Supports member method invocations and field accesses.
*/
public static Matcher<ExpressionTree> selectedIsInstance() {
return new Matcher<ExpressionTree>() {
@Override
public boolean matches(ExpressionTree expr, VisitorState state) {
if (!(expr instanceof JCFieldAccess)) {
// TODO(cushon): throw IllegalArgumentException?
return false;
}
JCExpression selected = ((JCFieldAccess) expr).getExpression();
if (selected instanceof JCNewClass) {
return true;
}
Symbol sym = ASTHelpers.getSymbol(selected);
return sym instanceof VarSymbol;
}
};
}
示例12: getSymbol
import com.sun.tools.javac.tree.JCTree.JCFieldAccess; //导入依赖的package包/类
/**
* Gets the symbol for a tree. Returns null if this tree does not have a symbol because it is of
* the wrong type, if {@code tree} is null, or if the symbol cannot be found due to a compilation
* error.
*/
// TODO(eaftan): refactor other code that accesses symbols to use this method
public static Symbol getSymbol(Tree tree) {
if (tree instanceof JCFieldAccess) {
return ((JCFieldAccess) tree).sym;
}
if (tree instanceof JCIdent) {
return ((JCIdent) tree).sym;
}
if (tree instanceof JCMethodInvocation) {
return ASTHelpers.getSymbol((MethodInvocationTree) tree);
}
if (tree instanceof JCNewClass) {
return ASTHelpers.getSymbol((NewClassTree) tree);
}
if (tree instanceof MemberReferenceTree) {
return ((JCMemberReference) tree).sym;
}
if (tree instanceof JCAnnotatedType) {
return getSymbol(((JCAnnotatedType) tree).underlyingType);
}
return getDeclaredSymbol(tree);
}
示例13: getReceiverType
import com.sun.tools.javac.tree.JCTree.JCFieldAccess; //导入依赖的package包/类
/**
* Returns the type of a receiver of a method call expression. Precondition: the expressionTree
* corresponds to a method call.
*
* <p>Examples:
*
* <pre>{@code
* a.b.foo() ==> type of a.b
* a.bar().foo() ==> type of a.bar()
* this.foo() ==> type of this
* foo() ==> type of this
* TheClass.aStaticMethod() ==> TheClass
* aStaticMethod() ==> type of class in which method is defined
* }</pre>
*/
public static Type getReceiverType(ExpressionTree expressionTree) {
if (expressionTree instanceof JCFieldAccess) {
JCFieldAccess methodSelectFieldAccess = (JCFieldAccess) expressionTree;
return methodSelectFieldAccess.selected.type;
} else if (expressionTree instanceof JCIdent) {
JCIdent methodCall = (JCIdent) expressionTree;
return methodCall.sym.owner.type;
} else if (expressionTree instanceof JCMethodInvocation) {
return getReceiverType(((JCMethodInvocation) expressionTree).getMethodSelect());
} else if (expressionTree instanceof JCMemberReference) {
return ((JCMemberReference) expressionTree).getQualifierExpression().type;
}
throw new IllegalArgumentException(
"Expected a JCFieldAccess or JCIdent from expression " + expressionTree);
}
示例14: isGetListMethodInvocation
import com.sun.tools.javac.tree.JCTree.JCFieldAccess; //导入依赖的package包/类
private static boolean isGetListMethodInvocation(ExpressionTree tree, VisitorState state) {
if (tree.getKind() == Tree.Kind.METHOD_INVOCATION) {
MethodInvocationTree method = (MethodInvocationTree) tree;
if (!method.getArguments().isEmpty()) {
return false;
}
if (!returnsListMatcher.matches(method, state)) {
return false;
}
ExpressionTree expressionTree = method.getMethodSelect();
if (expressionTree instanceof JCFieldAccess) {
JCFieldAccess access = (JCFieldAccess) expressionTree;
String methodName = access.sym.getQualifiedName().toString();
return isFieldGetMethod(methodName);
}
return true;
}
return false;
}
示例15: isGetMethodInvocation
import com.sun.tools.javac.tree.JCTree.JCFieldAccess; //导入依赖的package包/类
private static boolean isGetMethodInvocation(ExpressionTree tree, VisitorState state) {
if (tree.getKind() == Tree.Kind.METHOD_INVOCATION) {
MethodInvocationTree method = (MethodInvocationTree) tree;
if (!method.getArguments().isEmpty()) {
return false;
}
if (returnsListMatcher.matches(method, state)) {
return false;
}
ExpressionTree expressionTree = method.getMethodSelect();
if (expressionTree instanceof JCFieldAccess) {
JCFieldAccess access = (JCFieldAccess) expressionTree;
String methodName = access.sym.getQualifiedName().toString();
return isFieldGetMethod(methodName);
}
return true;
}
return false;
}