use of javax.persistence.Inheritance in project BroadleafCommerce by BroadleafCommerce.
the class SingleTableInheritanceClassTransformer method transform.
@Override
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
// Lambdas and anonymous methods in Java 8 do not have a class name defined and so no transformation should be done
if (className == null) {
return null;
}
if (infos.isEmpty()) {
return null;
}
String convertedClassName = className.replace('/', '.');
SingleTableInheritanceInfo key = new SingleTableInheritanceInfo();
key.setClassName(convertedClassName);
int pos = infos.indexOf(key);
if (pos >= 0) {
try {
if (LOG.isDebugEnabled()) {
LOG.debug("Converting " + convertedClassName + " to a SingleTable inheritance strategy.");
}
SingleTableInheritanceInfo myInfo = infos.get(pos);
ClassFile classFile = new ClassFile(new DataInputStream(new ByteArrayInputStream(classfileBuffer)));
ConstPool constantPool = classFile.getConstPool();
AnnotationsAttribute annotationsAttribute = new AnnotationsAttribute(constantPool, AnnotationsAttribute.visibleTag);
List<?> attributes = classFile.getAttributes();
Iterator<?> itr = attributes.iterator();
while (itr.hasNext()) {
Object object = itr.next();
if (AnnotationsAttribute.class.isAssignableFrom(object.getClass())) {
AnnotationsAttribute attr = (AnnotationsAttribute) object;
Annotation[] items = attr.getAnnotations();
for (Annotation annotation : items) {
String typeName = annotation.getTypeName();
if (!typeName.equals(Inheritance.class.getName())) {
annotationsAttribute.addAnnotation(annotation);
}
}
itr.remove();
}
}
Annotation inheritance = new Annotation(Inheritance.class.getName(), constantPool);
ClassPool pool = ClassPool.getDefault();
pool.importPackage("javax.persistence");
pool.importPackage("java.lang");
EnumMemberValue strategy = (EnumMemberValue) Annotation.createMemberValue(constantPool, pool.makeClass("InheritanceType"));
strategy.setType(InheritanceType.class.getName());
strategy.setValue(InheritanceType.SINGLE_TABLE.name());
inheritance.addMemberValue("strategy", strategy);
annotationsAttribute.addAnnotation(inheritance);
if (myInfo.getDiscriminatorName() != null) {
Annotation discriminator = new Annotation(DiscriminatorColumn.class.getName(), constantPool);
StringMemberValue name = new StringMemberValue(constantPool);
name.setValue(myInfo.getDiscriminatorName());
discriminator.addMemberValue("name", name);
EnumMemberValue discriminatorType = (EnumMemberValue) Annotation.createMemberValue(constantPool, pool.makeClass("DiscriminatorType"));
discriminatorType.setType(DiscriminatorType.class.getName());
discriminatorType.setValue(myInfo.getDiscriminatorType().name());
discriminator.addMemberValue("discriminatorType", discriminatorType);
IntegerMemberValue length = new IntegerMemberValue(constantPool);
length.setValue(myInfo.getDiscriminatorLength());
discriminator.addMemberValue("length", length);
annotationsAttribute.addAnnotation(discriminator);
}
classFile.addAttribute(annotationsAttribute);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream os = new DataOutputStream(bos);
classFile.write(os);
os.close();
return bos.toByteArray();
} catch (Exception ex) {
ex.printStackTrace();
throw new IllegalClassFormatException("Unable to convert " + convertedClassName + " to a SingleTable inheritance strategy: " + ex.getMessage());
}
} else {
return null;
}
}
use of javax.persistence.Inheritance in project simplejpa by appoxy.
the class AnnotationManager method putAnnotationInfo.
/**
* Gets all the annotation info for a particular class and puts it in our annotation info cache.
*
* @param c
* @return
*/
public AnnotationInfo putAnnotationInfo(Class c) {
{
Entity entity = (Entity) c.getAnnotation(Entity.class);
if (entity == null) {
throw new PersistenceException("Class not marked as an @Entity: " + c.getName());
}
}
AnnotationInfo ai = new AnnotationInfo();
ai.setClassAnnotations(c.getAnnotations());
ai.setMainClass(c);
Class superClass = c;
Class rootClass = null;
while ((superClass = superClass.getSuperclass()) != null) {
MappedSuperclass mappedSuperclass = (MappedSuperclass) superClass.getAnnotation(MappedSuperclass.class);
Entity entity = (Entity) superClass.getAnnotation(Entity.class);
Inheritance inheritance = (Inheritance) superClass.getAnnotation(Inheritance.class);
if (mappedSuperclass != null || entity != null) {
putProperties(ai, superClass);
putMethods(ai, superClass);
if (entity != null) {
rootClass = superClass;
}
putEntityListeners(ai, superClass);
}
}
if (rootClass != null) {
ai.setRootClass(rootClass);
DiscriminatorValue dv = (DiscriminatorValue) c.getAnnotation(DiscriminatorValue.class);
String discriminatorValue;
if (dv != null) {
discriminatorValue = dv.value();
if (discriminatorValue == null) {
throw new PersistenceException("You must specify a value= for @DiscriminatorValue on " + c.getName());
}
} else {
discriminatorValue = c.getSimpleName();
}
ai.setDiscriminatorValue(discriminatorValue);
discriminatorMap.put(discriminatorValue, ai);
} else {
ai.setRootClass(c);
}
putTableDeclaration(ai, c);
putProperties(ai, c);
putMethods(ai, c);
if (ai.getIdMethod() == null) {
throw new PersistenceException("No ID method specified for: " + c.getName());
}
putEntityListeners(ai, c);
getAnnotationMap().put(c.getName(), ai);
return ai;
}
use of javax.persistence.Inheritance in project hibernate-orm by hibernate.
the class InheritanceState method extractInheritanceType.
private void extractInheritanceType() {
XAnnotatedElement element = getClazz();
Inheritance inhAnn = element.getAnnotation(Inheritance.class);
MappedSuperclass mappedSuperClass = element.getAnnotation(MappedSuperclass.class);
if (mappedSuperClass != null) {
setEmbeddableSuperclass(true);
setType(inhAnn == null ? null : inhAnn.strategy());
} else {
setType(inhAnn == null ? InheritanceType.SINGLE_TABLE : inhAnn.strategy());
}
}
use of javax.persistence.Inheritance in project hibernate-orm by hibernate.
the class JPAOverriddenAnnotationReader method getInheritance.
private Inheritance getInheritance(Element tree, XMLContext.Default defaults) {
Element element = tree != null ? tree.element("inheritance") : null;
if (element != null) {
AnnotationDescriptor ad = new AnnotationDescriptor(Inheritance.class);
Attribute attr = element.attribute("strategy");
InheritanceType strategy = InheritanceType.SINGLE_TABLE;
if (attr != null) {
String value = attr.getValue();
if ("SINGLE_TABLE".equals(value)) {
strategy = InheritanceType.SINGLE_TABLE;
} else if ("JOINED".equals(value)) {
strategy = InheritanceType.JOINED;
} else if ("TABLE_PER_CLASS".equals(value)) {
strategy = InheritanceType.TABLE_PER_CLASS;
} else {
throw new AnnotationException("Unknown InheritanceType in XML: " + value + " (" + SCHEMA_VALIDATION + ")");
}
}
ad.setValue("strategy", strategy);
return AnnotationFactory.create(ad);
} else if (defaults.canUseJavaAnnotations()) {
return getPhysicalAnnotation(Inheritance.class);
} else {
return null;
}
}
use of javax.persistence.Inheritance in project cuba by cuba-platform.
the class MetadataImpl method getEntityNameForIdGeneration.
protected String getEntityNameForIdGeneration(MetaClass metaClass) {
Optional<MetaClass> optRoot = metaClass.getAncestors().stream().filter(// filter out all mapped superclasses
mc -> tools.isPersistent(mc)).findFirst();
if (optRoot.isPresent()) {
MetaClass root = optRoot.get();
Class<?> javaClass = root.getJavaClass();
Inheritance inheritance = javaClass.getAnnotation(Inheritance.class);
if (inheritance == null || inheritance.strategy() != InheritanceType.TABLE_PER_CLASS) {
// use root of inheritance tree if the strategy is JOINED or SINGLE_TABLE because ID is stored in the root table
return root.getName();
}
}
return metaClass.getName();
}
Aggregations