001: /*
002: * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.es.parser;
030:
031: import com.caucho.es.ESBase;
032: import com.caucho.es.ESException;
033:
034: import java.io.IOException;
035: import java.lang.reflect.Constructor;
036: import java.lang.reflect.Modifier;
037:
038: /**
039: * JavaNewExpr is an intermediate form representing a new expression
040: * when the type is known to be a java class.
041: */
042: class JavaNewExpr extends CallExpr {
043: private Class javaClass;
044:
045: private Constructor constructor;
046:
047: JavaNewExpr(Block block, Class javaClass) throws ESException {
048: super (block, null, null, true);
049:
050: this .javaClass = javaClass;
051: }
052:
053: /**
054: * adds a new call parameter
055: */
056: void addCallParam(Expr param) {
057: param.setUsed();
058: args.add(param);
059: }
060:
061: int getType() {
062: calculateType();
063:
064: return type;
065: }
066:
067: Expr getTypeExpr() {
068: calculateType();
069:
070: return typeExpr;
071: }
072:
073: private void calculateType() {
074: if (isCalculated)
075: return;
076:
077: isCalculated = true;
078:
079: if (javaClass == null)
080: return;
081:
082: method = JavaMethod.bestMethod(javaClass, "create", true, args);
083: if (method == null || !method.getReturnType().equals(javaClass))
084: method = null;
085:
086: if (method != null)
087: term = new JavaClassExpr(block, javaClass);
088:
089: Constructor[] constructors = javaClass.getConstructors();
090: Constructor bestConstructor = null;
091: int bestCost = Integer.MAX_VALUE;
092:
093: for (int i = 0; i < constructors.length; i++) {
094: Constructor constructor = constructors[i];
095:
096: if (!Modifier.isPublic(constructor.getModifiers()))
097: continue;
098:
099: Class[] param = constructor.getParameterTypes();
100:
101: int cost = JavaMethod.methodCost(param, args);
102:
103: if (cost < bestCost) {
104: bestCost = cost;
105: bestConstructor = constructor;
106: }
107: }
108:
109: this .constructor = bestConstructor;
110:
111: type = TYPE_JAVA;
112: typeExpr = new JavaTypeExpr(block, javaClass);
113: }
114:
115: void printJavaImpl() throws IOException {
116: if (method != null) {
117: super .printJavaImpl();
118: return;
119: }
120:
121: if (constructor == null)
122: throw new IOException("can't create `"
123: + javaClass.getName() + "'");
124:
125: cl.print("new ");
126:
127: cl.print(javaClass.getName());
128: cl.print("(");
129:
130: Class[] params = null;
131: if (constructor != null)
132: params = constructor.getParameterTypes();
133:
134: for (int i = 0; i < params.length; i++) {
135: Expr expr;
136:
137: if (i < args.size())
138: expr = (Expr) args.get(i);
139: else
140: expr = block.newLiteral(ESBase.esUndefined);
141:
142: if (i != 0)
143: cl.print(", ");
144:
145: if (params != null && !params[i].isPrimitive()) {
146: cl.print("(");
147: printJavaClass(params[i]);
148: cl.print(")");
149: }
150:
151: expr.printJava();
152: }
153: cl.print(")");
154: }
155: }
|