Java Code Examples for javax.persistence.AttributeConverter

The following examples show how to use javax.persistence.AttributeConverter. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
private AutoApplicableConverterDescriptor resolveAutoApplicableDescriptor(
		Class<? extends AttributeConverter> converterClass,
		Boolean forceAutoApply) {
	final boolean autoApply;

	if ( forceAutoApply != null ) {
		// if the caller explicitly specified whether to auto-apply, honor that
		autoApply = forceAutoApply;
	}
	else {
		// otherwise, look at the converter's @Converter annotation
		final Converter annotation = converterClass.getAnnotation( Converter.class );
		autoApply = annotation != null && annotation.autoApply();
	}

	return autoApply
			? new AutoApplicableConverterDescriptorStandardImpl( this )
			: AutoApplicableConverterDescriptorBypassedImpl.INSTANCE;
}
 
Example 2
Source Project: lams   Source File: MetadataBuilderImpl.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public MetadataBuilder applyAttributeConverter(Class<? extends AttributeConverter> attributeConverterClass) {
	this.bootstrapContext.addAttributeConverterInfo(
			new AttributeConverterInfo() {
				@Override
				public Class<? extends AttributeConverter> getConverterClass() {
					return attributeConverterClass;
				}

				@Override
				public ConverterDescriptor toConverterDescriptor(MetadataBuildingContext context) {
					return new ClassBasedConverterDescriptor(
							attributeConverterClass,
							null,
							context.getBootstrapContext().getClassmateContext()
					);
				}
			}
	);
	return this;
}
 
Example 3
Source Project: lams   Source File: MetadataBuilderImpl.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public MetadataBuilder applyAttributeConverter(Class<? extends AttributeConverter> attributeConverterClass, boolean autoApply) {
	this.bootstrapContext.addAttributeConverterInfo(
			new AttributeConverterInfo() {
				@Override
				public Class<? extends AttributeConverter> getConverterClass() {
					return attributeConverterClass;
				}

				@Override
				public ConverterDescriptor toConverterDescriptor(MetadataBuildingContext context) {
					return new ClassBasedConverterDescriptor(
							attributeConverterClass,
							autoApply,
							context.getBootstrapContext().getClassmateContext()
					);
				}
			}
	);
	return this;
}
 
Example 4
Source Project: warpdb   Source File: AccessibleProperty.java    License: Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private AttributeConverter<Object, Object> getConverter(AccessibleObject accessible) {
	Convert converter = accessible.getAnnotation(Convert.class);
	if (converter != null) {
		Class<?> converterClass = converter.converter();
		if (!AttributeConverter.class.isAssignableFrom(converterClass)) {
			throw new RuntimeException(
					"Converter class must be AttributeConverter rather than " + converterClass.getName());
		}
		try {
			Constructor<?> cs = converterClass.getDeclaredConstructor();
			cs.setAccessible(true);
			return (AttributeConverter<Object, Object>) cs.newInstance();
		} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | SecurityException
				| InvocationTargetException e) {
			throw new RuntimeException("Cannot instantiate Converter: " + converterClass.getName(), e);
		}
	}
	return null;
}
 
Example 5
Source Project: warpdb   Source File: AccessibleProperty.java    License: Apache License 2.0 6 votes vote down vote up
private static Class<?> getConverterType(AttributeConverter<Object, Object> converter) {
	if (converter != null) {
		List<Type> types = ClassUtils.getGenericInterfacesIncludeHierarchy(converter.getClass());
		for (Type type : types) {
			if (type instanceof ParameterizedType) {
				ParameterizedType pt = (ParameterizedType) type;
				if (pt.getRawType() == AttributeConverter.class) {
					Type dbType = pt.getActualTypeArguments()[1];
					if (dbType instanceof Class) {
						return (Class<?>) dbType;
					}
				}
			}

		}
	}
	return null;
}
 
Example 6
Source Project: warpdb   Source File: Where.java    License: Apache License 2.0 6 votes vote down vote up
Where<T> append(String type, String clause, Object... params) {
	// check clause:
	Mapper<T> mapper = this.criteria.mapper;
	CompiledClause cc = CompiledClause.compile(mapper, clause);
	if (cc.converters.length != params.length) {
		throw new IllegalArgumentException("Arguments not match the placeholder.");
	}
	// convert params:
	int n = 0;
	for (AttributeConverter<Object, Object> converter : cc.converters) {
		if (converter != null) {
			params[n] = converter.convertToDatabaseColumn(params[n]);
		}
		n++;
	}
	// add:
	if (type != null) {
		this.criteria.where.add(type);
	}
	this.criteria.where.add(cc.clause);
	for (Object param : params) {
		this.criteria.whereParams.add(param);
	}
	return this;
}
 
