001: /*
002: * Copyright (c) 1998-2007 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: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Nam Nguyen
028: */
029:
030: package com.caucho.quercus.lib.reflection;
031:
032: import com.caucho.quercus.env.Env;
033: import com.caucho.quercus.env.QuercusClass;
034: import com.caucho.quercus.env.Value;
035: import com.caucho.quercus.expr.RequiredExpr;
036: import com.caucho.quercus.program.AbstractFunction;
037: import com.caucho.quercus.program.Arg;
038: import com.caucho.util.L10N;
039:
040: public class ReflectionParameter implements Reflector {
041: private static final L10N L = new L10N(ReflectionParameter.class);
042:
043: private String _clsName;
044: private AbstractFunction _fun;
045: private Arg _arg;
046:
047: protected ReflectionParameter(AbstractFunction fun, Arg arg) {
048: _fun = fun;
049: _arg = arg;
050: }
051:
052: protected ReflectionParameter(String clsName, AbstractFunction fun,
053: Arg arg) {
054: this (fun, arg);
055:
056: _clsName = clsName;
057: }
058:
059: final private void __clone() {
060: }
061:
062: public static ReflectionParameter __construct(Env env,
063: String funName, String paramName) {
064: AbstractFunction fun = env.findFunction(funName);
065:
066: Arg[] args = fun.getArgs();
067:
068: for (int i = 0; i < args.length; i++) {
069: if (args[i].getName().equals(paramName))
070: return new ReflectionParameter(fun, args[i]);
071: }
072:
073: throw new ReflectionException(L.l(
074: "cannot find parameter '{0}'", paramName));
075: }
076:
077: public String __toString() {
078: return null;
079: }
080:
081: public static String export(Env env, Value function,
082: Value parameter, boolean isReturn) {
083: return null;
084: }
085:
086: public String getName() {
087: return _arg.getName();
088: }
089:
090: public boolean isPassedByReference() {
091: return _arg.isReference();
092: }
093:
094: public ReflectionClass getDeclaringClass(Env env) {
095: if (_clsName != null) {
096: QuercusClass cls = env.findClass(_clsName);
097: QuercusClass parent = cls.getParent();
098:
099: if (parent == null
100: || parent.findFunction(_fun.getName()) != _fun)
101: return new ReflectionClass(cls);
102: else
103: return getDeclaringClass(env, parent);
104: } else
105: return null;
106: }
107:
108: private ReflectionClass getDeclaringClass(Env env, QuercusClass cls) {
109: if (cls == null)
110: return null;
111:
112: ReflectionClass refClass = getDeclaringClass(env, cls
113: .getParent());
114:
115: if (refClass != null)
116: return refClass;
117: else if (cls.findFunction(_fun.getName()) != null)
118: return new ReflectionClass(cls);
119: else
120: return null;
121: }
122:
123: public ReflectionClass getClass(Env env) {
124: return null;
125: }
126:
127: public boolean isArray() {
128: return false;
129: }
130:
131: public boolean allowsNull() {
132: return false;
133: }
134:
135: public boolean isOptional() {
136: return !(_arg.getDefault() instanceof RequiredExpr);
137: }
138:
139: public boolean isDefaultValueAvailable() {
140: return isOptional();
141: }
142:
143: public Value getDefaultValue(Env env) {
144: //XXX: more specific exception
145: if (!isOptional())
146: throw new ReflectionException(L.l(
147: "parameter '{0}' is not optional", _arg.getName()));
148:
149: return _arg.getDefault().eval(env);
150: }
151:
152: public String toString() {
153: return "ReflectionParameter[" + _fun.getName() + "("
154: + _arg.getName() + ")]";
155: }
156: }
|