1 /*
2 * Copyright (c) 1997, 2015, 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
91 import com.sun.tools.internal.xjc.model.CClassRef;
92 import com.sun.tools.internal.xjc.outline.Aspect;
93 import com.sun.tools.internal.xjc.outline.ClassOutline;
94 import com.sun.tools.internal.xjc.outline.EnumConstantOutline;
95 import com.sun.tools.internal.xjc.outline.EnumOutline;
96 import com.sun.tools.internal.xjc.outline.FieldOutline;
97 import com.sun.tools.internal.xjc.outline.Outline;
98 import com.sun.tools.internal.xjc.outline.PackageOutline;
99 import com.sun.tools.internal.xjc.util.CodeModelClassFactory;
100 import com.sun.xml.internal.bind.v2.model.core.PropertyInfo;
101 import com.sun.xml.internal.bind.v2.runtime.SwaRefAdapterMarker;
102 import com.sun.xml.internal.xsom.XmlString;
103 import com.sun.istack.internal.NotNull;
104 import com.sun.tools.internal.xjc.model.CReferencePropertyInfo;
105
106 /**
107 * Generates fields and accessors.
108 */
109 public final class BeanGenerator implements Outline {
110
111 /** Simplifies class/interface creation and collision detection. */
112 private final CodeModelClassFactory codeModelClassFactory;
113 private final ErrorReceiver errorReceiver;
114 /** all {@link PackageOutline}s keyed by their {@link PackageOutline#_package}. */
115 private final Map<JPackage, PackageOutlineImpl> packageContexts = new LinkedHashMap<JPackage, PackageOutlineImpl>();
116 /** all {@link ClassOutline}s keyed by their {@link ClassOutline#target}. */
117 private final Map<CClassInfo, ClassOutlineImpl> classes = new LinkedHashMap<CClassInfo, ClassOutlineImpl>();
118 /** all {@link EnumOutline}s keyed by their {@link EnumOutline#target}. */
119 private final Map<CEnumLeafInfo, EnumOutline> enums = new LinkedHashMap<CEnumLeafInfo, EnumOutline>();
120 /**
121 * Generated runtime classes.
122 */
123 private final Map<Class, JClass> generatedRuntime = new LinkedHashMap<Class, JClass>();
124 /** the model object which we are processing. */
125 private final Model model;
126 private final JCodeModel codeModel;
127 /**
128 * for each property, the information about the generated field.
129 */
130 private final Map<CPropertyInfo, FieldOutline> fields = new LinkedHashMap<CPropertyInfo, FieldOutline>();
235 String shortName = base.fullName().substring(base.fullName().indexOf(pkg)+pkg.length()+1);
236 if (cc.target.shortName.equals(shortName)) {
237 getErrorReceiver().error(cc.target.getLocator(), Messages.ERR_KEYNAME_COLLISION.format(shortName));
238 }
239 }
240
241 }
242
243 // fill in implementation classes
244 for (ClassOutlineImpl co : getClasses()) {
245 generateClassBody(co);
246 }
247
248 for (EnumOutline eo : enums.values()) {
249 generateEnumBody(eo);
250 }
251
252 // create factories for the impl-less elements
253 for (CElementInfo ei : model.getAllElements()) {
254 getPackageContext(ei._package()).objectFactoryGenerator().populate(ei);
255 }
256
257 if (model.options.debugMode) {
258 generateClassList();
259 }
260 }
261
262 /**
263 * Generates a class that knows how to create an instance of JAXBContext
264 *
265 * <p>
266 * This is used in the debug mode so that a new properly configured
267 * {@link JAXBContext} object can be used.
268 */
269 @SuppressWarnings("CallToThreadDumpStack")
270 private void generateClassList() {
271 try {
272 JDefinedClass jc = codeModel.rootPackage()._class("JAXBDebug");
273 JMethod m = jc.method(JMod.PUBLIC | JMod.STATIC, JAXBContext.class, "createContext");
274 JVar $classLoader = m.param(ClassLoader.class, "classLoader");
|
1 /*
2 * Copyright (c) 1997, 2016, 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
91 import com.sun.tools.internal.xjc.model.CClassRef;
92 import com.sun.tools.internal.xjc.outline.Aspect;
93 import com.sun.tools.internal.xjc.outline.ClassOutline;
94 import com.sun.tools.internal.xjc.outline.EnumConstantOutline;
95 import com.sun.tools.internal.xjc.outline.EnumOutline;
96 import com.sun.tools.internal.xjc.outline.FieldOutline;
97 import com.sun.tools.internal.xjc.outline.Outline;
98 import com.sun.tools.internal.xjc.outline.PackageOutline;
99 import com.sun.tools.internal.xjc.util.CodeModelClassFactory;
100 import com.sun.xml.internal.bind.v2.model.core.PropertyInfo;
101 import com.sun.xml.internal.bind.v2.runtime.SwaRefAdapterMarker;
102 import com.sun.xml.internal.xsom.XmlString;
103 import com.sun.istack.internal.NotNull;
104 import com.sun.tools.internal.xjc.model.CReferencePropertyInfo;
105
106 /**
107 * Generates fields and accessors.
108 */
109 public final class BeanGenerator implements Outline {
110
111 /** JAXB module name. JAXB dependency is mandatory in generated Java module. */
112 private static final String JAXB_PACKAGE = "java.xml.bind";
113
114 /** Simplifies class/interface creation and collision detection. */
115 private final CodeModelClassFactory codeModelClassFactory;
116 private final ErrorReceiver errorReceiver;
117 /** all {@link PackageOutline}s keyed by their {@link PackageOutline#_package}. */
118 private final Map<JPackage, PackageOutlineImpl> packageContexts = new LinkedHashMap<JPackage, PackageOutlineImpl>();
119 /** all {@link ClassOutline}s keyed by their {@link ClassOutline#target}. */
120 private final Map<CClassInfo, ClassOutlineImpl> classes = new LinkedHashMap<CClassInfo, ClassOutlineImpl>();
121 /** all {@link EnumOutline}s keyed by their {@link EnumOutline#target}. */
122 private final Map<CEnumLeafInfo, EnumOutline> enums = new LinkedHashMap<CEnumLeafInfo, EnumOutline>();
123 /**
124 * Generated runtime classes.
125 */
126 private final Map<Class, JClass> generatedRuntime = new LinkedHashMap<Class, JClass>();
127 /** the model object which we are processing. */
128 private final Model model;
129 private final JCodeModel codeModel;
130 /**
131 * for each property, the information about the generated field.
132 */
133 private final Map<CPropertyInfo, FieldOutline> fields = new LinkedHashMap<CPropertyInfo, FieldOutline>();
238 String shortName = base.fullName().substring(base.fullName().indexOf(pkg)+pkg.length()+1);
239 if (cc.target.shortName.equals(shortName)) {
240 getErrorReceiver().error(cc.target.getLocator(), Messages.ERR_KEYNAME_COLLISION.format(shortName));
241 }
242 }
243
244 }
245
246 // fill in implementation classes
247 for (ClassOutlineImpl co : getClasses()) {
248 generateClassBody(co);
249 }
250
251 for (EnumOutline eo : enums.values()) {
252 generateEnumBody(eo);
253 }
254
255 // create factories for the impl-less elements
256 for (CElementInfo ei : model.getAllElements()) {
257 getPackageContext(ei._package()).objectFactoryGenerator().populate(ei);
258 }
259
260 if (model.options.getModuleName() != null) {
261 codeModel._prepareModuleInfo(model.options.getModuleName(), JAXB_PACKAGE);
262 }
263
264 if (model.options.debugMode) {
265 generateClassList();
266 }
267 }
268
269 /**
270 * Generates a class that knows how to create an instance of JAXBContext
271 *
272 * <p>
273 * This is used in the debug mode so that a new properly configured
274 * {@link JAXBContext} object can be used.
275 */
276 @SuppressWarnings("CallToThreadDumpStack")
277 private void generateClassList() {
278 try {
279 JDefinedClass jc = codeModel.rootPackage()._class("JAXBDebug");
280 JMethod m = jc.method(JMod.PUBLIC | JMod.STATIC, JAXBContext.class, "createContext");
281 JVar $classLoader = m.param(ClassLoader.class, "classLoader");
|