Example 7
Source Project: jeddict   Source File: DBEntityMappings.java    License: Apache License 2.0 6 votes vote down vote up
private void createConverterClass(Converter convert, ClassLoader classLoader) {
        //create Java Class
//        Class<?> attributeConverter = new ByteBuddy()
////                .subclass(TypeDescription.Generic.Builder.parameterizedType(AttributeConverter.class, String.class, Integer.class).build())
//                .subclass(AttributeConverter.class)
//                .name(convert.getClazz())
//                .annotateType(AnnotationDescription.Builder.ofType(javax.persistence.Converter.class).build())
//                .make()
//                .load(classLoader, ClassLoadingStrategy.Default.INJECTION)
//                .getLoaded();

        //create MetadataClass
        MetadataClass metadataClass = new MetadataClass(getMetadataFactory(), convert.getClazz());
        metadataClass.addInterface(AttributeConverter.class.getName());
        metadataClass.addGenericType("");
        metadataClass.addGenericType("");
        metadataClass.addGenericType(convert.getAttributeType());
        metadataClass.addGenericType("");
        metadataClass.addGenericType(convert.getFieldType());
        getMetadataFactory().addMetadataClass(metadataClass);

    }
 
Example 8
Source Project: mPaaS   Source File: ExtraMappingMetadataContributor.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 处理枚举类型的转换器
 */
private void handleEnumType(PersistentClass pclazz, SimpleValue value) {
	// 自动装载枚举转换
	if (!"org.hibernate.type.EnumType".equals(value.getTypeName())) {
		return;
	}
	if (value.getTypeParameters() == null) {
		return;
	}
	String returnedClass = value.getTypeParameters().getProperty(
			"org.hibernate.type.ParameterType.returnedClass");
	if (StringUtils.isBlank(returnedClass)) {
		return;
	}

	Class<?> enumClass = ReflectUtil.classForName(returnedClass);
	Class<?>[] inners = enumClass.getClasses();
	if (inners == null) {
		return;
	}
	for (Class<?> inner : inners) {
		if (!AttributeConverter.class.isAssignableFrom(inner)) {
			continue;
		}
		String propertyName = value.getTypeParameters().getProperty(
				"org.hibernate.type.ParameterType.propertyName");
		value.setTypeName(StringHelper.join(
				AttributeConverterTypeAdapter.NAME_PREFIX,
				inner.getName()));
		value.setTypeName(null);
		value.setTypeParameters(null);
		value.setTypeUsingReflection(pclazz.getClassName(), propertyName);
		return;
	}
}
 
Example 9
Source Project: lams   Source File: AttributeConversionInfo.java    License: GNU General Public License v2.0 5 votes vote down vote up
public AttributeConversionInfo(
		Class<? extends AttributeConverter> converterClass,
		boolean conversionDisabled,
		String attributeName,
		XAnnotatedElement source) {
	this.converterClass = converterClass;
	this.conversionDisabled = conversionDisabled;
	this.attributeName = attributeName;
	this.source = source;
}
 
Example 10
public InstanceBasedConverterDescriptor(
		AttributeConverter converterInstance,
		Boolean forceAutoApply,
		ClassmateContext classmateContext) {
	super( converterInstance.getClass(), forceAutoApply, classmateContext );
	this.converterInstance = converterInstance;
}
 
Example 11
@SuppressWarnings("WeakerAccess")
public AbstractConverterDescriptor(
		Class<? extends AttributeConverter> converterClass,
		Boolean forceAutoApply,
		ClassmateContext classmateContext) {
	this.converterClass = converterClass;

	final ResolvedType converterType = classmateContext.getTypeResolver().resolve( converterClass );
	final List<ResolvedType> converterParamTypes = converterType.typeParametersFor( AttributeConverter.class );
	if ( converterParamTypes == null ) {
		throw new AnnotationException(
				"Could not extract type parameter information from AttributeConverter implementation ["
						+ converterClass.getName() + "]"
		);
	}
	else if ( converterParamTypes.size() != 2 ) {
		throw new AnnotationException(
				"Unexpected type parameter information for AttributeConverter implementation [" +
						converterClass.getName() + "]; expected 2 parameter types, but found " + converterParamTypes.size()
		);
	}

	this.domainType = converterParamTypes.get( 0 );
	this.jdbcType = converterParamTypes.get( 1 );

	this.autoApplicableDescriptor = resolveAutoApplicableDescriptor( converterClass, forceAutoApply );
}
 
Example 12
Source Project: warpdb   Source File: AccessibleProperty.java    License: Apache License 2.0 5 votes vote down vote up
private static Class<?> checkPropertyType(Class<?> typeClass, AttributeConverter<Object, Object> converter) {
	Class<?> converterType = getConverterType(converter);
	if (converterType != null) {
		typeClass = converterType;
	}
	if (typeClass.isEnum() || DEFAULT_COLUMN_TYPES.containsKey(typeClass)) {
		return typeClass;
	}
	throw new RuntimeException("Unsupported type: " + typeClass);
}
 
