use of javax.xml.bind.annotation.XmlRootElement in project camel by apache.
the class CoreEipAnnotationProcessor method processRefExpression.
/**
* Special for processing an @XmlElementRef expression field
*/
private void processRefExpression(ProcessingEnvironment processingEnv, RoundEnvironment roundEnv, TypeElement originalClassType, TypeElement classElement, XmlElementRef elementRef, VariableElement fieldElement, String fieldName, Set<EipOption> eipOptions, String prefix) {
Elements elementUtils = processingEnv.getElementUtils();
if ("expression".equals(fieldName)) {
String kind = "expression";
String name = elementRef.name();
if (isNullOrEmpty(name) || "##default".equals(name)) {
name = fieldName;
}
name = prefix + name;
TypeMirror fieldType = fieldElement.asType();
String fieldTypeName = fieldType.toString();
// find javadoc from original class as it will override the setExpression method where we can provide the javadoc for the given EIP
String docComment = findJavaDoc(elementUtils, fieldElement, fieldName, name, originalClassType, true);
// is it used as predicate (check field first and then fallback to its class / original class)
boolean asPredicate = fieldElement.getAnnotation(AsPredicate.class) != null;
if (!asPredicate) {
asPredicate = classElement.getAnnotation(AsPredicate.class) != null;
}
if (!asPredicate) {
asPredicate = originalClassType.getAnnotation(AsPredicate.class) != null;
}
// gather oneOf expression/predicates which uses language
Set<String> oneOfTypes = new TreeSet<String>();
for (String language : ONE_OF_LANGUAGES) {
TypeElement languages = findTypeElement(processingEnv, roundEnv, language);
String superClassName = canonicalClassName(languages.toString());
// find all classes that has that superClassName
Set<TypeElement> children = new LinkedHashSet<TypeElement>();
findTypeElementChildren(processingEnv, roundEnv, children, superClassName);
for (TypeElement child : children) {
XmlRootElement rootElement = child.getAnnotation(XmlRootElement.class);
if (rootElement != null) {
String childName = rootElement.name();
oneOfTypes.add(childName);
}
}
}
boolean deprecated = fieldElement.getAnnotation(Deprecated.class) != null;
String displayName = null;
Metadata metadata = fieldElement.getAnnotation(Metadata.class);
if (metadata != null) {
displayName = metadata.displayName();
}
EipOption ep = new EipOption(name, displayName, kind, fieldTypeName, true, "", docComment, deprecated, false, null, true, oneOfTypes, asPredicate);
eipOptions.add(ep);
}
}
use of javax.xml.bind.annotation.XmlRootElement in project Gargoyle by callakrsos.
the class SAXPasrerUtil method saveXml.
// private static final Logger LOGGER =
// LoggerFactory.getLogger(SAXPasrerUtil.class);
/**
* 특정객체를 파일로 저장
*
* 단.. 클래스에 XmlRootElement 어노테이션이 붙어 있을것
*
* @작성자 : KYJ
* @작성일 : 2016. 3. 16.
* @param file
* @param job
* @throws JAXBException
* @throws GargoyleException
*/
public static <T> void saveXml(File file, T job) throws JAXBException, GargoyleException {
if (job.getClass().getAnnotation(XmlRootElement.class) == null)
throw new GargoyleException("this object is not contains XmlRootElement annotation");
JAXBContext context = JAXBContext.newInstance(job.getClass());
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.marshal(job, file);
}
use of javax.xml.bind.annotation.XmlRootElement in project opennms by OpenNMS.
the class JaxbUtils method getClassForElement.
public static Class<?> getClassForElement(final String elementName) {
if (elementName == null)
return null;
final Class<?> existing = m_elementClasses.get(elementName);
if (existing != null)
return existing;
final ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false);
scanner.addIncludeFilter(new AnnotationTypeFilter(XmlRootElement.class));
for (final BeanDefinition bd : scanner.findCandidateComponents("org.opennms")) {
final String className = bd.getBeanClassName();
try {
final Class<?> clazz = Class.forName(className);
final XmlRootElement annotation = clazz.getAnnotation(XmlRootElement.class);
if (annotation == null) {
LOG.warn("Somehow found class {} but it has no @XmlRootElement annotation! Skipping.", className);
continue;
}
if (elementName.equalsIgnoreCase(annotation.name())) {
LOG.trace("Found class {} for element name {}", className, elementName);
m_elementClasses.put(elementName, clazz);
return clazz;
}
} catch (final ClassNotFoundException e) {
LOG.warn("Unable to get class object from class name {}. Skipping.", className, e);
}
}
return null;
}
use of javax.xml.bind.annotation.XmlRootElement in project winery by eclipse.
the class Util method getJAXBElement.
public static <T> JAXBElement<T> getJAXBElement(Class<T> clazz, T obj) {
String namespace = null;
XmlRootElement xmlRootElement = clazz.getAnnotation(XmlRootElement.class);
if (xmlRootElement != null) {
namespace = xmlRootElement.namespace();
if ("##default".equals(namespace)) {
XmlSchema xmlSchema = clazz.getPackage().getAnnotation(XmlSchema.class);
if (xmlSchema != null) {
namespace = xmlSchema.namespace();
} else {
// trigger default handling
namespace = null;
}
}
}
if (namespace == null) {
// fallback non-specified namespaces
namespace = Namespaces.TOSCA_NAMESPACE;
}
String localName = Util.getLocalName(clazz);
QName qname = new QName(namespace, localName);
return new JAXBElement<T>(qname, clazz, obj);
}
use of javax.xml.bind.annotation.XmlRootElement in project cxf by apache.
the class JAXBExtensionHelper method addExtensions.
public static void addExtensions(ExtensionRegistry registry, Class<?> parentType, Class<?> cls, String namespace, ClassLoader loader) throws JAXBException {
JAXBExtensionHelper helper = new JAXBExtensionHelper(cls, namespace);
boolean found = false;
Class<?> extCls = cls;
try {
Class<?> objectFactory = Class.forName(PackageUtils.getPackageName(cls) + ".ObjectFactory", true, loader);
Method[] methods = ReflectionUtil.getDeclaredMethods(objectFactory);
for (Method method : methods) {
if (method.getParameterTypes().length == 1 && method.getParameterTypes()[0].equals(cls)) {
XmlElementDecl elementDecl = method.getAnnotation(XmlElementDecl.class);
if (null != elementDecl) {
String name = elementDecl.name();
String ns = namespace != null ? namespace : elementDecl.namespace();
if (namespace != null) {
helper.setJaxbNamespace(elementDecl.namespace());
}
QName elementType = new QName(ns, name);
if (!ExtensibilityElement.class.isAssignableFrom(extCls)) {
extCls = createExtensionClass(cls, elementType, loader);
helper.setExtensionClass(extCls);
}
registry.registerDeserializer(parentType, elementType, helper);
registry.registerSerializer(parentType, elementType, helper);
registry.mapExtensionTypes(parentType, elementType, extCls);
found = true;
}
}
}
} catch (ClassNotFoundException ex) {
// ignore
}
if (!found) {
// not in object factory or no object factory, try other annotations
XmlRootElement elAnnot = cls.getAnnotation(XmlRootElement.class);
if (elAnnot != null) {
String name = elAnnot.name();
String ns = elAnnot.namespace();
if (StringUtils.isEmpty(ns) || "##default".equals(ns)) {
XmlSchema schema = null;
if (cls.getPackage() != null) {
schema = cls.getPackage().getAnnotation(XmlSchema.class);
}
if (schema != null) {
ns = schema.namespace();
}
}
if (!StringUtils.isEmpty(ns) && !StringUtils.isEmpty(name)) {
if (namespace != null) {
helper.setJaxbNamespace(ns);
ns = namespace;
}
QName elementType = new QName(ns, name);
if (!ExtensibilityElement.class.isAssignableFrom(extCls)) {
extCls = createExtensionClass(cls, elementType, loader);
helper.setExtensionClass(extCls);
}
registry.registerDeserializer(parentType, elementType, helper);
registry.registerSerializer(parentType, elementType, helper);
registry.mapExtensionTypes(parentType, elementType, extCls);
found = true;
}
}
}
if (!found) {
LOG.log(Level.WARNING, "EXTENSION_NOT_REGISTERED", new Object[] { cls.getName(), parentType.getName() });
}
}
Aggregations