001: /*
002: * ClassRep.java --
003: *
004: * This class implements the internal representation of a Java
005: * class name.
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: ClassRep.java,v 1.3 2000/10/29 06:00:42 mdejong Exp $
014: *
015: */
016:
017: package tcl.lang;
018:
019: import java.lang.reflect.*;
020:
021: /**
022: * This class implements the internal representation of a Java class
023: * name.
024: */
025:
026: class ClassRep implements InternalRep {
027:
028: // The class referred to by this ClassRep.
029:
030: Class cls;
031:
032: /*
033: *----------------------------------------------------------------------
034: *
035: * ClassRep --
036: *
037: * Creates a new ClassRep instance.
038: *
039: * Side effects:
040: * Member fields are initialized.
041: *
042: *----------------------------------------------------------------------
043: */
044:
045: ClassRep(Class c) // Initial value for cls.
046: {
047: cls = c;
048: }
049:
050: /*
051: *----------------------------------------------------------------------
052: *
053: * duplicate --
054: *
055: * Make a copy of an object's internal representation.
056: *
057: * Results:
058: * Returns a newly allocated instance of the appropriate type.
059: *
060: * Side effects:
061: * None.
062: *
063: *----------------------------------------------------------------------
064: */
065:
066: public InternalRep duplicate() {
067: return new ClassRep(cls);
068: }
069:
070: /**
071: * Implement this no-op for the InternalRep interface.
072: */
073:
074: public void dispose() {
075: }
076:
077: /*
078: *----------------------------------------------------------------------
079: *
080: * get --
081: *
082: * Returns the Class object referred to by the TclObject.
083: *
084: * Results:
085: * The Class object referred to by the TclObject.
086: *
087: * Side effects:
088: * When successful, the internalRep of the signature object is
089: * converted to ClassRep.
090: *
091: *----------------------------------------------------------------------
092: */
093:
094: static Class get(Interp interp, // Current interpreter.
095: TclObject tclObj) // TclObject that contains a valid Java
096: // class name.
097: throws TclException // If the TclObject doesn't contain a valid
098: // class name.
099: {
100: InternalRep rep = tclObj.getInternalRep();
101:
102: if (rep instanceof ClassRep) {
103: // If a ClassRep is already cached, return it right away.
104: return ((ClassRep) rep).cls;
105: } else {
106: Class c = JavaInvoke.getClassByName(interp, tclObj
107: .toString());
108: tclObj.setInternalRep(new ClassRep(c));
109: return c;
110: }
111: }
112:
113: } // end ClassRep
|