@SuppressWarnings("unchecked") // essentially depends on T being a superclass of JCExpression @Override public <T extends JCTree> T copy(T t, Void v) { if (t instanceof BinaryTree || t instanceof UnaryTree || t instanceof ConditionalExpressionTree) { return super.copy(t, v); } else { return (T) defaultNegation(t); } }
@Override public <T extends JCTree> T copy(T tree, Inliner inliner) { if (tree == null) { return null; } T result = super.copy(tree, inliner); if (result.toString().equals(tree.toString())) { return tree; } else { return result; } }
@Override public JCExpression inline(Inliner inliner) throws CouldNotResolveImportException { /* * Copy the original source bound to the placeholder, except anywhere we matched a placeholder * parameter, replace that with the corresponding expression in this invocation. */ try { return copier(arguments(), inliner) .copy(inliner.getBinding(placeholder().exprKey()), inliner); } catch (UncheckedCouldNotResolveImportException e) { throw e.getCause(); } }
@Override public List<JCStatement> inlineStatements(final Inliner inliner) throws CouldNotResolveImportException { try { Optional<List<JCStatement>> binding = inliner.getOptionalBinding(placeholder().blockKey()); // If a placeholder was used as an expression binding in the @BeforeTemplate, // and as a bare statement or as a return in the @AfterTemplate, we may need to convert. Optional<JCExpression> exprBinding = inliner.getOptionalBinding(placeholder().exprKey()); binding = binding.or( exprBinding.transform( (JCExpression expr) -> { switch (implementationFlow()) { case NEVER_EXITS: return List.of((JCStatement) inliner.maker().Exec(expr)); case ALWAYS_RETURNS: return List.of((JCStatement) inliner.maker().Return(expr)); default: throw new AssertionError(); } })); return UPlaceholderExpression.copier(arguments(), inliner).copy(binding.get(), inliner); } catch (UncheckedCouldNotResolveImportException e) { throw e.getCause(); } } }
JCTree inlineBody(Inliner inliner) throws CouldNotResolveImportException { if (getBody() instanceof UPlaceholderExpression) { UPlaceholderExpression body = (UPlaceholderExpression) getBody(); Optional<List<JCStatement>> blockBinding = inliner.getOptionalBinding(body.placeholder().blockKey()); if (blockBinding.isPresent()) { // this lambda is of the form args -> blockPlaceholder(); List<JCStatement> blockInlined = UPlaceholderExpression.copier(body.arguments(), inliner) .copy(blockBinding.get(), inliner); if (blockInlined.size() == 1) { if (blockInlined.get(0) instanceof JCReturn) { return ((JCReturn) blockInlined.get(0)).getExpression(); } else if (blockInlined.get(0) instanceof JCExpressionStatement) { return ((JCExpressionStatement) blockInlined.get(0)).getExpression(); } } return inliner.maker().Block(0, blockInlined); } } return getBody().inline(inliner); }
public <T extends JCTree> T copy(T t, JCTree leaf) { T t2 = super.copy(t, leaf); if (t == leaf) leafCopy = t2; return t2; } }
public JCTree visitMethod(MethodTree node, P p) { JCMethodDecl t = (JCMethodDecl) node; JCModifiers mods = copy(t.mods, p); JCExpression restype = copy(t.restype, p); List<JCTypeParameter> typarams = copy(t.typarams, p); List<JCVariableDecl> params = copy(t.params, p); JCVariableDecl recvparam = copy(t.recvparam, p); List<JCExpression> thrown = copy(t.thrown, p); JCBlock body = copy(t.body, p); JCExpression defaultValue = copy(t.defaultValue, p); return M.at(t.pos).MethodDef(mods, t.name, restype, typarams, recvparam, params, thrown, body, defaultValue); }
@Override public <T extends JCTree> T copy(T original, Void p) { T copy = super.copy(original, p); originalToCopy.put(original, copy); return copy; }
public JCTree visitCompilationUnit(CompilationUnitTree node, P p) { JCCompilationUnit t = (JCCompilationUnit) node; List<JCAnnotation> packageAnnotations = copy(t.packageAnnotations, p); JCExpression pid = copy(t.pid, p); List<JCTree> defs = copy(t.defs, p); return M.at(t.pos).TopLevel(packageAnnotations, pid, defs); }
public JCTree visitVariable(VariableTree node, P p) { JCVariableDecl t = (JCVariableDecl) node; JCModifiers mods = copy(t.mods, p); JCExpression vartype = copy(t.vartype, p); if (t.nameexpr == null) { JCExpression init = copy(t.init, p); return M.at(t.pos).VarDef(mods, t.name, vartype, init); } else { JCExpression nameexpr = copy(t.nameexpr, p); return M.at(t.pos).ReceiverVarDef(mods, nameexpr, vartype); } }
public JCTree visitThrow(ThrowTree node, P p) { JCThrow t = (JCThrow) node; JCTree expr = copy(t.expr, p); return M.at(t.pos).Throw(expr); }
public JCTree visitMemberSelect(MemberSelectTree node, P p) { JCFieldAccess t = (JCFieldAccess) node; JCExpression selected = copy(t.selected, p); return M.at(t.pos).Select(selected, t.name); }
public JCTree visitUnary(UnaryTree node, P p) { JCUnary t = (JCUnary) node; JCExpression arg = copy(t.arg, p); return M.at(t.pos).Unary(t.getTag(), arg); }
public JCTree visitWildcard(WildcardTree node, P p) { JCWildcard t = (JCWildcard) node; TypeBoundKind kind = M.at(t.kind.pos).TypeBoundKind(t.kind.kind); JCTree inner = copy(t.inner, p); return M.at(t.pos).Wildcard(kind, inner); }