001: /*
002: * PropertySig.java --
003: *
004: * This class implements the internal representation of a Java
005: * property signature.
006: *
007: * Copyright (c) 1997 Sun Microsystems, Inc.
008: *
009: * See the file "license.terms" for information on usage and
010: * redistribution of this file, and for a DISCLAIMER OF ALL
011: * WARRANTIES.
012: *
013: * RCS: @(#) $Id: PropertySig.java,v 1.4 2000/10/29 06:00:42 mdejong Exp $
014: *
015: */
016:
017: package tcl.lang;
018:
019: import tcl.lang.reflect.*;
020: import java.lang.reflect.*;
021: import java.beans.*;
022:
023: /**
024: * This class implements the internal representation of a Java Property
025: * signature.
026: */
027:
028: class PropertySig implements InternalRep {
029:
030: // The class that the property signature is used against. It is the
031: // class of the java object specified in the java::prop command.
032: // targetCls is used to test the validity of a cached PropertySig
033: // internal rep.
034:
035: Class targetCls;
036:
037: // Property descriptor of the property.
038:
039: PropertyDescriptor desc;
040:
041: // The PkgInvoker used to access the property.
042:
043: PkgInvoker pkgInvoker;
044:
045: /*
046: *----------------------------------------------------------------------
047: *
048: * PropertySig --
049: *
050: * Creates a new PropertySig instance.
051: *
052: * Side effects:
053: * Member fields are initialized.
054: *
055: *----------------------------------------------------------------------
056: */
057:
058: PropertySig(Class c, // Initial value for targetCls.
059: PkgInvoker i, // Initial value for pkgInvoker.
060: PropertyDescriptor d) // Initial value for desc.
061: {
062: targetCls = c;
063: pkgInvoker = i;
064: desc = d;
065: }
066:
067: /*
068: *----------------------------------------------------------------------
069: *
070: * duplicate --
071: *
072: * Make a copy of an object's internal representation.
073: *
074: * Results:
075: * Returns a newly allocated instance of the appropriate type.
076: *
077: * Side effects:
078: * None.
079: *
080: *----------------------------------------------------------------------
081: */
082:
083: public InternalRep duplicate() {
084: return new PropertySig(targetCls, pkgInvoker, desc);
085: }
086:
087: /**
088: * Implement this no-op for the InternalRep interface.
089: */
090:
091: public void dispose() {
092: }
093:
094: /*
095: *----------------------------------------------------------------------
096: *
097: * get --
098: *
099: * Returns the FieldSig internal rep given by the field signature.
100: *
101: * Results:
102: * The FieldSig given by the signature.
103: *
104: * Side effects:
105: * When successful, the internalRep of the signature object is
106: * converted to FieldSig.
107: *
108: *----------------------------------------------------------------------
109: */
110:
111: static PropertySig get(Interp interp, // Current interpreter
112: Class targetCls, // The target class of the property signature.
113: TclObject signature) // The TclObject that holds a prop. signature.
114: throws TclException // Standard Tcl exception.
115: {
116: InternalRep rep = signature.getInternalRep();
117:
118: if ((rep instanceof PropertySig)
119: && (targetCls == ((PropertySig) rep).targetCls)) {
120: // The cached internal rep is a valid property signature for
121: // the given targetCls. Return it.
122:
123: return (PropertySig) rep;
124: }
125:
126: String propName = signature.toString();
127: PropertyDescriptor desc = null;
128:
129: search_prop: {
130: BeanInfo beanInfo;
131:
132: try {
133: beanInfo = Introspector.getBeanInfo(targetCls);
134: } catch (IntrospectionException e) {
135: break search_prop;
136: }
137:
138: PropertyDescriptor descriptors[] = beanInfo
139: .getPropertyDescriptors();
140:
141: if (descriptors == null) {
142: break search_prop;
143: }
144:
145: // Search for a property that has the same name as propName.
146:
147: for (int i = 0; i < descriptors.length; i++) {
148: // FIXME : null tests are just for debugging
149: if (descriptors[i] == null) {
150: throw new NullPointerException("descriptor is null");
151: }
152: if (descriptors[i].getName() == null) {
153: throw new NullPointerException(
154: "descriptor.getName() is null");
155: }
156: if (propName == null) {
157: throw new NullPointerException("propName is null");
158: }
159:
160: if (descriptors[i].getName().equals(propName)) {
161: if (descriptors[i] instanceof IndexedPropertyDescriptor) {
162: throw new TclException(interp,
163: "can't access indexed property \""
164: + propName
165: + "\": not implemented");
166: }
167: desc = descriptors[i];
168: break;
169: }
170: }
171: }
172:
173: if (desc == null) {
174: throw new TclException(interp, "unknown property \""
175: + propName + "\"");
176: }
177:
178: PropertySig sig = new PropertySig(targetCls, PkgInvoker
179: .getPkgInvoker(targetCls), desc);
180:
181: signature.setInternalRep(sig);
182:
183: return sig;
184: }
185:
186: } // end PropertySig
|