org.eclipse.persistence.jaxb.javamodel.xjc.XJCJavaModelImpl 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, 2021 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.1 - Initial implementation
package org.eclipse.persistence.jaxb.javamodel.xjc;
import java.lang.annotation.Annotation;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.persistence.dynamic.DynamicClassLoader;
import org.eclipse.persistence.internal.oxm.XMLConversionManager;
import org.eclipse.persistence.jaxb.javamodel.JavaAnnotation;
import org.eclipse.persistence.jaxb.javamodel.JavaClass;
import org.eclipse.persistence.jaxb.javamodel.JavaModel;
import com.sun.codemodel.JClassAlreadyExistsException;
import com.sun.codemodel.JCodeModel;
/**
* INTERNAL:
*
* Purpose: JavaModel
implementation wrapping XJC's JCodeModel
. Used when
* bootstrapping a DynamicJAXBContext
from an XML Schema.
*
*
*
* Responsibilities:
*
*
* - Return a
JavaClass
based on a Class
or Class
name.
* - Return a Java
Annotation
for a given JavaAnnotation
.
*
*
* @since EclipseLink 2.1
*
* @see org.eclipse.persistence.jaxb.javamodel.JavaModel
*/
public class XJCJavaModelImpl implements JavaModel {
private JCodeModel jCodeModel;
private DynamicClassLoader dynamicClassLoader;
private Map javaModelClasses = new HashMap<>();
/**
* Construct a new instance of XJCJavaModelImpl
.
*
* @param codeModel - the XJC JCodeModel
to be wrapped.
* @param loader - the ClassLoader
used to bootstrap the DynamicJAXBContext
.
*/
public XJCJavaModelImpl(JCodeModel codeModel, DynamicClassLoader loader) {
this.jCodeModel = codeModel;
this.dynamicClassLoader = loader;
}
/**
* Obtain the JavaClass
given the corresponding Java Class
.
*
* @param jClass - the Java Class
to search for.
*
* @return the JavaClass
corresponding to jClass
.
*/
@Override
public JavaClass getClass(Class> jClass) {
if (jClass == null) {
return null;
}
JavaClass cachedClass = this.javaModelClasses.get(jClass.getCanonicalName());
if (cachedClass != null) {
return cachedClass;
}
try {
XJCJavaClassImpl jc = new XJCJavaClassImpl(jCodeModel._class(jClass.getCanonicalName()), jCodeModel, dynamicClassLoader);
jc.setJavaModel(this);
this.javaModelClasses.put(jClass.getCanonicalName(), jc);
return jc;
} catch (JClassAlreadyExistsException ex) {
XJCJavaClassImpl jc = new XJCJavaClassImpl(jCodeModel._getClass(jClass.getCanonicalName()), jCodeModel, dynamicClassLoader);
this.javaModelClasses.put(jClass.getCanonicalName(), jc);
jc.setJavaModel(this);
return jc;
}
}
/**
* 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
.
*/
@Override
public JavaClass getClass(String className) {
JavaClass cachedClass = this.javaModelClasses.get(className);
if (cachedClass != null) {
return cachedClass;
}
String componentName = className;
boolean isArray = className.contains("[]");
if (isArray) {
componentName = className.replace("[]", "");
}
boolean isTyped = className.contains("<");
if (isTyped) {
// Only keep the generic part
componentName = componentName.substring(0, className.indexOf('<'));
}
boolean isPrimitive = XMLConversionManager.getPrimitiveClass(componentName) != null;
try {
JavaClass jc = new XJCJavaClassImpl(jCodeModel._class(componentName), jCodeModel, dynamicClassLoader, isArray, isPrimitive);
this.javaModelClasses.put(className, jc);
return jc;
} catch (JClassAlreadyExistsException ex) {
JavaClass jc = new XJCJavaClassImpl(jCodeModel._getClass(componentName), jCodeModel, dynamicClassLoader, isArray, isPrimitive);
this.javaModelClasses.put(className, jc);
return jc;
}
}
/**
* Return a Java Annotation
representation of the given JavaAnnotation
.
*
* @param annotation - the JavaAnnotation
to be converted.
* @param jClass - the Java Class
this annotation belogs to.
*
* @return a Java Annotation
representation of the given JavaAnnotation
.
*/
@Override
public Annotation getAnnotation(JavaAnnotation annotation, Class> jClass) {
return ((XJCJavaAnnotationImpl) annotation).getJavaAnnotation();
}
/**
* Returns a Map
of this JavaModel's
JavaClasses
, keyed on class name.
*
* @return this JavaModel's
Map
of JavaClasses
.
*/
public Map getJavaModelClasses() {
return javaModelClasses;
}
/**
* Sets the Map
of JavaClasses
for this JavaModel's
, keyed on class name.
*
* @param javaModelClasses - a Map
of JavaClasses
, keyed on class name.
*/
public void setJavaModelClasses(Map javaModelClasses) {
this.javaModelClasses = javaModelClasses;
}
/**
* Returns this JavaModel's
ClassLoader
.
*
* @return the ClassLoader
used by this JavaModel
.
*/
@Override
public ClassLoader getClassLoader() {
return this.dynamicClassLoader;
}
}