use of com.webcohesion.enunciate.javac.decorations.DecoratedProcessingEnvironment 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.DecoratedProcessingEnvironment in project enunciate by stoicflame.
the class JsonTypeVisitor method visitDeclared.
@Override
public JsonType visitDeclared(DeclaredType declaredType, Context context) {
JsonType jsonType = null;
Element declaredElement = declaredType.asElement();
DecoratedProcessingEnvironment env = context.getContext().getContext().getProcessingEnvironment();
DecoratedTypeMirror decoratedTypeMirror = (DecoratedTypeMirror) TypeMirrorDecorator.decorate(declaredType, env);
String fqn = declaredElement instanceof TypeElement ? ((TypeElement) declaredElement).getQualifiedName().toString() : declaredType.toString();
if (context.getStack().contains(fqn) && !decoratedTypeMirror.isCollection() && !decoratedTypeMirror.isStream()) {
// break out of recursive loop.
return wrapAsNeeded(KnownJsonType.OBJECT, context);
}
context.getStack().push(fqn);
try {
TypeHint typeHint = declaredElement.getAnnotation(TypeHint.class);
if (typeHint != null) {
TypeMirror hint = TypeHintUtils.getTypeHint(typeHint, context.getContext().getContext().getProcessingEnvironment(), null);
if (hint != null) {
jsonType = hint.accept(this, new Context(context.context, false, false, context.stack));
}
}
final JsonSerialize serializeInfo = declaredElement.getAnnotation(JsonSerialize.class);
if (serializeInfo != null) {
DecoratedTypeMirror using = Annotations.mirrorOf(new Callable<Class<?>>() {
@Override
public Class<?> call() throws Exception {
return serializeInfo.using();
}
}, env, JsonSerializer.None.class);
if (using != null) {
// custom serializer; just say it's an object.
jsonType = KnownJsonType.OBJECT;
}
DecoratedTypeMirror as = Annotations.mirrorOf(new Callable<Class<?>>() {
@Override
public Class<?> call() throws Exception {
return serializeInfo.as();
}
}, env, Void.class);
if (as != null) {
jsonType = (JsonType) as.accept(this, new Context(context.context, false, false, context.stack));
}
}
AdapterType adapterType = JacksonUtil.findAdapterType(declaredElement, context.getContext());
if (adapterType != null) {
adapterType.getAdaptingType().accept(this, new Context(context.context, false, false, context.stack));
} else {
MapType mapType = MapType.findMapType(declaredType, context.getContext());
if (mapType != null) {
JsonType keyType = mapType.getKeyType().accept(this, new Context(context.getContext(), false, false, context.getStack()));
JsonType valueType = mapType.getValueType().accept(this, new Context(context.getContext(), false, false, context.getStack()));
jsonType = new JsonMapType(keyType, valueType);
} else {
TypeMirror componentType = getComponentType(decoratedTypeMirror, env);
if (componentType != null) {
return wrapAsNeeded(componentType.accept(this, new Context(context.context, false, true, context.stack)), context);
} else {
switch(declaredElement.getKind()) {
case ENUM:
case CLASS:
case INTERFACE:
JsonType knownType = context.getContext().getKnownType(declaredElement);
if (knownType != null) {
jsonType = knownType;
} else {
// type not known, not specified. Last chance: look for the type definition.
TypeDefinition typeDefinition = context.getContext().findTypeDefinition(declaredElement);
if (typeDefinition != null) {
jsonType = new JsonClassType(typeDefinition);
}
}
break;
}
}
}
}
if (jsonType == null) {
jsonType = super.visitDeclared(declaredType, context);
}
return wrapAsNeeded(jsonType, context);
} finally {
context.getStack().pop();
}
}
use of com.webcohesion.enunciate.javac.decorations.DecoratedProcessingEnvironment in project enunciate by stoicflame.
the class JsonTypeFactory method findSpecifiedType.
/**
* Find the specified type of the given adaptable element, if it exists.
*
* @param adaptable The adaptable element for which to find the specified type.
* @param context The context
* @return The specified JSON type, or null if it doesn't exist.
*/
public static JsonType findSpecifiedType(Adaptable adaptable, EnunciateJacksonContext context) {
JsonType jsonType = null;
if (adaptable instanceof Accessor) {
Accessor accessor = (Accessor) adaptable;
TypeHint typeHint = accessor.getAnnotation(TypeHint.class);
if (typeHint != null) {
TypeMirror hint = TypeHintUtils.getTypeHint(typeHint, context.getContext().getProcessingEnvironment(), null);
if (hint != null) {
return getJsonType(hint, context);
}
}
JsonFormat format = accessor.getAnnotation(JsonFormat.class);
if (format != null) {
switch(format.shape()) {
case ARRAY:
return KnownJsonType.ARRAY;
case BOOLEAN:
return KnownJsonType.BOOLEAN;
case NUMBER:
case NUMBER_FLOAT:
return KnownJsonType.NUMBER;
case NUMBER_INT:
return KnownJsonType.WHOLE_NUMBER;
case OBJECT:
return KnownJsonType.OBJECT;
case STRING:
case SCALAR:
return KnownJsonType.STRING;
case ANY:
default:
}
}
final JsonSerialize serializeInfo = accessor.getAnnotation(JsonSerialize.class);
if (serializeInfo != null) {
DecoratedProcessingEnvironment env = context.getContext().getProcessingEnvironment();
DecoratedTypeMirror using = Annotations.mirrorOf(new Callable<Class<?>>() {
@Override
public Class<?> call() throws Exception {
return serializeInfo.using();
}
}, env, JsonSerializer.None.class);
if (using != null) {
// we're using some custom serialization, so we just have to return a generic object.
return KnownJsonType.OBJECT;
} else {
DecoratedTypeMirror as = Annotations.mirrorOf(new Callable<Class<?>>() {
@Override
public Class<?> call() throws Exception {
return serializeInfo.as();
}
}, env, Void.class);
if (as != null) {
return getJsonType(as, context);
} else {
DecoratedTypeMirror contentAs = Annotations.mirrorOf(new Callable<Class<?>>() {
@Override
public Class<?> call() throws Exception {
return serializeInfo.contentAs();
}
}, env, Void.class);
DecoratedTypeMirror contentUsing = Annotations.mirrorOf(new Callable<Class<?>>() {
@Override
public Class<?> call() throws Exception {
return serializeInfo.contentUsing();
}
}, env, JsonSerializer.None.class);
DecoratedTypeMirror accessorType = (DecoratedTypeMirror) accessor.asType();
if (accessorType.isCollection() || accessorType.isArray() || accessorType.isStream()) {
if (contentUsing != null) {
// the json type has to be just a list of object.
return new JsonArrayType(KnownJsonType.OBJECT);
} else if (contentAs != null) {
return new JsonArrayType(getJsonType(contentAs, context));
}
} else {
MapType mapType = MapType.findMapType(accessorType, context);
if (mapType != null) {
DecoratedTypeMirror keyAs = Annotations.mirrorOf(new Callable<Class<?>>() {
@Override
public Class<?> call() throws Exception {
return serializeInfo.keyAs();
}
}, env, Void.class);
DecoratedTypeMirror keyUsing = Annotations.mirrorOf(new Callable<Class<?>>() {
@Override
public Class<?> call() throws Exception {
return serializeInfo.keyUsing();
}
}, env, JsonSerializer.None.class);
if (keyAs != null || contentAs != null) {
JsonType keyType = keyUsing == null ? getJsonType(keyAs == null ? (DecoratedTypeMirror) mapType.getKeyType() : keyAs, context) : KnownJsonType.OBJECT;
JsonType valueType = contentUsing == null ? getJsonType(contentAs == null ? (DecoratedTypeMirror) mapType.getValueType() : contentAs, context) : KnownJsonType.OBJECT;
return new JsonMapType(keyType, valueType);
}
}
}
}
}
}
}
if (adaptable.isAdapted()) {
jsonType = getJsonType(adaptable.getAdapterType().getAdaptingType(), context);
}
return jsonType;
}
use of com.webcohesion.enunciate.javac.decorations.DecoratedProcessingEnvironment in project enunciate by stoicflame.
the class JsonTypeVisitor method visitDeclared.
@Override
public JsonType visitDeclared(DeclaredType declaredType, Context context) {
JsonType jsonType = null;
Element declaredElement = declaredType.asElement();
DecoratedProcessingEnvironment env = context.getContext().getContext().getProcessingEnvironment();
DecoratedTypeMirror decoratedTypeMirror = (DecoratedTypeMirror) TypeMirrorDecorator.decorate(declaredType, env);
String fqn = declaredElement instanceof TypeElement ? ((TypeElement) declaredElement).getQualifiedName().toString() : declaredType.toString();
if (context.getStack().contains(fqn) && !decoratedTypeMirror.isCollection() && !decoratedTypeMirror.isStream()) {
// break out of recursive loop.
return wrapAsNeeded(KnownJsonType.OBJECT, context);
}
context.getStack().push(fqn);
try {
TypeHint typeHint = declaredElement.getAnnotation(TypeHint.class);
if (typeHint != null) {
TypeMirror hint = TypeHintUtils.getTypeHint(typeHint, context.getContext().getContext().getProcessingEnvironment(), null);
if (hint != null) {
jsonType = hint.accept(this, new Context(context.context, false, false, context.stack));
}
}
final JsonSerialize serializeInfo = declaredElement.getAnnotation(JsonSerialize.class);
if (serializeInfo != null) {
DecoratedTypeMirror using = Annotations.mirrorOf(new Callable<Class<?>>() {
@Override
public Class<?> call() throws Exception {
return serializeInfo.using();
}
}, env, JsonSerializer.None.class);
if (using != null) {
// custom serializer; just say it's an object.
jsonType = KnownJsonType.OBJECT;
}
DecoratedTypeMirror as = Annotations.mirrorOf(new Callable<Class<?>>() {
@Override
public Class<?> call() throws Exception {
return serializeInfo.as();
}
}, env, Void.class);
if (as != null) {
jsonType = (JsonType) as.accept(this, new Context(context.context, false, false, context.stack));
}
}
AdapterType adapterType = JacksonUtil.findAdapterType(declaredElement, context.getContext());
if (adapterType != null) {
adapterType.getAdaptingType().accept(this, new Context(context.context, false, false, context.stack));
} else {
MapType mapType = MapType.findMapType(declaredType, context.getContext());
if (mapType != null) {
JsonType keyType = mapType.getKeyType().accept(this, new Context(context.getContext(), false, false, context.stack));
JsonType valueType = mapType.getValueType().accept(this, new Context(context.getContext(), false, false, context.stack));
jsonType = new JsonMapType(keyType, valueType);
} else {
TypeMirror componentType = getComponentType(decoratedTypeMirror, env);
if (componentType != null) {
return wrapAsNeeded(componentType.accept(this, new Context(context.context, false, true, context.stack)), context);
} else {
switch(declaredElement.getKind()) {
case ENUM:
case CLASS:
case INTERFACE:
JsonType knownType = context.getContext().getKnownType(declaredElement);
if (knownType != null) {
jsonType = knownType;
} else {
// type not known, not specified. Last chance: look for the type definition.
TypeDefinition typeDefinition = context.getContext().findTypeDefinition(declaredElement);
if (typeDefinition != null) {
jsonType = new JsonClassType(typeDefinition);
}
}
break;
}
}
}
}
if (jsonType == null) {
jsonType = super.visitDeclared(declaredType, context);
}
return wrapAsNeeded(jsonType, context);
} finally {
context.getStack().pop();
}
}
use of com.webcohesion.enunciate.javac.decorations.DecoratedProcessingEnvironment in project enunciate by stoicflame.
the class JacksonUtil method findAdapterType.
private static AdapterType findAdapterType(DecoratedTypeMirror maybeContainedAdaptedType, Element referer, EnunciateJacksonContext context) {
DecoratedProcessingEnvironment env = context.getContext().getProcessingEnvironment();
TypeMirror adaptedType = TypeMirrorUtils.getComponentType(maybeContainedAdaptedType, env);
final boolean isContained = adaptedType != null;
adaptedType = isContained ? adaptedType : maybeContainedAdaptedType;
JsonSerialize serializationInfo = referer != null ? referer.getAnnotation(JsonSerialize.class) : null;
if (serializationInfo == null && adaptedType instanceof DeclaredType) {
serializationInfo = ((DeclaredType) adaptedType).asElement().getAnnotation(JsonSerialize.class);
}
if (serializationInfo != null) {
final JsonSerialize finalInfo = serializationInfo;
DecoratedTypeMirror adapterTypeMirror = Annotations.mirrorOf(new Callable<Class<?>>() {
@Override
public Class<?> call() throws Exception {
return isContained ? finalInfo.contentConverter() : finalInfo.converter();
}
}, env, Converter.None.class);
if (adapterTypeMirror instanceof DeclaredType) {
return new AdapterType((DeclaredType) adapterTypeMirror, context);
}
}
if (context.isHonorJaxb()) {
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) {
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);
if (!context.getContext().getProcessingEnvironment().getTypeUtils().isSameType(adapterType.getAdaptingType(), adaptedType)) {
return adapterType;
}
}
}
}
return null;
}
Aggregations