src/share/jaxws_classes/com/sun/xml/internal/bind/v2/runtime/reflect/opt/OptimizedAccessorFactory.java

changeset 286
f50545b5e2f1
child 368
0989ad8c0860
equal deleted inserted replaced
284:88b85470e72c 286:f50545b5e2f1
1 /*
2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.xml.internal.bind.v2.runtime.reflect.opt;
27
28 import java.lang.reflect.Field;
29 import java.lang.reflect.Method;
30 import java.lang.reflect.Modifier;
31 import java.util.logging.Level;
32 import java.util.logging.Logger;
33
34 import com.sun.xml.internal.bind.Util;
35 import com.sun.xml.internal.bind.v2.runtime.reflect.Accessor;
36 import com.sun.xml.internal.bind.v2.runtime.RuntimeUtil;
37
38 import static com.sun.xml.internal.bind.v2.bytecode.ClassTailor.toVMClassName;
39 import static com.sun.xml.internal.bind.v2.bytecode.ClassTailor.toVMTypeName;
40
41 /**
42 * @author Kohsuke Kawaguchi
43 */
44 public abstract class OptimizedAccessorFactory {
45 private OptimizedAccessorFactory() {} // no instanciation please
46
47 private static final Logger logger = Util.getClassLogger();
48
49
50 private static final String fieldTemplateName;
51 private static final String methodTemplateName;
52
53 static {
54 String s = FieldAccessor_Byte.class.getName();
55 fieldTemplateName = s.substring(0,s.length()-"Byte".length()).replace('.','/');
56
57 s = MethodAccessor_Byte.class.getName();
58 methodTemplateName = s.substring(0,s.length()-"Byte".length()).replace('.','/');
59 }
60
61 /**
62 * Gets the optimized {@link Accessor} that accesses the given getter/setter.
63 *
64 * @return null
65 * if for some reason it fails to create an optimized version.
66 */
67 public static final <B,V> Accessor<B,V> get(Method getter, Method setter) {
68 // make sure the method signatures are what we expect
69 if(getter.getParameterTypes().length!=0)
70 return null;
71 Class<?>[] sparams = setter.getParameterTypes();
72 if(sparams.length!=1)
73 return null;
74 if(sparams[0]!=getter.getReturnType())
75 return null;
76 if(setter.getReturnType()!=Void.TYPE)
77 return null;
78 if(getter.getDeclaringClass()!=setter.getDeclaringClass())
79 return null;
80 if(Modifier.isPrivate(getter.getModifiers()) || Modifier.isPrivate(setter.getModifiers()))
81 // we can't access private fields
82 return null;
83
84 Class t = sparams[0];
85 String typeName = t.getName().replace('.','_');
86 if (t.isArray()) {
87 typeName = "AOf_";
88 String compName = t.getComponentType().getName().replace('.','_');
89 while (compName.startsWith("[L")) {
90 compName = compName.substring(2);
91 typeName += "AOf_";
92 }
93 typeName = typeName + compName;
94 }
95
96 String newClassName = toVMClassName(getter.getDeclaringClass())+"$JaxbAccessorM_"+getter.getName()+'_'+setter.getName()+'_'+typeName;
97 Class opt;
98
99 if(t.isPrimitive())
100 opt = AccessorInjector.prepare( getter.getDeclaringClass(),
101 methodTemplateName+RuntimeUtil.primitiveToBox.get(t).getSimpleName(),
102 newClassName,
103 toVMClassName(Bean.class),
104 toVMClassName(getter.getDeclaringClass()),
105 "get_"+t.getName(),
106 getter.getName(),
107 "set_"+t.getName(),
108 setter.getName());
109 else
110 opt = AccessorInjector.prepare( getter.getDeclaringClass(),
111 methodTemplateName+"Ref",
112 newClassName,
113 toVMClassName(Bean.class),
114 toVMClassName(getter.getDeclaringClass()),
115 toVMClassName(Ref.class),
116 toVMClassName(t),
117 "()"+toVMTypeName(Ref.class),
118 "()"+toVMTypeName(t),
119 '('+toVMTypeName(Ref.class)+")V",
120 '('+toVMTypeName(t)+")V",
121 "get_ref",
122 getter.getName(),
123 "set_ref",
124 setter.getName());
125
126 if(opt==null)
127 return null;
128
129 Accessor<B,V> acc = instanciate(opt);
130 if(acc!=null)
131 logger.log(Level.FINE,"Using optimized Accessor for "+getter+" and "+setter);
132 return acc;
133 }
134
135
136 /**
137 * Gets the optimizd {@link Accessor} that accesses the given field.
138 *
139 * @return null
140 * if for some reason it fails to create an optimized version.
141 */
142 public static final <B,V> Accessor<B,V> get(Field field) {
143 int mods = field.getModifiers();
144 if(Modifier.isPrivate(mods) || Modifier.isFinal(mods))
145 // we can't access private fields
146 return null;
147
148 String newClassName = toVMClassName(field.getDeclaringClass())+"$JaxbAccessorF_"+field.getName();
149
150 Class opt;
151
152 if(field.getType().isPrimitive())
153 opt = AccessorInjector.prepare( field.getDeclaringClass(),
154 fieldTemplateName+RuntimeUtil.primitiveToBox.get(field.getType()).getSimpleName(),
155 newClassName,
156 toVMClassName(Bean.class),
157 toVMClassName(field.getDeclaringClass()),
158 "f_"+field.getType().getName(),
159 field.getName() );
160 else
161 opt = AccessorInjector.prepare( field.getDeclaringClass(),
162 fieldTemplateName+"Ref",
163 newClassName,
164 toVMClassName(Bean.class),
165 toVMClassName(field.getDeclaringClass()),
166 toVMClassName(Ref.class),
167 toVMClassName(field.getType()),
168 toVMTypeName(Ref.class),
169 toVMTypeName(field.getType()),
170 "f_ref",
171 field.getName() );
172
173 if(opt==null)
174 return null;
175
176 Accessor<B,V> acc = instanciate(opt);
177 if(acc!=null)
178 logger.log(Level.FINE,"Using optimized Accessor for "+field);
179 return acc;
180 }
181
182 private static <B,V> Accessor<B,V> instanciate(Class opt) {
183 try {
184 return (Accessor<B,V>)opt.newInstance();
185 } catch (InstantiationException e) {
186 logger.log(Level.INFO,"failed to load an optimized Accessor",e);
187 } catch (IllegalAccessException e) {
188 logger.log(Level.INFO,"failed to load an optimized Accessor",e);
189 } catch (SecurityException e) {
190 logger.log(Level.INFO,"failed to load an optimized Accessor",e);
191 }
192 return null;
193 }
194 }

mercurial