Example 13
Source Project: warpdb   Source File: Where.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
static CompiledClause doCompile(Mapper<?> mapper, String clause) {
	Map<String, AccessibleProperty> properties = mapper.allPropertiesMap;
	StringBuilder sb = new StringBuilder(clause.length() + 10);
	List<AttributeConverter<Object, Object>> list = new ArrayList<>();
	int start = 0;
	Matcher m = p.matcher(clause.toLowerCase());
	while (m.find()) {
		sb.append(clause.substring(start, m.start()));
		String s = clause.substring(m.start(), m.end());
		if (properties.containsKey(s.toLowerCase())) {
			AccessibleProperty ap = properties.get(s.toLowerCase());
			sb.append(ap.columnName);
			list.add(ap.converter);
		} else {
			if (s.toLowerCase().equals("between")) {
				list.add(list.get(list.size() - 1));
			} else if (s.toLowerCase().equals("null")) {
				list.remove(list.size() - 1);
			} else {
				if (!KEYWORDS.contains(s.toLowerCase())) {
					throw new IllegalArgumentException("Invalid string \"" + s + "\" found in clause: " + clause);
				}
			}
			sb.append(s);
		}
		start = m.end();
	}
	sb.append(clause.substring(start));
	if (list.size() != numOfPlaceholder(clause)) {
		throw new IllegalArgumentException("Invalid number of placeholder.");
	}
	return new CompiledClause(sb.toString(), list.toArray(new AttributeConverter[0]));
}
 
Example 14
@Override
public AttributeConverter<ClusterManagerVariant, String> getVictim() {
    return new ClusterManagerVariantConverter();
}
 
Example 15
Source Project: cloudbreak   Source File: InstanceGroupTypeConverterTest.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public AttributeConverter<InstanceGroupType, String> getVictim() {
    return new InstanceGroupTypeConverter();
}
 
Example 16
@Override
public AttributeConverter<CloudStorageValidation, String> getVictim() {
    return new CloudStorageValidationConverter();
}
 
Example 17
@SuppressWarnings("unchecked")
public static void registerConverter(Configuration config, Class<?> converterClass) {
	config.addAttributeConverter((Class<? extends AttributeConverter<?, ?>>) converterClass);
}
 
Example 18
Source Project: lams   Source File: AttributeConversionInfo.java    License: GNU General Public License v2.0 4 votes vote down vote up
public Class<? extends AttributeConverter> getConverterClass() {
	return converterClass;
}
 
Example 19
Source Project: cloudbreak   Source File: InstanceGroupTypeConverterTest.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public AttributeConverter<InstanceGroupType, String> getVictim() {
    return new InstanceGroupTypeConverter();
}
 
Example 20
@Override
public AttributeConverter<InstanceMetadataType, String> getVictim() {
    return new InstanceMetadataTypeConverter();
}
 
Example 21
Source Project: cloudbreak   Source File: AlertStateConverterTest.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public AttributeConverter<AlertState, String> getVictim() {
    return new AlertStateConverter();
}
 
Example 22
Source Project: cloudbreak   Source File: AlertTypeConverterTest.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public AttributeConverter<AlertType, String> getVictim() {
    return new AlertTypeConverter();
}
 
Example 23
@Override
public AttributeConverter<ClusterCommandType, String> getVictim() {
    return new ClusterCommandTypeConverter();
}
 
Example 24
public AttributeConverter getAttributeConverter() {
	return attributeConverter;
}
 
Example 25
Source Project: cloudbreak   Source File: AdjustmentTypeConverterTest.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public AttributeConverter<AdjustmentType, String> getVictim() {
    return new AdjustmentTypeConverter();
}
 
Example 26
Source Project: cloudbreak   Source File: ExecutorTypeConverterTest.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public AttributeConverter<ExecutorType, String> getVictim() {
    return new ExecutorTypeConverter();
}
 
Example 27
Source Project: cloudbreak   Source File: EnvironmentStatusConverterTest.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public AttributeConverter<EnvironmentStatus, String> getVictim() {
    return new EnvironmentStatusConverter();
}
 
Example 28
@Override
public AttributeConverter<ResourceGroupUsagePattern, String> getVictim() {
    return new ResourceGroupUsagePatternConverter();
}
 
Example 29
@Override
public ManagedBean<AttributeConverter<O, R>> getConverterBean() {
	return attributeConverterBean;
}
 
Example 30
@Override
public JavaTypeDescriptor<AttributeConverter<O, R>> getConverterJavaTypeDescriptor() {
	return converterJavaTypeDescriptor;
}