org.eclipse.persistence.jaxb.javamodel.oxm.OXMJavaModelImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of eclipselink Show documentation
Show all versions of eclipselink Show documentation
EclipseLink build based upon Git transaction f2b9fc5
/*
* Copyright (c) 2011, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/
// Contributors:
// Rick Barkhouse - 2.2 - Initial implementation
package org.eclipse.persistence.jaxb.javamodel.oxm;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.persistence.internal.jaxb.JaxbClassLoader;
import org.eclipse.persistence.internal.security.PrivilegedAccessHelper;
import org.eclipse.persistence.jaxb.javamodel.JavaClass;
import org.eclipse.persistence.jaxb.javamodel.JavaModel;
import org.eclipse.persistence.jaxb.javamodel.reflection.JavaClassImpl;
import org.eclipse.persistence.jaxb.javamodel.reflection.JavaModelImpl;
/**
* INTERNAL:
*
* Purpose: JavaModel
implementation backed by a collection of MOXY's
* xmlmodel.JavaClasses
. Used when bootstrapping a DynamicJAXBContext
* from XML Bindings.
*
*
*
* Responsibilities:
*
*
* - Return a
JavaClass
based on a Class
or Class
name.
*
*
* @since EclipseLink 2.2
*
* @see org.eclipse.persistence.jaxb.javamodel.JavaModel
*/
public class OXMJavaModelImpl extends JavaModelImpl implements JavaModel {
private Map javaModelClasses = new HashMap();
/**
* Construct a new instance of OXMJavaModelImpl
.
*
* @param loader - the ClassLoader
used to bootstrap the DynamicJAXBContext
.
* @param javaClasses - an array of JavaClasses
for which to generate mappings.
*
*/
public OXMJavaModelImpl(ClassLoader loader, JavaClass[] javaClasses) {
super(loader);
for (int i = 0; i < javaClasses.length; i++) {
this.javaModelClasses.put(javaClasses[i].getQualifiedName(), javaClasses[i]);
}
}
/**
* Obtain the JavaClass
given the corresponding Java Class
.
*
* @param jClass - the Java Class
to search for.
*
* @return the JavaClass
corresponding to jClass
.
*/
public JavaClass getClass(Class> jClass) {
if (jClass == null) {
return null;
}
String className = jClass.getCanonicalName();
JavaClass cachedClass = this.javaModelClasses.get(className);
if (cachedClass != null) {
return cachedClass;
}
// try actually finding the class, might be concrete
try {
if (jClass.isArray()) {
className = jClass.getName();
}
Class> realClass = PrivilegedAccessHelper.getClassForName(className, true, classLoader);
if (realClass != null) {
JavaModelImpl jm = new JavaModelImpl(classLoader);
JavaClassImpl jc = new JavaClassImpl(realClass, jm);
return jc;
}
} catch (Exception e) {
}
return new OXMJavaClassImpl(jClass.getCanonicalName());
}
/**
* Obtain the JavaClass
given the corresponding Java Class'
name.
*
* @param className - the name of the Java Class
to search for.
*
* @return the JavaClass
corresponding to className
.
*/
public JavaClass getClass(String className) {
if (className == null) {
return null;
}
// javax.xml.bind.annotation.XmlElement.DEFAULT
if (className.contains(DEFAULT)) {
return getClass(JAVA_LANG_OBJECT);
}
JavaClass cachedClass = this.javaModelClasses.get(className);
if (cachedClass != null) {
return cachedClass;
}
// try actually finding the class, might be concrete
try {
Class> realClass = PrivilegedAccessHelper.getClassForName(className, true, classLoader);
if (realClass != null) {
JavaModelImpl jm = new JavaModelImpl(this.classLoader);
JavaClassImpl jc = new JavaClassImpl(realClass, jm);
return jc;
}
} catch (Exception e) {
}
return new OXMJavaClassImpl(className);
}
/**
* Returns this JavaModel's
ClassLoader
.
*
* @return the ClassLoader
used by this JavaModel
.
*/
public ClassLoader getClassLoader() {
return this.classLoader;
}
/**
* Returns this JavaModel's
JaxbClassLoader
, which
* should be the parent ClassLoader
.
*
* @return the JaxbClassLoader
used by this JavaModel
.
*/
public JaxbClassLoader getJaxbClassLoader() {
ClassLoader parent = getClassLoader().getParent();
if (parent instanceof JaxbClassLoader) {
return (JaxbClassLoader) parent;
}
return null;
}
// ========================================================================
private static String DEFAULT = "DEFAULT";
private static String JAVA_LANG_OBJECT = "java.lang.Object";
}