List of usage examples for com.fasterxml.jackson.databind JavaType getRawClass
public final Class<?> getRawClass()
From source file:org.broadleafcommerce.core.web.api.WrapperOverrideTypeModifier.java
@Override public JavaType modifyType(JavaType type, Type jdkType, TypeBindings context, TypeFactory typeFactory) { try {/*w w w .j av a 2s . c om*/ if (type.getClass().isAssignableFrom(SimpleType.class)) { Object overriddenBean = applicationContext.getBean(type.getRawClass().getName()); return SimpleType.construct(overriddenBean.getClass()); } } catch (NoSuchBeanDefinitionException e) { LOG.debug("No configured bean for " + type.getClass().getName() + " returning original type"); } return type; }
From source file:org.candlepin.swagger.CandlepinSwaggerModelConverter.java
public Property resolveProperty(Type type, ModelConverterContext context, Annotation[] annotations, Iterator<ModelConverter> next) { JavaType propType = null; /**//from w w w . ja v a2s. com * See java doc of NestedComplexType. This unwrapping makes sure that a * real type of field is passed to _mapper */ if (type instanceof NestedComplexType) { propType = pMapper.constructType(((NestedComplexType) type).getOriginalType()); } else { propType = pMapper.constructType(type); } log.debug("resolveProperty {}", propType); Property property = null; if (propType.isContainerType()) { JavaType keyType = propType.getKeyType(); JavaType valueType = propType.getContentType(); if (keyType != null && valueType != null) { property = new MapProperty() .additionalProperties(context.resolveProperty(valueType, new Annotation[] {})); } else if (valueType != null) { ArrayProperty arrayProperty = new ArrayProperty() .items(context.resolveProperty(valueType, new Annotation[] {})); if (pIsSetType(propType.getRawClass())) { arrayProperty.setUniqueItems(true); } property = arrayProperty; } } else { property = PrimitiveType.createProperty(propType); } if (property == null) { if (propType.isEnumType()) { property = new StringProperty(); pAddEnumProps(propType.getRawClass(), property); } else if (pIsOptionalType(propType)) { property = context.resolveProperty(propType.containedType(0), null); } else { // complex type Model innerModel = context.resolve(type); if (innerModel instanceof ModelImpl) { ModelImpl mi = (ModelImpl) innerModel; property = new RefProperty( StringUtils.isNotEmpty(mi.getReference()) ? mi.getReference() : mi.getName()); } } } return property; }
From source file:org.candlepin.swagger.CandlepinSwaggerModelConverter.java
private boolean pIsOptionalType(JavaType propType) { return Arrays.asList("com.google.common.base.Optional", "java.util.Optional") .contains(propType.getRawClass().getCanonicalName()); }
From source file:org.candlepin.swagger.CandlepinSwaggerModelConverter.java
private void parseProperty(ModelConverterContext context, boolean isNested, final BeanDescription beanDesc, Set<String> propertiesToIgnore, List<Property> props, BeanPropertyDefinition propDef) { Property property = null;//from w w w. j a va2 s. c om String propName = propDef.getName(); Annotation[] annotations = null; propName = getPropName(propDef, propName); PropertyMetadata md = propDef.getMetadata(); boolean hasSetter = false, hasGetter = false; if (propDef.getSetter() == null) { hasSetter = false; } else { hasSetter = true; } if (propDef.getGetter() != null) { JsonProperty pd = propDef.getGetter().getAnnotation(JsonProperty.class); if (pd != null) { hasGetter = true; } } Boolean isReadOnly = null; if (!hasSetter & hasGetter) { isReadOnly = Boolean.TRUE; } else { isReadOnly = Boolean.FALSE; } final AnnotatedMember member = propDef.getPrimaryMember(); if (member != null && !propertiesToIgnore.contains(propName) && /** * If the owning type is nested than we should include only those * fields that have the Hateoas annotation. */ !(isNested && !member.hasAnnotation(HateoasInclude.class))) { List<Annotation> annotationList = new ArrayList<Annotation>(); for (Annotation a : member.annotations()) { annotationList.add(a); } annotations = annotationList.toArray(new Annotation[annotationList.size()]); ApiModelProperty mp = member.getAnnotation(ApiModelProperty.class); if (mp != null && mp.readOnly()) { isReadOnly = mp.readOnly(); } Type nested = null; JavaType propType = member.getType(beanDesc.bindingsForBeanType()); JsonFilter jsonFilter = propType.getRawClass().getAnnotation(JsonFilter.class); /** * At this point the propType is a type of some nested field of the * type that is being processed. The condition checks if this * particular type should have Hateoas serialization enabled. In * other words, if we should create a new Nested* model. */ if (jsonFilter != null && (jsonFilter.value().equals("ConsumerFilter") || jsonFilter.value().equals("EntitlementFilter") || jsonFilter.value().equals("OwnerFilter") || jsonFilter.value().equals("GuestFilter"))) { if (!nestedJavaTypes.containsKey(propType)) { nestedJavaTypes.put(propType, new NestedComplexType(propType)); } nested = nestedJavaTypes.get(propType); } else { nested = propType; } // allow override of name from annotation if (mp != null && !mp.name().isEmpty()) { propName = mp.name(); } if (mp != null && !mp.dataType().isEmpty()) { property = resolveApiAnnotated(context, property, annotations, mp, propType); } // no property from override, construct from propType if (property == null) { if (mp != null && StringUtils.isNotEmpty(mp.reference())) { property = new RefProperty(mp.reference()); } else if (member.getAnnotation(JsonIdentityInfo.class) != null) { property = GeneratorWrapper.processJsonIdentity(propType, context, pMapper, member.getAnnotation(JsonIdentityInfo.class), member.getAnnotation(JsonIdentityReference.class)); } if (property == null) { property = context.resolveProperty(nested, annotations); } } if (property != null) { addMetadataToProperty(property, propName, md, isReadOnly, member, mp); applyBeanValidatorAnnotations(property, annotations); props.add(property); } } }
From source file:uk.gov.gchq.koryphe.serialisation.json.SimpleClassNameCache.java
/** * Gets the class name for a given id.//w ww. j av a 2s. c om * * @param id the simple class name or full class name. * @param baseType the base type of the ID - used to help resolve conflicting IDs * @return the full class name or the original id if one could not be found. * @throws IllegalArgumentException if there are multiple classes with the same id. */ public static String getClassName(final String id, final JavaType baseType) { String className = null; if (null != id && !id.contains(".")) { initialise(); // Remove the array brackets if required, these will be added again at the end. final boolean isArray = id.endsWith("[]"); String nonArrayId = isArray ? id.substring(0, id.length() - 2) : id; nonArrayId = StringUtils.capitalize(nonArrayId); final Set<Class> classesForId = getClassesFromId(nonArrayId); // If the class is unknown (not in the cache) then try the core packages if (null == classesForId || classesForId.isEmpty()) { for (final String corePackage : corePackages) { final String classNameTmp = corePackage + "." + nonArrayId; final Class<?> clazz = ReflectionUtil.getClassFromName(classNameTmp); if (null != clazz) { className = classNameTmp; addIdClasses(nonArrayId, Sets.newHashSet(clazz)); break; } } } else if (1 == classesForId.size()) { // There is exactly one class name for the given ID so we are // confident that is this the class required. className = classesForId.iterator().next().getName(); } else { // If the base type has been provided then attempt to use // it to resolve the conflicts if (null != baseType) { final Class<?> baseClass = baseType.getRawClass(); // First check the conflict cache Map<String, Class> idToClass = baseTypeToIdToClass.get(baseClass); if (null != idToClass) { final Class clazz = getClassFromId(nonArrayId, idToClass); if (null != clazz) { className = clazz.getName(); } } // If the conflict has not previously been seen, then try // and resolve it and add it to the cache. if (null == className) { Class<?> matchedClass = null; for (final Class classForId : classesForId) { if (baseClass.isAssignableFrom(classForId)) { if (null == matchedClass) { matchedClass = classForId; } else { matchedClass = null; break; } } } // If the conflict has been resolved then cache the result if (null != matchedClass) { className = matchedClass.getName(); if (null == idToClass) { idToClass = new ConcurrentHashMap<>(); baseTypeToIdToClass.put(baseClass, idToClass); } addIdClass(nonArrayId, matchedClass, idToClass); } } } // If the conflict cannot be resolved then explain to the user // that they need to provide the full class name. if (null == className) { final List<String> classOptions = classesForId.stream() .map(c -> isArray ? "[L" + c.getName() + ";" : c.getName()) .collect(Collectors.toList()); throw new IllegalArgumentException("Multiple " + nonArrayId + " classes exist. Please choose one of the following and specify the full class name: " + classOptions); } } // Add the array information if required. if (isArray && null != className) { className = "[L" + className + ";"; } } // If we couldn't find the class name, the just return the original value. if (null == className) { className = id; } return className; }