private static JCIdent createIdent(TreeMaker treeMaker, Symbol symbol) { JCIdent newFieldIdent = treeMaker.Ident(symbol.name); newFieldIdent.type = symbol.type; newFieldIdent.sym = symbol; return newFieldIdent; }
private JCExpression makeSelectExpression( Inliner inliner, List<String> qualifiedPath, int start) { Iterator<String> selects = qualifiedPath.listIterator(start); TreeMaker maker = inliner.maker(); JCExpression select = maker.Ident(inliner.asName(selects.next())); while (selects.hasNext()) { select = maker.Select(select, inliner.asName(selects.next())); } return select; } },
@Override public JCExpression classReference( Inliner inliner, CharSequence topLevelClazz, CharSequence fullyQualifiedClazz) { if (Refaster.class.getName().contentEquals(fullyQualifiedClazz)) { // Special handling to ensure that the pretty-printer always recognizes Refaster references return inliner.maker().Ident(inliner.asName("Refaster")); } String packge = topLevelClazz.toString(); int lastDot = packge.lastIndexOf('.'); packge = (lastDot >= 0) ? packge.substring(0, lastDot) : ""; PackageSymbol currentPackage = inliner.getContext().get(PackageSymbol.class); if (currentPackage == null || !currentPackage.getQualifiedName().contentEquals(packge) || !topLevelClazz.toString().contentEquals(fullyQualifiedClazz)) { // don't import classes from the same package as the class we're refactoring inliner.addImport(fullyQualifiedClazz.toString()); } String simpleName = fullyQualifiedClazz.toString(); simpleName = simpleName.substring(simpleName.lastIndexOf('.') + 1); return inliner.maker().Ident(inliner.asName(simpleName)); }
@Override public JCIdent inline(Inliner inliner) throws CouldNotResolveImportException { Optional<LocalVarBinding> binding = inliner.getOptionalBinding(key()); return inliner .maker() .Ident(binding.isPresent() ? binding.get().getName() : getName().inline(inliner)); } }
@Override public JCExpression staticReference( Inliner inliner, CharSequence topLevelClazz, CharSequence fullyQualifiedClazz, CharSequence member) { if (Refaster.class.getName().contentEquals(topLevelClazz)) { // Special handling to ensure that the pretty-printer always recognizes Refaster references return inliner .maker() .Select(inliner.maker().Ident(inliner.asName("Refaster")), inliner.asName(member)); } inliner.addStaticImport(fullyQualifiedClazz + "." + member); return inliner.maker().Ident(inliner.asName(member)); } };
if (Refaster.class.getName().contentEquals(fullyQualifiedClazz)) { return inliner.maker().Ident(inliner.asName("Refaster"));
private Symbol attribIdent(String name) { Attr attr = Attr.instance(context); TreeMaker tm = TreeMaker.instance(context); return attr.attribIdent(tm.Ident(getName(name)), compilationUnit); }
private JCMethodInvocation createSyntheticShadowAccess(VisitorState state) { TreeMaker treeMaker = state.getTreeMaker(); JCExpression application = treeMaker.Select( treeMaker.Ident(findSymbol(state, "org.robolectric.RuntimeEnvironment")), findSymbol(state, "org.robolectric.RuntimeEnvironment", "application")); JCExpression shadowOfApp = treeMaker.Select( treeMaker.Ident(findSymbol(state, "org.robolectric.Shadows")), findSymbol(state, "org.robolectric.Shadows", "shadowOf(android.app.Application)")); JCMethodInvocation callShadowOf = treeMaker.Apply(null, shadowOfApp, com.sun.tools.javac.util.List.of(application)); callShadowOf.type = callShadowOf.meth.type; return callShadowOf; }
@Override public JCExpression staticReference( Inliner inliner, CharSequence topLevelClazz, CharSequence fullyQualifiedClazz, CharSequence member) { if (Refaster.class.getName().contentEquals(topLevelClazz)) { // Special handling to ensure that the pretty-printer always recognizes Refaster references return inliner .maker() .Select(inliner.maker().Ident(inliner.asName("Refaster")), inliner.asName(member)); } return inliner .maker() .Select( classReference(inliner, topLevelClazz, fullyQualifiedClazz), inliner.asName(member)); } },
private static JCMethodInvocation createSyntheticShadowAccess( MethodInvocationTree shadowOfCall, String newFieldName, Symbol originalSymbol, VisitorState state) { TreeMaker treeMaker = state.getTreeMaker(); Symbol newSymbol = createSymbol(originalSymbol, state.getName(newFieldName), ((JCExpression) shadowOfCall.getArguments().get(0)).type); JCExpression methodSelect = (JCExpression) shadowOfCall.getMethodSelect(); if (methodSelect instanceof JCIdent) { // clone so start pos can be changed... methodSelect = treeMaker.Ident(((JCIdent) shadowOfCall.getMethodSelect()).sym); } JCMethodInvocation callShadowOf = treeMaker.Apply( null, methodSelect, com.sun.tools.javac.util.List.of(createIdent(treeMaker, newSymbol))); callShadowOf.type = ((JCMethodInvocation) shadowOfCall).type; return callShadowOf; }
@Override public JCExpression inline(Inliner inliner) throws CouldNotResolveImportException { JCExpression expression = getExpression().inline(inliner); if (expression.toString().equals(CONVERT_TO_IDENT)) { return inliner.maker().Ident(getIdentifier().inline(inliner)); } // TODO(lowasser): consider inlining this.foo() as foo() return inliner.maker().Select(getExpression().inline(inliner), getIdentifier().inline(inliner)); } }
/** Create an identifier that refers to the variable declared in given variable * declaration. */ public JCExpression Ident(JCVariableDecl param) { return Ident(param.sym); }
/** Create a tree representing `super', given its type and owner. */ public JCIdent Super(Type t, TypeSymbol owner) { return Ident(new VarSymbol(FINAL, names._super, t, owner)); }
private JCExpression makeSelectExpression( Inliner inliner, List<String> qualifiedPath, int start) { Iterator<String> selects = qualifiedPath.listIterator(start); TreeMaker maker = inliner.maker(); JCExpression select = maker.Ident(inliner.asName(selects.next())); while (selects.hasNext()) { select = maker.Select(select, inliner.asName(selects.next())); } return select; } },
/** Construct an assignment from a variable symbol and a right hand side. */ public JCStatement Assignment(Symbol v, JCExpression rhs) { return Exec(Assign(Ident(v), rhs).setType(v.type)); }
/** * 不等于null * * @param varName 变量名 * @return 返回不等于null的表达式 */ public static JcaObject notNull(String varName) { return new JcaObject(treeMaker.Binary(JCTree.Tag.NE, treeMaker.Ident(names.fromString(varName)), treeMaker.Literal(TypeTag.BOT, null))); }
private JCExpression makeReceiver(VarSymbol rcvr) { if (rcvr == null) return null; JCExpression rcvrExpr = make.Ident(rcvr); Type rcvrType = tree.sym.enclClass().type; if (!rcvr.type.tsym.isSubClass(rcvrType.tsym, types)) { rcvrExpr = make.TypeCast(make.Type(rcvrType), rcvrExpr).setType(rcvrType); } return rcvrExpr; }
private VarSymbol addParameter(String name, Type p, boolean genArg) { VarSymbol vsym = new VarSymbol(0, names.fromString(name), p, localContext.bridgeSym); params.append(make.VarDef(vsym, null)); if (genArg) { args.append(make.Ident(vsym)); } return vsym; } }
public ExpressionTree QualIdent(Element element) { Symbol s = (Symbol) element; if (s.owner != null && (s.owner.kind == Kinds.Kind.MTH || s.owner.name.isEmpty())) { JCIdent result = make.at(NOPOS).Ident(s); result.setType(s.type); return result; } QualIdentTree result = new QualIdentTree(make.at(NOPOS).QualIdent(s.owner), s.name, s); result.setPos(make.pos).setType(s.type); return result; }
/** * Qualident = Ident { DOT Ident } */ public JCExpression qualident() { JCExpression t = toP(F.at(S.pos()).Ident(ident())); while (S.token() == DOT) { int pos = S.pos(); S.nextToken(); t = toP(F.at(pos).Select(t, ident())); } return t; }