Search in sources :

Example 1 with Inheritance

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;
    }
}
Also used : EnumMemberValue(javassist.bytecode.annotation.EnumMemberValue) ConstPool(javassist.bytecode.ConstPool) StringMemberValue(javassist.bytecode.annotation.StringMemberValue) DataOutputStream(java.io.DataOutputStream) Inheritance(javax.persistence.Inheritance) ClassPool(javassist.ClassPool) InheritanceType(javax.persistence.InheritanceType) IntegerMemberValue(javassist.bytecode.annotation.IntegerMemberValue) IllegalClassFormatException(java.lang.instrument.IllegalClassFormatException) ClassFile(javassist.bytecode.ClassFile) AnnotationsAttribute(javassist.bytecode.AnnotationsAttribute) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream) Annotation(javassist.bytecode.annotation.Annotation) IllegalClassFormatException(java.lang.instrument.IllegalClassFormatException) DiscriminatorType(javax.persistence.DiscriminatorType) ByteArrayInputStream(java.io.ByteArrayInputStream) DiscriminatorColumn(javax.persistence.DiscriminatorColumn)

Example 2 with Inheritance

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;
}
Also used : Entity(javax.persistence.Entity) MappedSuperclass(javax.persistence.MappedSuperclass) Inheritance(javax.persistence.Inheritance) PersistenceException(javax.persistence.PersistenceException) DiscriminatorValue(javax.persistence.DiscriminatorValue)

Example 3 with Inheritance

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());
    }
}
Also used : Inheritance(javax.persistence.Inheritance) MappedSuperclass(javax.persistence.MappedSuperclass) XAnnotatedElement(org.hibernate.annotations.common.reflection.XAnnotatedElement)

Example 4 with Inheritance

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;
    }
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) Attribute(org.dom4j.Attribute) AnnotatedElement(java.lang.reflect.AnnotatedElement) Element(org.dom4j.Element) Inheritance(javax.persistence.Inheritance) AnnotationException(org.hibernate.AnnotationException) InheritanceType(javax.persistence.InheritanceType)

Example 5 with Inheritance

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();
}
Also used : EmbeddedParameters(com.haulmont.cuba.core.entity.annotation.EmbeddedParameters) java.util(java.util) LoggerFactory(org.slf4j.LoggerFactory) DatatypeRegistry(com.haulmont.chile.core.datatypes.DatatypeRegistry) MetaClass(com.haulmont.chile.core.model.MetaClass) com.haulmont.cuba.core.global(com.haulmont.cuba.core.global) Inject(javax.inject.Inject) com.haulmont.cuba.core.entity(com.haulmont.cuba.core.entity) StackTrace(com.haulmont.bali.util.StackTrace) Method(java.lang.reflect.Method) Nullable(javax.annotation.Nullable) Order(org.springframework.core.annotation.Order) InheritanceType(javax.persistence.InheritanceType) SessionImpl(com.haulmont.chile.core.model.impl.SessionImpl) Logger(org.slf4j.Logger) MetaProperty(com.haulmont.chile.core.model.MetaProperty) EventListener(org.springframework.context.event.EventListener) MetaModel(com.haulmont.chile.core.model.MetaModel) ApplicationContext(org.springframework.context.ApplicationContext) InvocationTargetException(java.lang.reflect.InvocationTargetException) AppContextInitializedEvent(com.haulmont.cuba.core.sys.events.AppContextInitializedEvent) Component(org.springframework.stereotype.Component) Inheritance(javax.persistence.Inheritance) PostConstruct(javax.annotation.PostConstruct) Session(com.haulmont.chile.core.model.Session) MetaClass(com.haulmont.chile.core.model.MetaClass) Inheritance(javax.persistence.Inheritance)

Aggregations

Inheritance (javax.persistence.Inheritance)5 InheritanceType (javax.persistence.InheritanceType)3 MappedSuperclass (javax.persistence.MappedSuperclass)2 StackTrace (com.haulmont.bali.util.StackTrace)1 DatatypeRegistry (com.haulmont.chile.core.datatypes.DatatypeRegistry)1 MetaClass (com.haulmont.chile.core.model.MetaClass)1 MetaModel (com.haulmont.chile.core.model.MetaModel)1 MetaProperty (com.haulmont.chile.core.model.MetaProperty)1 Session (com.haulmont.chile.core.model.Session)1 SessionImpl (com.haulmont.chile.core.model.impl.SessionImpl)1 com.haulmont.cuba.core.entity (com.haulmont.cuba.core.entity)1 EmbeddedParameters (com.haulmont.cuba.core.entity.annotation.EmbeddedParameters)1 com.haulmont.cuba.core.global (com.haulmont.cuba.core.global)1 AppContextInitializedEvent (com.haulmont.cuba.core.sys.events.AppContextInitializedEvent)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataInputStream (java.io.DataInputStream)1 DataOutputStream (java.io.DataOutputStream)1 IllegalClassFormatException (java.lang.instrument.IllegalClassFormatException)1 AnnotatedElement (java.lang.reflect.AnnotatedElement)1