001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.bind.v2.runtime.reflect.opt;
038:
039: import java.lang.reflect.Field;
040: import java.lang.reflect.Method;
041: import java.lang.reflect.Modifier;
042: import java.util.logging.Level;
043: import java.util.logging.Logger;
044:
045: import com.sun.xml.bind.Util;
046: import com.sun.xml.bind.v2.runtime.reflect.Accessor;
047:
048: import static com.sun.xml.bind.v2.bytecode.ClassTailor.toVMClassName;
049: import static com.sun.xml.bind.v2.bytecode.ClassTailor.toVMTypeName;
050:
051: /**
052: * @author Kohsuke Kawaguchi
053: */
054: public abstract class OptimizedAccessorFactory {
055: private OptimizedAccessorFactory() {
056: } // no instanciation please
057:
058: private static final Logger logger = Util.getClassLogger();
059:
060: private static final String fieldTemplateName;
061: private static final String methodTemplateName;
062:
063: static {
064: String s = FieldAccessor_Byte.class.getName();
065: fieldTemplateName = s
066: .substring(0, s.length() - "Byte".length()).replace(
067: '.', '/');
068:
069: s = MethodAccessor_Byte.class.getName();
070: methodTemplateName = s.substring(0,
071: s.length() - "Byte".length()).replace('.', '/');
072: }
073:
074: /**
075: * Gets the optimized {@link Accessor} that accesses the given getter/setter.
076: *
077: * @return null
078: * if for some reason it fails to create an optimized version.
079: */
080: public static final <B, V> Accessor<B, V> get(Method getter,
081: Method setter) {
082: // make sure the method signatures are what we expect
083: if (getter.getParameterTypes().length != 0)
084: return null;
085: Class<?>[] sparams = setter.getParameterTypes();
086: if (sparams.length != 1)
087: return null;
088: if (sparams[0] != getter.getReturnType())
089: return null;
090: if (setter.getReturnType() != Void.TYPE)
091: return null;
092: if (getter.getDeclaringClass() != setter.getDeclaringClass())
093: return null;
094: if (Modifier.isPrivate(getter.getModifiers())
095: || Modifier.isPrivate(setter.getModifiers()))
096: // we can't access private fields
097: return null;
098:
099: Class t = sparams[0];
100: String typeName = t.getName().replace('.', '_');
101:
102: String newClassName = toVMClassName(getter.getDeclaringClass())
103: + "$JaxbAccessorM_" + getter.getName() + '_'
104: + setter.getName() + '_' + typeName;
105:
106: Class opt;
107:
108: if (t.isPrimitive())
109: opt = AccessorInjector.prepare(getter.getDeclaringClass(),
110: methodTemplateName + t.getName(), newClassName,
111: toVMClassName(Bean.class), toVMClassName(getter
112: .getDeclaringClass()),
113: "get_" + t.getName(), getter.getName(), "set_"
114: + t.getName(), setter.getName());
115: else
116: opt = AccessorInjector.prepare(getter.getDeclaringClass(),
117: methodTemplateName + "Ref", newClassName,
118: toVMClassName(Bean.class), toVMClassName(getter
119: .getDeclaringClass()),
120: toVMClassName(Ref.class), toVMClassName(t), "()"
121: + toVMTypeName(Ref.class), "()"
122: + toVMTypeName(t), '('
123: + toVMTypeName(Ref.class) + ")V", '('
124: + toVMTypeName(t) + ")V", "get_ref", getter
125: .getName(), "set_ref", setter.getName());
126:
127: if (opt == null)
128: return null;
129:
130: Accessor<B, V> acc = instanciate(opt);
131: if (acc != null)
132: logger.log(Level.FINE, "Using optimized Accessor for "
133: + getter + " and " + setter);
134: return acc;
135: }
136:
137: /**
138: * Gets the optimizd {@link Accessor} that accesses the given field.
139: *
140: * @return null
141: * if for some reason it fails to create an optimized version.
142: */
143: public static final <B, V> Accessor<B, V> get(Field field) {
144: int mods = field.getModifiers();
145: if (Modifier.isPrivate(mods) || Modifier.isFinal(mods))
146: // we can't access private fields
147: return null;
148:
149: String newClassName = toVMClassName(field.getDeclaringClass())
150: + "$JaxbAccessorF_" + field.getName();
151:
152: Class opt;
153:
154: if (field.getType().isPrimitive())
155: opt = AccessorInjector.prepare(field.getDeclaringClass(),
156: fieldTemplateName + field.getType().getName(),
157: newClassName, toVMClassName(Bean.class),
158: toVMClassName(field.getDeclaringClass()), "f_"
159: + field.getType().getName(), field
160: .getName());
161: else
162: opt = AccessorInjector.prepare(field.getDeclaringClass(),
163: fieldTemplateName + "Ref", newClassName,
164: toVMClassName(Bean.class), toVMClassName(field
165: .getDeclaringClass()),
166: toVMClassName(Ref.class), toVMClassName(field
167: .getType()), toVMTypeName(Ref.class),
168: toVMTypeName(field.getType()), "f_ref", field
169: .getName());
170:
171: if (opt == null)
172: return null;
173:
174: Accessor<B, V> acc = instanciate(opt);
175: if (acc != null)
176: logger.log(Level.FINE, "Using optimized Accessor for "
177: + field);
178: return acc;
179: }
180:
181: private static <B, V> Accessor<B, V> instanciate(Class opt) {
182: try {
183: return (Accessor<B, V>) opt.newInstance();
184: } catch (InstantiationException e) {
185: logger.log(Level.INFO,
186: "failed to load an optimized Accessor", e);
187: } catch (IllegalAccessException e) {
188: logger.log(Level.INFO,
189: "failed to load an optimized Accessor", e);
190: } catch (SecurityException e) {
191: logger.log(Level.INFO,
192: "failed to load an optimized Accessor", e);
193: }
194: return null;
195: }
196: }
|