diff options
Diffstat (limited to 'src/main/java/io/devnulllabs/openjava/ptree/AllocationExpression.java')
-rw-r--r-- | src/main/java/io/devnulllabs/openjava/ptree/AllocationExpression.java | 203 |
1 files changed, 203 insertions, 0 deletions
diff --git a/src/main/java/io/devnulllabs/openjava/ptree/AllocationExpression.java b/src/main/java/io/devnulllabs/openjava/ptree/AllocationExpression.java new file mode 100644 index 0000000..3aea1e2 --- /dev/null +++ b/src/main/java/io/devnulllabs/openjava/ptree/AllocationExpression.java @@ -0,0 +1,203 @@ +/* + * AllocationExpression.java 1.0 + * + * + * Jun 20, 1997 by mich + * Sep 29, 1997 by bv + * Oct 10, 1997 by mich + * + * @see io.devnulllabs.openjava.ptree.ParseTree + * @version 1.0 last updated: Oct 10, 1997 + * @author Michiaki Tatsubori + */ +package io.devnulllabs.openjava.ptree; + +import io.devnulllabs.openjava.mop.Environment; +import io.devnulllabs.openjava.mop.OJClass; +import io.devnulllabs.openjava.ptree.util.ParseTreeVisitor; + +/** + * The <code>AllocationExpression</code> class represents + * an expression which allocates a new object with its constructor. + * <br> + * This expression is like: + * <br><blockquote><pre> + * new String( "test" ) + * </pre></blockquote><br> + * or: + * <br><blockquote><pre> + * new String( "test" ){ + * public void hoge(){ ... } + * ... + * } + * </pre></blockquote><br> + * The latter is supported from JDK 1.1, is called an anoymous class + * of the inner class. + * + * @see io.devnulllabs.openjava.ptree.Expression + * @see io.devnulllabs.openjava.ptree.TypeName + * @see io.devnulllabs.openjava.ptree.ExpressionList + * @see io.devnulllabs.openjava.ptree.MemberDeclarationList + */ +public class AllocationExpression extends NonLeaf implements Expression { + + /** + * Allocates a new object with the class body. + * + * @param ctype a class name to be constructed + * @param args argument list of constructor + * @param mdlst the class body. + * If this is null, no class body will be provided + * this construct expression with. + */ + public AllocationExpression( + Expression encloser, + TypeName typename, + ExpressionList args, + MemberDeclarationList mdlst) { + super(); + if (args == null) + args = new ExpressionList(); + /* an explicitly specified null has meaning of no body */ + //if (mdlst == null) mdlst = new MemberDeclarationList(); + set(typename, args, mdlst, encloser); + } + + /** + * Allocates a new object with the class body. + * + * @param ctype a class name to be constructed + * @param args argument list of constructor + * @param mdlst the class body. + * If this is null, no class body will be provided + * this construct expression with. + */ + public AllocationExpression( + TypeName typename, + ExpressionList args, + MemberDeclarationList mdlst) { + this(null, typename, args, mdlst); + } + + /** + * Allocates a new object with the class body. + * + * @param ctype a class name to be constructed + * @param args argument list of constructor + * @param mdlst the class body. + * If this is null, no class body will be provided + * this construct expression with. + */ + public AllocationExpression( + Expression encloser, + TypeName typename, + ExpressionList args) { + this(encloser, typename, args, null); + } + + /** + * Allocates a new object without class body. + * + * @param ctype a class name to be constructed + * @param args argument list of constructor + */ + public AllocationExpression(TypeName ctype, ExpressionList args) { + this(ctype, args, null); + } + + public AllocationExpression(OJClass type, ExpressionList args) { + this(TypeName.forOJClass(type), args); + } + + AllocationExpression() { + super(); + } + + /** + * Gets the expression of enclosing object. + * + * @return the expression of enclosing object + */ + public Expression getEncloser() { + return (Expression) elementAt(3); + } + + /** + * Sets the expression of enclosing object. + * + * @param encloser the expression of enclosing object + */ + public void setEncloser(Expression encloser) { + setElementAt(encloser, 3); + } + + /** + * Gets the class type of this constructor. + * + * @return the class type of this constructor. + */ + public TypeName getClassType() { + return (TypeName) elementAt(0); + } + + /** + * Sets the class type of this constructor. + * + * @param ctype the class body to set. + */ + public void setClassType(TypeName ctype) { + setElementAt(ctype, 0); + } + + /** + * Gets the arguments of this constructor. + * + * @return the arguments as an expressions list. + */ + public ExpressionList getArguments() { + return (ExpressionList) elementAt(1); + } + + /** + * Sets the arguments of this constructor. + * + * @return the expressions list of arguments. + */ + public void setArguments(ExpressionList args) { + if (args == null) { + args = new ExpressionList(); + } + setElementAt(args, 1); + } + + /** + * Gets the class body of this constructor. + * + * @return the member declaration list as the class body of + * this constructor. + */ + public MemberDeclarationList getClassBody() { + return (MemberDeclarationList) elementAt(2); + } + + /** + * Sets the class body of this constructor. + * + * @param mdlist the member declaration list of the class body. + * If this is null, the class body will disappear. + */ + public void setClassBody(MemberDeclarationList mdlist) { + setElementAt(mdlist, 2); + } + + public OJClass getType(Environment env) throws Exception { + String typename = env.toQualifiedName(getClassType().toString()); + OJClass result = env.lookupClass(typename); + return result; + } + + public void accept(ParseTreeVisitor v) throws ParseTreeException { + v.visit(this); + } + +} |