001: //--------------------------------------------------------------------------
002: // Copyright (c) 1998-2004, Drew Davidson and Luke Blanshard
003: // All rights reserved.
004: //
005: // Redistribution and use in source and binary forms, with or without
006: // modification, are permitted provided that the following conditions are
007: // met:
008: //
009: // Redistributions of source code must retain the above copyright notice,
010: // this list of conditions and the following disclaimer.
011: // Redistributions in binary form must reproduce the above copyright
012: // notice, this list of conditions and the following disclaimer in the
013: // documentation and/or other materials provided with the distribution.
014: // Neither the name of the Drew Davidson nor the names of its contributors
015: // may be used to endorse or promote products derived from this software
016: // without specific prior written permission.
017: //
018: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
019: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
020: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
021: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
022: // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
023: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
024: // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
025: // OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
026: // AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
027: // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
028: // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
029: // DAMAGE.
030: //--------------------------------------------------------------------------
031: package ognl;
032:
033: import java.lang.reflect.Field;
034: import java.lang.reflect.Modifier;
035:
036: /**
037: * @author Luke Blanshard (blanshlu@netscape.net)
038: * @author Drew Davidson (drew@ognl.org)
039: */
040: class ASTStaticField extends SimpleNode {
041: private String className;
042: private String fieldName;
043:
044: public ASTStaticField(int id) {
045: super (id);
046: }
047:
048: public ASTStaticField(OgnlParser p, int id) {
049: super (p, id);
050: }
051:
052: /** Called from parser action. */
053: void init(String className, String fieldName) {
054: this .className = className;
055: this .fieldName = fieldName;
056: }
057:
058: protected Object getValueBody(OgnlContext context, Object source)
059: throws OgnlException {
060: return OgnlRuntime
061: .getStaticField(context, className, fieldName);
062: }
063:
064: public boolean isNodeConstant(OgnlContext context)
065: throws OgnlException {
066: boolean result = false;
067: Exception reason = null;
068:
069: try {
070: Class c = OgnlRuntime.classForName(context, className);
071:
072: /*
073: Check for virtual static field "class"; this cannot interfere with
074: normal static fields because it is a reserved word. It is considered
075: constant.
076: */
077: if (fieldName.equals("class")) {
078: result = true;
079: } else {
080: Field f = c.getField(fieldName);
081:
082: if (!Modifier.isStatic(f.getModifiers())) {
083: throw new OgnlException("Field " + fieldName
084: + " of class " + className
085: + " is not static");
086: }
087: result = Modifier.isFinal(f.getModifiers());
088: }
089: } catch (ClassNotFoundException e) {
090: reason = e;
091: } catch (NoSuchFieldException e) {
092: reason = e;
093: } catch (SecurityException e) {
094: reason = e;
095: }
096:
097: if (reason != null) {
098: throw new OgnlException("Could not get static field "
099: + fieldName + " from class " + className, reason);
100: }
101: return result;
102: }
103:
104: public String toString() {
105: return "@" + className + "@" + fieldName;
106: }
107: }
|