use of org.eclipse.persistence.internal.jaxb.JaxbClassLoader in project eclipselink by eclipse-ee4j.
the class PropertyTypeTestCases method testPropertyTypes.
public void testPropertyTypes() throws Exception {
JaxbClassLoader classLoader = new JaxbClassLoader(Thread.currentThread().getContextClassLoader());
Generator generator = new Generator(new JavaModelInputImpl(new Class<?>[] { TestObject.class }, new JavaModelImpl(this.classLoader)));
CoreProject proj = generator.generateProject();
TypeInfo info = generator.getAnnotationsProcessor().getTypeInfos().get("org.eclipse.persistence.testing.jaxb.annotations.TestObject");
Property booleanProp = info.getProperties().get("booleanTest");
JavaClass type = booleanProp.getType();
assertEquals("java.lang.Boolean", type.getRawName());
Property booleanListProp = info.getProperties().get("booleanListTest");
type = booleanListProp.getType();
assertEquals("java.util.List", type.getRawName());
Property byteArrayProp = info.getProperties().get("byteArrayTest");
type = byteArrayProp.getType();
assertEquals("java.lang.String", type.getRawName());
Property byteArrayListProp = info.getProperties().get("byteArrayListTest");
type = byteArrayListProp.getType();
assertEquals("java.util.List", type.getRawName());
assertEquals("java.lang.String", byteArrayListProp.getGenericType().getRawName());
}
use of org.eclipse.persistence.internal.jaxb.JaxbClassLoader in project eclipselink by eclipse-ee4j.
the class MappingsGenerator method generateWrapperClassAndDescriptor.
private Class<?> generateWrapperClassAndDescriptor(TypeInfo type, QName next, ElementDeclaration nextElement, String nextClassName, String attributeTypeName) {
String namespaceUri = null;
if (next != null) {
// generate a class/descriptor for this element
namespaceUri = next.getNamespaceURI();
if (namespaceUri == null || namespaceUri.equals(XMLProcessor.DEFAULT)) {
namespaceUri = "";
}
}
TypeMappingInfo tmi = nextElement.getTypeMappingInfo();
Class<?> generatedClass = null;
JaxbClassLoader loader = getJaxbClassLoader();
if (tmi != null) {
generatedClass = CompilerHelper.getExisitingGeneratedClass(tmi, typeMappingInfoToGeneratedClasses, typeMappingInfoToAdapterClasses, helper.getClassLoader());
if (generatedClass == null) {
generatedClass = this.generateWrapperClass(loader.nextAvailableGeneratedClassName(), attributeTypeName, nextElement.isList(), next);
}
typeMappingInfoToGeneratedClasses.put(tmi, generatedClass);
} else {
generatedClass = this.generateWrapperClass(loader.nextAvailableGeneratedClassName(), attributeTypeName, nextElement.isList(), next);
}
this.qNamesToGeneratedClasses.put(next, generatedClass);
try {
Class<Object> declaredClass = PrivilegedAccessHelper.getClassForName(nextClassName, false, helper.getClassLoader());
this.qNamesToDeclaredClasses.put(next, declaredClass);
} catch (Exception e) {
}
Descriptor desc = (Descriptor) project.getDescriptor(generatedClass);
if (desc == null) {
desc = new XMLDescriptor();
desc.setJavaClass(generatedClass);
if (nextElement.isList()) {
DirectCollectionMapping<AbstractSession, AttributeAccessor, ContainerPolicy, Converter, ClassDescriptor, DatabaseField, XMLMarshaller, Session, XMLUnmarshaller, XMLRecord> mapping = new XMLCompositeDirectCollectionMapping();
mapping.setAttributeName("value");
mapping.setXPath("text()");
mapping.setUsesSingleNode(true);
mapping.setReuseContainer(true);
if (type != null && type.isEnumerationType()) {
mapping.setValueConverter(buildJAXBEnumTypeConverter(mapping, (EnumTypeInfo) type));
} else {
try {
Class<Object> fieldElementClass = PrivilegedAccessHelper.getClassForName(nextClassName, false, helper.getClassLoader());
mapping.setFieldElementClass(fieldElementClass);
} catch (ClassNotFoundException e) {
}
}
if (nextClassName.equals("[B") || nextClassName.equals("[Ljava.lang.Byte;")) {
((Field) mapping.getField()).setSchemaType(Constants.BASE_64_BINARY_QNAME);
} else if (nextClassName.equals("javax.xml.namespace.QName")) {
((Field) mapping.getField()).setSchemaType(Constants.QNAME_QNAME);
}
desc.addMapping((CoreMapping) mapping);
} else {
if (nextElement.getJavaTypeName().equals(OBJECT_CLASS_NAME)) {
CompositeObjectMapping<AbstractSession, AttributeAccessor, ContainerPolicy, Converter, ClassDescriptor, DatabaseField, XMLMarshaller, Session, UnmarshalKeepAsElementPolicy, XMLUnmarshaller, XMLRecord> mapping = new XMLCompositeObjectMapping();
mapping.setAttributeName("value");
mapping.setSetMethodName("setValue");
mapping.setGetMethodName("getValue");
mapping.setKeepAsElementPolicy(UnmarshalKeepAsElementPolicy.KEEP_UNKNOWN_AS_ELEMENT);
mapping.setXPath(".");
setTypedTextField((Field) mapping.getField());
desc.addMapping((CoreMapping) mapping);
} else if (isBinaryData(nextElement.getJavaType())) {
BinaryDataMapping<AbstractSession, AttributeAccessor, ContainerPolicy, Converter, ClassDescriptor, DatabaseField, XMLMarshaller, MimeTypePolicy, Session, XMLUnmarshaller, XMLRecord> mapping = new XMLBinaryDataMapping();
mapping.setAttributeName("value");
mapping.setXPath(".");
((Field) mapping.getField()).setSchemaType(Constants.BASE_64_BINARY_QNAME);
mapping.setSetMethodName("setValue");
mapping.setGetMethodName("getValue");
mapping.getNullPolicy().setNullRepresentedByXsiNil(true);
mapping.getNullPolicy().setNullRepresentedByEmptyNode(false);
Class<?> attributeClassification = org.eclipse.persistence.internal.helper.Helper.getClassFromClasseName(attributeTypeName, helper.getClassLoader());
mapping.setAttributeClassification(attributeClassification);
mapping.setShouldInlineBinaryData(false);
// if(nextElement.getTypeMappingInfo() != null) {
mapping.setSwaRef(nextElement.isXmlAttachmentRef());
mapping.setMimeType(nextElement.getXmlMimeType());
// }
desc.addMapping((CoreMapping) mapping);
} else {
DirectMapping<AbstractSession, AttributeAccessor, ContainerPolicy, Converter, ClassDescriptor, DatabaseField, XMLMarshaller, Session, XMLUnmarshaller, XMLRecord> mapping = new XMLDirectMapping();
mapping.setNullValueMarshalled(true);
mapping.setAttributeName("value");
mapping.setXPath("text()");
mapping.setSetMethodName("setValue");
mapping.setGetMethodName("getValue");
if (nextElement.getDefaultValue() != null) {
mapping.setNullValue(nextElement.getDefaultValue());
mapping.getNullPolicy().setNullRepresentedByXsiNil(true);
}
if (helper.isBuiltInJavaType(nextElement.getJavaType())) {
Class<?> attributeClassification = null;
if (nextElement.getJavaType().isPrimitive()) {
attributeClassification = XMLConversionManager.getDefaultManager().convertClassNameToClass(attributeTypeName);
} else {
attributeClassification = org.eclipse.persistence.internal.helper.Helper.getClassFromClasseName(attributeTypeName, helper.getClassLoader());
}
mapping.setAttributeClassification(attributeClassification);
}
IsSetNullPolicy nullPolicy = new IsSetNullPolicy("isSetValue", false, true, XMLNullRepresentationType.ABSENT_NODE);
// nullPolicy.setNullRepresentedByEmptyNode(true);
mapping.setNullPolicy(nullPolicy);
if (type != null && type.isEnumerationType()) {
mapping.setConverter(buildJAXBEnumTypeConverter(mapping, (EnumTypeInfo) type));
}
if (nextClassName.equals("[B") || nextClassName.equals("[Ljava.lang.Byte;")) {
((Field) mapping.getField()).setSchemaType(Constants.BASE_64_BINARY_QNAME);
} else if (nextClassName.equals("javax.xml.namespace.QName")) {
((Field) mapping.getField()).setSchemaType(Constants.QNAME_QNAME);
}
if (nextElement.getJavaTypeAdapterClass() != null) {
mapping.setConverter(new XMLJavaTypeConverter(nextElement.getJavaTypeAdapterClass()));
}
desc.addMapping((CoreMapping) mapping);
}
}
if (next != null) {
NamespaceInfo info = getNamespaceInfoForURI(namespaceUri);
if (info != null) {
NamespaceResolver resolver = getNamespaceResolverForDescriptor(info);
String prefix = null;
if (namespaceUri != Constants.EMPTY_STRING) {
prefix = resolver.resolveNamespaceURI(namespaceUri);
if (prefix == null) {
prefix = getPrefixForNamespace(namespaceUri, resolver);
}
}
desc.setNamespaceResolver(resolver);
if (nextElement.isXmlRootElement()) {
desc.setDefaultRootElement(getQualifiedString(prefix, next.getLocalPart()));
} else {
desc.setDefaultRootElement("");
desc.addRootElement(getQualifiedString(prefix, next.getLocalPart()));
desc.setResultAlwaysXMLRoot(true);
}
} else {
if (namespaceUri.equals("")) {
desc.setDefaultRootElement(next.getLocalPart());
} else {
NamespaceResolver resolver = new org.eclipse.persistence.oxm.NamespaceResolver();
String prefix = getPrefixForNamespace(namespaceUri, resolver);
desc.setNamespaceResolver(resolver);
if (nextElement.isXmlRootElement()) {
desc.setDefaultRootElement(getQualifiedString(prefix, next.getLocalPart()));
} else {
desc.setDefaultRootElement("");
desc.addRootElement(getQualifiedString(prefix, next.getLocalPart()));
desc.setResultAlwaysXMLRoot(true);
}
}
}
}
project.addDescriptor((CoreDescriptor) desc);
}
return generatedClass;
}
use of org.eclipse.persistence.internal.jaxb.JaxbClassLoader in project eclipselink by eclipse-ee4j.
the class AnnotationsProcessor method generateClassFromBytes.
private Class<?> generateClassFromBytes(String className, byte[] classBytes) {
JaxbClassLoader loader = (JaxbClassLoader) helper.getClassLoader();
Class<?> generatedClass = loader.generateClass(className, classBytes);
return generatedClass;
}
use of org.eclipse.persistence.internal.jaxb.JaxbClassLoader in project eclipselink by eclipse-ee4j.
the class ExternalizedMetadataTestCases method createXmlContext.
/**
* Convenience method that returns a newly created XMLContext based on an array of classes.
*/
protected XMLContext createXmlContext(Class<?>[] classes) {
try {
ClassLoader classLoader = new JaxbClassLoader(Thread.currentThread().getContextClassLoader());
Generator generator = new Generator(new JavaModelInputImpl(classes, new JavaModelImpl(classLoader)));
CoreProject proj = generator.generateProject();
ConversionManager manager = new ConversionManager();
manager.setLoader(classLoader);
for (Iterator<ClassDescriptor> descriptorIt = proj.getOrderedDescriptors().iterator(); descriptorIt.hasNext(); ) {
ClassDescriptor descriptor = descriptorIt.next();
if (descriptor.getJavaClass() == null) {
descriptor.setJavaClass(manager.convertClassNameToClass(descriptor.getJavaClassName()));
}
}
return new XMLContext((Project) proj, classLoader);
} catch (Exception e) {
e.printStackTrace();
fail("XmlContext creation failed");
}
return null;
}
use of org.eclipse.persistence.internal.jaxb.JaxbClassLoader in project eclipselink by eclipse-ee4j.
the class JAXBContextTransformationMappingTestCases method testInvalidMethod.
/**
* Test exception handling: in this case the method name that has been
* set is invalid (wrong number or type of params. An exception should
* be thrown.
*
* Negative test.
*/
public void testInvalidMethod() {
InputStream inputStream = ClassLoader.getSystemResourceAsStream(METADATA_FILE_BAD_METHOD);
HashMap<String, Source> metadataSourceMap = new HashMap<>();
metadataSourceMap.put("org.eclipse.persistence.testing.jaxb.externalizedmetadata.mappings.xmltransformation", new StreamSource(inputStream));
Map<String, Object> properties = new HashMap<>();
properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, metadataSourceMap);
int exceptionCount = 0;
try {
JAXBContext jaxbContext = JAXBContextFactory.createContext(new Class<?>[] { Employee.class }, properties, Thread.currentThread().getContextClassLoader());
} catch (JAXBException e) {
exceptionCount++;
}
assertTrue("The expected exception was never thrown.", exceptionCount > 0);
exceptionCount--;
// test exception from SchemaGenerator
try {
Map<String, XmlBindings> bindings = JAXBContextFactory.getXmlBindingsFromProperties(properties, Thread.currentThread().getContextClassLoader());
JavaModelInputImpl jModelInput = new JavaModelInputImpl(new Class<?>[] { Employee.class }, new JavaModelImpl(new JaxbClassLoader(Thread.currentThread().getContextClassLoader(), new Class<?>[] { Employee.class })));
Generator generator = new Generator(jModelInput, bindings, Thread.currentThread().getContextClassLoader(), "", false);
generator.generateSchema();
} catch (Exception e) {
exceptionCount++;
}
assertTrue("The expected exception was never thrown.", exceptionCount > 0);
}
Aggregations