|
Java example source code file (TreeMaker.java)
The TreeMaker.java Java example source code
/*
* Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.tools.javac.tree;
import com.sun.tools.javac.code.*;
import com.sun.tools.javac.code.Symbol.*;
import com.sun.tools.javac.code.Type.*;
import com.sun.tools.javac.util.*;
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
import com.sun.tools.javac.tree.JCTree.*;
import static com.sun.tools.javac.code.Flags.*;
import static com.sun.tools.javac.code.Kinds.*;
import static com.sun.tools.javac.code.TypeTag.*;
/** Factory class for trees.
*
* <p>This is NOT part of any supported API.
* If you write code that depends on this, you do so at your own risk.
* This code and its internal interfaces are subject to change or
* deletion without notice.</b>
*/
public class TreeMaker implements JCTree.Factory {
/** The context key for the tree factory. */
protected static final Context.Key<TreeMaker> treeMakerKey =
new Context.Key<TreeMaker>();
/** Get the TreeMaker instance. */
public static TreeMaker instance(Context context) {
TreeMaker instance = context.get(treeMakerKey);
if (instance == null)
instance = new TreeMaker(context);
return instance;
}
/** The position at which subsequent trees will be created.
*/
public int pos = Position.NOPOS;
/** The toplevel tree to which created trees belong.
*/
public JCCompilationUnit toplevel;
/** The current name table. */
Names names;
Types types;
/** The current symbol table. */
Symtab syms;
/** Create a tree maker with null toplevel and NOPOS as initial position.
*/
protected TreeMaker(Context context) {
context.put(treeMakerKey, this);
this.pos = Position.NOPOS;
this.toplevel = null;
this.names = Names.instance(context);
this.syms = Symtab.instance(context);
this.types = Types.instance(context);
}
/** Create a tree maker with a given toplevel and FIRSTPOS as initial position.
*/
protected TreeMaker(JCCompilationUnit toplevel, Names names, Types types, Symtab syms) {
this.pos = Position.FIRSTPOS;
this.toplevel = toplevel;
this.names = names;
this.types = types;
this.syms = syms;
}
/** Create a new tree maker for a given toplevel.
*/
public TreeMaker forToplevel(JCCompilationUnit toplevel) {
return new TreeMaker(toplevel, names, types, syms);
}
/** Reassign current position.
*/
public TreeMaker at(int pos) {
this.pos = pos;
return this;
}
/** Reassign current position.
*/
public TreeMaker at(DiagnosticPosition pos) {
this.pos = (pos == null ? Position.NOPOS : pos.getStartPosition());
return this;
}
/**
* Create given tree node at current position.
* @param defs a list of ClassDef, Import, and Skip
*/
public JCCompilationUnit TopLevel(List<JCAnnotation> packageAnnotations,
JCExpression pid,
List<JCTree> defs) {
Assert.checkNonNull(packageAnnotations);
for (JCTree node : defs)
Assert.check(node instanceof JCClassDecl
|| node instanceof JCImport
|| node instanceof JCSkip
|| node instanceof JCErroneous
|| (node instanceof JCExpressionStatement
&& ((JCExpressionStatement)node).expr instanceof JCErroneous),
node.getClass().getSimpleName());
JCCompilationUnit tree = new JCCompilationUnit(packageAnnotations, pid, defs,
null, null, null, null);
tree.pos = pos;
return tree;
}
public JCImport Import(JCTree qualid, boolean importStatic) {
JCImport tree = new JCImport(qualid, importStatic);
tree.pos = pos;
return tree;
}
public JCClassDecl ClassDef(JCModifiers mods,
Name name,
List<JCTypeParameter> typarams,
JCExpression extending,
List<JCExpression> implementing,
List<JCTree> defs)
{
JCClassDecl tree = new JCClassDecl(mods,
name,
typarams,
extending,
implementing,
defs,
null);
tree.pos = pos;
return tree;
}
public JCMethodDecl MethodDef(JCModifiers mods,
Name name,
JCExpression restype,
List<JCTypeParameter> typarams,
List<JCVariableDecl> params,
List<JCExpression> thrown,
JCBlock body,
JCExpression defaultValue) {
return MethodDef(
mods, name, restype, typarams, null, params,
thrown, body, defaultValue);
}
public JCMethodDecl MethodDef(JCModifiers mods,
Name name,
JCExpression restype,
List<JCTypeParameter> typarams,
JCVariableDecl recvparam,
List<JCVariableDecl> params,
List<JCExpression> thrown,
JCBlock body,
JCExpression defaultValue)
{
JCMethodDecl tree = new JCMethodDecl(mods,
name,
restype,
typarams,
recvparam,
params,
thrown,
body,
defaultValue,
null);
tree.pos = pos;
return tree;
}
public JCVariableDecl VarDef(JCModifiers mods, Name name, JCExpression vartype, JCExpression init) {
JCVariableDecl tree = new JCVariableDecl(mods, name, vartype, init, null);
tree.pos = pos;
return tree;
}
public JCVariableDecl ReceiverVarDef(JCModifiers mods, JCExpression name, JCExpression vartype) {
JCVariableDecl tree = new JCVariableDecl(mods, name, vartype);
tree.pos = pos;
return tree;
}
public JCSkip Skip() {
JCSkip tree = new JCSkip();
tree.pos = pos;
return tree;
}
public JCBlock Block(long flags, List<JCStatement> stats) {
JCBlock tree = new JCBlock(flags, stats);
tree.pos = pos;
return tree;
}
public JCDoWhileLoop DoLoop(JCStatement body, JCExpression cond) {
JCDoWhileLoop tree = new JCDoWhileLoop(body, cond);
tree.pos = pos;
return tree;
}
public JCWhileLoop WhileLoop(JCExpression cond, JCStatement body) {
JCWhileLoop tree = new JCWhileLoop(cond, body);
tree.pos = pos;
return tree;
}
public JCForLoop ForLoop(List<JCStatement> init,
JCExpression cond,
List<JCExpressionStatement> step,
JCStatement body)
{
JCForLoop tree = new JCForLoop(init, cond, step, body);
tree.pos = pos;
return tree;
}
public JCEnhancedForLoop ForeachLoop(JCVariableDecl var, JCExpression expr, JCStatement body) {
JCEnhancedForLoop tree = new JCEnhancedForLoop(var, expr, body);
tree.pos = pos;
return tree;
}
public JCLabeledStatement Labelled(Name label, JCStatement body) {
JCLabeledStatement tree = new JCLabeledStatement(label, body);
tree.pos = pos;
return tree;
}
public JCSwitch Switch(JCExpression selector, List<JCCase> cases) {
JCSwitch tree = new JCSwitch(selector, cases);
tree.pos = pos;
return tree;
}
public JCCase Case(JCExpression pat, List<JCStatement> stats) {
JCCase tree = new JCCase(pat, stats);
tree.pos = pos;
return tree;
}
public JCSynchronized Synchronized(JCExpression lock, JCBlock body) {
JCSynchronized tree = new JCSynchronized(lock, body);
tree.pos = pos;
return tree;
}
public JCTry Try(JCBlock body, List<JCCatch> catchers, JCBlock finalizer) {
return Try(List.<JCTree>nil(), body, catchers, finalizer);
}
public JCTry Try(List<JCTree> resources,
JCBlock body,
List<JCCatch> catchers,
JCBlock finalizer) {
JCTry tree = new JCTry(resources, body, catchers, finalizer);
tree.pos = pos;
return tree;
}
public JCCatch Catch(JCVariableDecl param, JCBlock body) {
JCCatch tree = new JCCatch(param, body);
tree.pos = pos;
return tree;
}
public JCConditional Conditional(JCExpression cond,
JCExpression thenpart,
JCExpression elsepart)
{
JCConditional tree = new JCConditional(cond, thenpart, elsepart);
tree.pos = pos;
return tree;
}
public JCIf If(JCExpression cond, JCStatement thenpart, JCStatement elsepart) {
JCIf tree = new JCIf(cond, thenpart, elsepart);
tree.pos = pos;
return tree;
}
public JCExpressionStatement Exec(JCExpression expr) {
JCExpressionStatement tree = new JCExpressionStatement(expr);
tree.pos = pos;
return tree;
}
public JCBreak Break(Name label) {
JCBreak tree = new JCBreak(label, null);
tree.pos = pos;
return tree;
}
public JCContinue Continue(Name label) {
JCContinue tree = new JCContinue(label, null);
tree.pos = pos;
return tree;
}
public JCReturn Return(JCExpression expr) {
JCReturn tree = new JCReturn(expr);
tree.pos = pos;
return tree;
}
public JCThrow Throw(JCExpression expr) {
JCThrow tree = new JCThrow(expr);
tree.pos = pos;
return tree;
}
public JCAssert Assert(JCExpression cond, JCExpression detail) {
JCAssert tree = new JCAssert(cond, detail);
tree.pos = pos;
return tree;
}
public JCMethodInvocation Apply(List<JCExpression> typeargs,
JCExpression fn,
List<JCExpression> args)
{
JCMethodInvocation tree = new JCMethodInvocation(typeargs, fn, args);
tree.pos = pos;
return tree;
}
public JCNewClass NewClass(JCExpression encl,
List<JCExpression> typeargs,
JCExpression clazz,
List<JCExpression> args,
JCClassDecl def)
{
JCNewClass tree = new JCNewClass(encl, typeargs, clazz, args, def);
tree.pos = pos;
return tree;
}
public JCNewArray NewArray(JCExpression elemtype,
List<JCExpression> dims,
List<JCExpression> elems)
{
JCNewArray tree = new JCNewArray(elemtype, dims, elems);
tree.pos = pos;
return tree;
}
public JCLambda Lambda(List<JCVariableDecl> params,
JCTree body)
{
JCLambda tree = new JCLambda(params, body);
tree.pos = pos;
return tree;
}
public JCParens Parens(JCExpression expr) {
JCParens tree = new JCParens(expr);
tree.pos = pos;
return tree;
}
public JCAssign Assign(JCExpression lhs, JCExpression rhs) {
JCAssign tree = new JCAssign(lhs, rhs);
tree.pos = pos;
return tree;
}
public JCAssignOp Assignop(JCTree.Tag opcode, JCTree lhs, JCTree rhs) {
JCAssignOp tree = new JCAssignOp(opcode, lhs, rhs, null);
tree.pos = pos;
return tree;
}
public JCUnary Unary(JCTree.Tag opcode, JCExpression arg) {
JCUnary tree = new JCUnary(opcode, arg);
tree.pos = pos;
return tree;
}
public JCBinary Binary(JCTree.Tag opcode, JCExpression lhs, JCExpression rhs) {
JCBinary tree = new JCBinary(opcode, lhs, rhs, null);
tree.pos = pos;
return tree;
}
public JCTypeCast TypeCast(JCTree clazz, JCExpression expr) {
JCTypeCast tree = new JCTypeCast(clazz, expr);
tree.pos = pos;
return tree;
}
public JCInstanceOf TypeTest(JCExpression expr, JCTree clazz) {
JCInstanceOf tree = new JCInstanceOf(expr, clazz);
tree.pos = pos;
return tree;
}
public JCArrayAccess Indexed(JCExpression indexed, JCExpression index) {
JCArrayAccess tree = new JCArrayAccess(indexed, index);
tree.pos = pos;
return tree;
}
public JCFieldAccess Select(JCExpression selected, Name selector) {
JCFieldAccess tree = new JCFieldAccess(selected, selector, null);
tree.pos = pos;
return tree;
}
public JCMemberReference Reference(JCMemberReference.ReferenceMode mode, Name name,
JCExpression expr, List<JCExpression> typeargs) {
JCMemberReference tree = new JCMemberReference(mode, name, expr, typeargs);
tree.pos = pos;
return tree;
}
public JCIdent Ident(Name name) {
JCIdent tree = new JCIdent(name, null);
tree.pos = pos;
return tree;
}
public JCLiteral Literal(TypeTag tag, Object value) {
JCLiteral tree = new JCLiteral(tag, value);
tree.pos = pos;
return tree;
}
public JCPrimitiveTypeTree TypeIdent(TypeTag typetag) {
JCPrimitiveTypeTree tree = new JCPrimitiveTypeTree(typetag);
tree.pos = pos;
return tree;
}
public JCArrayTypeTree TypeArray(JCExpression elemtype) {
JCArrayTypeTree tree = new JCArrayTypeTree(elemtype);
tree.pos = pos;
return tree;
}
public JCTypeApply TypeApply(JCExpression clazz, List<JCExpression> arguments) {
JCTypeApply tree = new JCTypeApply(clazz, arguments);
tree.pos = pos;
return tree;
}
public JCTypeUnion TypeUnion(List<JCExpression> components) {
JCTypeUnion tree = new JCTypeUnion(components);
tree.pos = pos;
return tree;
}
public JCTypeIntersection TypeIntersection(List<JCExpression> components) {
JCTypeIntersection tree = new JCTypeIntersection(components);
tree.pos = pos;
return tree;
}
public JCTypeParameter TypeParameter(Name name, List<JCExpression> bounds) {
return TypeParameter(name, bounds, List.<JCAnnotation>nil());
}
public JCTypeParameter TypeParameter(Name name, List<JCExpression> bounds, List
Other Java examples (source code examples)Here is a short list of links related to this Java TreeMaker.java source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.