
org.eclipse.persistence.jaxb.javamodel.xjc.XJCJavaPackageImpl Maven / Gradle / Ivy
/*******************************************************************************
* Copyright (c) 2011, 2014 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 v1.0 and Eclipse Distribution License v. 1.0
* which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Rick Barkhouse - 2.1 - Initial implementation
******************************************************************************/
package org.eclipse.persistence.jaxb.javamodel.xjc;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import org.eclipse.persistence.dynamic.DynamicClassLoader;
import org.eclipse.persistence.exceptions.JAXBException;
import org.eclipse.persistence.internal.security.PrivilegedAccessHelper;
import org.eclipse.persistence.jaxb.javamodel.JavaAnnotation;
import org.eclipse.persistence.jaxb.javamodel.JavaClass;
import org.eclipse.persistence.jaxb.javamodel.JavaPackage;
import com.sun.codemodel.JAnnotationUse;
import com.sun.codemodel.JPackage;
/**
* INTERNAL:
*
* Purpose: JavaPackage
implementation wrapping XJC's JPackage
. Used when
* bootstrapping a DynamicJAXBContext
from an XML Schema.
*
*
*
* Responsibilities:
*
*
* - Provide
Package
information from the underlying JPackage
.
*
*
* @since EclipseLink 2.1
*
* @see org.eclipse.persistence.jaxb.javamodel.JavaPackage
*/
public class XJCJavaPackageImpl implements JavaPackage {
protected JPackage xjcPackage;
private DynamicClassLoader dynamicClassLoader;
private static Field JPACKAGE_ANNOTATIONS = null;
static {
try {
JPACKAGE_ANNOTATIONS = PrivilegedAccessHelper.getDeclaredField(JPackage.class, "annotations", true);
} catch (Exception e) {
throw JAXBException.errorCreatingDynamicJAXBContext(e);
}
}
/**
* Construct a new instance of XJCJavaPackageImpl
.
*
* @param jPackage - the XJC JPackage
to be wrapped.
* @param loader - the ClassLoader
used to bootstrap the DynamicJAXBContext
.
*/
public XJCJavaPackageImpl(JPackage jPackage, DynamicClassLoader loader) {
this.xjcPackage = jPackage;
this.dynamicClassLoader = loader;
}
/**
* If this JavaPackage
is annotated with an Annotation
matching aClass
,
* return its JavaAnnotation
representation.
*
* @param aClass a JavaClass
representing the Annotation
to look for.
*
* @return the JavaAnnotation
represented by aClass
, if one exists, otherwise return null
.
*/
@SuppressWarnings("unchecked")
public JavaAnnotation getAnnotation(JavaClass aClass) {
if (aClass != null) {
Collection annotations = null;
try {
annotations = (Collection) PrivilegedAccessHelper.getValueFromField(JPACKAGE_ANNOTATIONS, xjcPackage);
} catch (Exception e) {
throw JAXBException.errorCreatingDynamicJAXBContext(e);
}
if (annotations == null) {
return null;
}
for (JAnnotationUse annotationUse : annotations) {
XJCJavaAnnotationImpl xjcAnnotation = new XJCJavaAnnotationImpl(annotationUse, dynamicClassLoader);
if (xjcAnnotation.getJavaAnnotationClass().getCanonicalName().equals(aClass.getQualifiedName())) {
return xjcAnnotation;
}
}
// Didn't find annotation so return null
return null;
}
// aClass was null so return null
return null;
}
/**
* Return all of the Annotations
for this JavaPackage
.
*
* @return A Collection
containing this JavaPackage's
JavaAnnotations
.
*/
@SuppressWarnings("unchecked")
public Collection getAnnotations() {
ArrayList annotationsList = new ArrayList();
Collection annotations = null;
try {
annotations = (Collection) PrivilegedAccessHelper.getValueFromField(JPACKAGE_ANNOTATIONS, xjcPackage);
} catch (Exception e) {
throw JAXBException.errorCreatingDynamicJAXBContext(e);
}
if (annotations == null) {
return annotationsList;
}
for (JAnnotationUse annotationUse : annotations) {
XJCJavaAnnotationImpl xjcAnnotation = new XJCJavaAnnotationImpl(annotationUse, dynamicClassLoader);
annotationsList.add(xjcAnnotation);
}
return annotationsList;
}
/**
* Returns the name of this JavaPackage
.
*
* @return the String
name of this JavaPackage
.
*/
public String getName() {
if (xjcPackage != null){
return xjcPackage.name();
}
return null;
}
/**
* Returns the fully-qualified name of this JavaPackage
.
*
* @return the String
name of this JavaPackage
.
*/
public String getQualifiedName() {
return getName();
}
/**
* Not supported.
*/
public JavaAnnotation getDeclaredAnnotation(JavaClass arg0) {
throw new UnsupportedOperationException("getDeclaredAnnotation");
}
/**
* Not supported.
*/
public Collection getDeclaredAnnotations() {
throw new UnsupportedOperationException("getDeclaredAnnotations");
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy