001: /*
002: * Copyright 1998-2003 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.java2d.loops;
027:
028: /**
029: * GraphicsPrimitiveProxy
030: *
031: * Acts as a proxy for instances of GraphicsPrimitive, enabling lazy
032: * classloading of these primitives. This leads to a substantial
033: * savings in start-up time and footprint. In the typical case,
034: * it has been found that a small number of GraphicsPrimitive instance
035: * actually end up getting instantiated.
036: * <p>
037: * Note that the makePrimitive method should never be invoked on
038: * a GraphicsPrimitiveProxy object since they are instantiated as
039: * soon as they are found in the primitive list and never returned
040: * to the caller.
041: */
042: public class GraphicsPrimitiveProxy extends GraphicsPrimitive {
043:
044: private Class owner;
045: private String relativeClassName;
046:
047: /**
048: * Create a GraphicsPrimitiveProxy for a primitive with a no-argument
049: * constructor.
050: *
051: * @param owner The owner class for this primitive. The primitive
052: * must be in the same package as this owner.
053: * @param relativeClassName The name of the class this is a proxy for.
054: * This should not include the package.
055: */
056: public GraphicsPrimitiveProxy(Class owner,
057: String relativeClassName, String methodSignature,
058: int primID, SurfaceType srctype, CompositeType comptype,
059: SurfaceType dsttype) {
060: super (methodSignature, primID, srctype, comptype, dsttype);
061: this .owner = owner;
062: this .relativeClassName = relativeClassName;
063: }
064:
065: public GraphicsPrimitive makePrimitive(SurfaceType srctype,
066: CompositeType comptype, SurfaceType dsttype) {
067: // This should never happen.
068: throw new InternalError("makePrimitive called on a Proxy!");
069: }
070:
071: //
072: // Come up with the real instance. Called from
073: // GraphicsPrimitiveMgr.locate()
074: //
075: GraphicsPrimitive instantiate() {
076: String name = getPackageName(owner.getName()) + "."
077: + relativeClassName;
078: try {
079: Class clazz = Class.forName(name);
080: GraphicsPrimitive p = (GraphicsPrimitive) clazz
081: .newInstance();
082: if (!satisfiesSameAs(p)) {
083: throw new RuntimeException("Primitive " + p
084: + " incompatible with proxy for " + name);
085: }
086: return p;
087: } catch (ClassNotFoundException ex) {
088: throw new RuntimeException(ex.toString());
089: } catch (InstantiationException ex) {
090: throw new RuntimeException(ex.toString());
091: } catch (IllegalAccessException ex) {
092: throw new RuntimeException(ex.toString());
093: }
094: // A RuntimeException should never happen in a deployed JDK, because
095: // the regression test GraphicsPrimitiveProxyTest will catch any
096: // of these errors.
097: }
098:
099: private static String getPackageName(String className) {
100: int lastDotIdx = className.lastIndexOf('.');
101: if (lastDotIdx < 0) {
102: return className;
103: }
104: return className.substring(0, lastDotIdx);
105: }
106:
107: public GraphicsPrimitive traceWrap() {
108: return instantiate().traceWrap();
109: }
110: }
|