use of com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror in project enunciate by stoicflame.
the class TypeHintUtils method getTypeHint.
public static TypeMirror getTypeHint(TypeHint hintInfo, DecoratedProcessingEnvironment env, TypeMirror defaultValue) {
TypeMirror typeMirror;
try {
Class hint = hintInfo.value();
if (TypeHint.NO_CONTENT.class.equals(hint)) {
typeMirror = env.getTypeUtils().getNoType(TypeKind.VOID);
} else {
String hintName = hint.getName();
if (TypeHint.NONE.class.equals(hint)) {
hintName = hintInfo.qualifiedName();
}
if (!"##NONE".equals(hintName)) {
TypeElement type = env.getElementUtils().getTypeElement(hintName);
typeMirror = TypeMirrorDecorator.decorate(env.getTypeUtils().getDeclaredType(type), env);
} else {
typeMirror = defaultValue;
}
}
} catch (MirroredTypeException e) {
DecoratedTypeMirror decorated = (DecoratedTypeMirror) TypeMirrorDecorator.decorate(e.getTypeMirror(), env);
if (decorated.isInstanceOf(TypeHint.NO_CONTENT.class)) {
typeMirror = env.getTypeUtils().getNoType(TypeKind.VOID);
} else if (decorated instanceof DeclaredType) {
String hintName = ((TypeElement) ((DeclaredType) decorated).asElement()).getQualifiedName().toString();
if (decorated.isInstanceOf(TypeHint.NONE.class)) {
hintName = hintInfo.qualifiedName();
}
if (!"##NONE".equals(hintName)) {
TypeElement type = env.getElementUtils().getTypeElement(hintName);
if (type != null) {
typeMirror = TypeMirrorDecorator.decorate(env.getTypeUtils().getDeclaredType(type), env);
} else {
env.getMessager().printMessage(Diagnostic.Kind.WARNING, "Unable to find element " + hintName);
typeMirror = null;
}
} else {
typeMirror = defaultValue;
}
} else {
typeMirror = decorated;
}
}
return typeMirror;
}
use of com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror in project enunciate by stoicflame.
the class EnunciateJaxrsContext method add.
/**
* Add a root resource to the model.
*
* @param rootResource The root resource to add to the model.
*/
public void add(RootResource rootResource) {
if (rootResource.isInterface()) {
// if the root resource is an interface, don't add it if its implementation has already been added (avoid duplication).
for (RootResource resource : this.rootResources) {
if (((DecoratedTypeMirror) (resource.asType())).isInstanceOf(rootResource)) {
debug("%s was identified as a JAX-RS root resource, but will be ignored because root resource %s implements it.", rootResource.getQualifiedName(), resource.getQualifiedName());
return;
}
}
} else {
// remove any interfaces of this root resource that have been identified as root resources (avoid duplication)
DecoratedTypeMirror rootResourceType = (DecoratedTypeMirror) rootResource.asType();
Iterator<RootResource> it = this.rootResources.iterator();
while (it.hasNext()) {
RootResource resource = it.next();
if (resource.isInterface() && rootResourceType.isInstanceOf(resource)) {
debug("%s was identified as a JAX-RS root resource, but will be ignored because root resource %s implements it.", resource.getQualifiedName(), rootResource.getQualifiedName());
it.remove();
}
}
}
this.rootResources.add(rootResource);
debug("Added %s as a JAX-RS root resource.", rootResource.getQualifiedName());
if (getContext().getProcessingEnvironment().findSourcePosition(rootResource) == null) {
OneTimeLogMessage.SOURCE_FILES_NOT_FOUND.log(getContext());
if (OneTimeLogMessage.SOURCE_FILES_NOT_FOUND.getLogged() <= 3) {
info("Unable to find source file for %s.", rootResource.getQualifiedName());
} else {
debug("Unable to find source file for %s.", rootResource.getQualifiedName());
}
}
}
use of com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror in project enunciate by stoicflame.
the class JAXBUtil method findAdapterType.
protected static AdapterType findAdapterType(DecoratedTypeMirror maybeContainedAdaptedType, Element referer, PackageElement pckg, EnunciateJaxbContext context) {
DecoratedProcessingEnvironment env = context.getContext().getProcessingEnvironment();
TypeMirror adaptedType = TypeMirrorUtils.getComponentType(maybeContainedAdaptedType, env);
adaptedType = adaptedType == null ? maybeContainedAdaptedType : adaptedType;
XmlJavaTypeAdapter typeAdapterInfo = referer != null ? referer.getAnnotation(XmlJavaTypeAdapter.class) : null;
if (adaptedType instanceof DeclaredType) {
if (typeAdapterInfo == null) {
typeAdapterInfo = ((DeclaredType) adaptedType).asElement().getAnnotation(XmlJavaTypeAdapter.class);
}
if ((typeAdapterInfo == null) && (pckg != null)) {
TypeElement typeDeclaration = (TypeElement) ((DeclaredType) adaptedType).asElement();
typeAdapterInfo = getAdaptersOfPackage(pckg, context).get(typeDeclaration.getQualifiedName().toString());
}
}
if (typeAdapterInfo != null) {
final XmlJavaTypeAdapter finalInfo = typeAdapterInfo;
DecoratedTypeMirror adapterTypeMirror = Annotations.mirrorOf(new Callable<Class<?>>() {
@Override
public Class<?> call() throws Exception {
return finalInfo.value();
}
}, env);
if (adapterTypeMirror instanceof DecoratedDeclaredType) {
AdapterType adapterType = new AdapterType((DecoratedDeclaredType) adapterTypeMirror, context.getContext());
if (!context.getContext().getProcessingEnvironment().getTypeUtils().isSameType(adapterType.getAdaptingType(), adaptedType)) {
return adapterType;
}
}
}
return null;
}
use of com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror in project enunciate by stoicflame.
the class JAXBUtil method getAdaptersOfPackage.
/**
* Gets the adapters of the specified package.
*
* @param pckg the package for which to get the adapters.
* @param context The context.
* @return The adapters for the package.
*/
private static Map<String, XmlJavaTypeAdapter> getAdaptersOfPackage(PackageElement pckg, EnunciateJaxbContext context) {
if (pckg == null) {
return null;
}
Map<String, Map<String, XmlJavaTypeAdapter>> adaptersOfAllPackages = (Map<String, Map<String, XmlJavaTypeAdapter>>) context.getContext().getProperty(ADAPTERS_BY_PACKAGE_PROPERTY);
if (adaptersOfAllPackages == null) {
adaptersOfAllPackages = new HashMap<String, Map<String, XmlJavaTypeAdapter>>();
context.getContext().setProperty(ADAPTERS_BY_PACKAGE_PROPERTY, adaptersOfAllPackages);
}
Map<String, XmlJavaTypeAdapter> adaptersOfPackage = adaptersOfAllPackages.get(pckg.getQualifiedName().toString());
if (adaptersOfPackage == null) {
adaptersOfPackage = new HashMap<String, XmlJavaTypeAdapter>();
adaptersOfAllPackages.put(pckg.getQualifiedName().toString(), adaptersOfPackage);
XmlJavaTypeAdapter javaType = pckg.getAnnotation(XmlJavaTypeAdapter.class);
XmlJavaTypeAdapters javaTypes = pckg.getAnnotation(XmlJavaTypeAdapters.class);
if ((javaType != null) || (javaTypes != null)) {
ArrayList<XmlJavaTypeAdapter> allAdaptedTypes = new ArrayList<XmlJavaTypeAdapter>();
if (javaType != null) {
allAdaptedTypes.add(javaType);
}
if (javaTypes != null) {
allAdaptedTypes.addAll(Arrays.asList(javaTypes.value()));
}
for (final XmlJavaTypeAdapter adaptedTypeInfo : allAdaptedTypes) {
DecoratedTypeMirror typeMirror = Annotations.mirrorOf(new Callable<Class<?>>() {
@Override
public Class<?> call() throws Exception {
return adaptedTypeInfo.type();
}
}, context.getContext().getProcessingEnvironment(), XmlJavaTypeAdapter.DEFAULT.class);
if (typeMirror == null) {
throw new EnunciateException("Package " + pckg.getQualifiedName() + ": a type must be specified in " + XmlJavaTypeAdapter.class.getName() + " at the package-level.");
}
if (!(typeMirror instanceof DeclaredType)) {
throw new EnunciateException("Package " + pckg.getQualifiedName() + ": unadaptable type: " + typeMirror);
}
TypeElement typeDeclaration = (TypeElement) ((DeclaredType) typeMirror).asElement();
if (typeDeclaration == null) {
throw new EnunciateException("Element not found: " + typeMirror);
}
adaptersOfPackage.put(typeDeclaration.getQualifiedName().toString(), adaptedTypeInfo);
}
}
}
return adaptersOfPackage;
}
use of com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror in project enunciate by stoicflame.
the class JAXBUtil method getNormalizedCollection.
public static DecoratedDeclaredType getNormalizedCollection(DecoratedTypeMirror typeMirror, DecoratedProcessingEnvironment env) {
DecoratedDeclaredType base = typeMirror.isList() ? TypeMirrorUtils.listType(env) : typeMirror.isCollection() ? TypeMirrorUtils.collectionType(env) : null;
if (base != null) {
// now narrow the component type to what can be valid xml.
DecoratedTypeMirror componentType = findCollectionComponentType((DeclaredType) typeMirror, env);
base = (DecoratedDeclaredType) env.getTypeUtils().getDeclaredType((TypeElement) base.asElement(), componentType);
}
return base;
}
Aggregations