下面列出了org.hibernate.mapping.PersistentClass#getMappedClass ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@SuppressWarnings("unchecked")
private static EntityTypeImpl<?> buildEntityType(PersistentClass persistentClass, MetadataContext context) {
final Class javaType = persistentClass.getMappedClass();
context.pushEntityWorkedOn( persistentClass );
final MappedSuperclass superMappedSuperclass = persistentClass.getSuperMappedSuperclass();
AbstractIdentifiableType<?> superType = superMappedSuperclass == null
? null
: locateOrBuildMappedsuperclassType( superMappedSuperclass, context );
//no mappedSuperclass, check for a super entity
if ( superType == null ) {
final PersistentClass superPersistentClass = persistentClass.getSuperclass();
superType = superPersistentClass == null
? null
: locateOrBuildEntityType( superPersistentClass, context );
}
EntityTypeImpl entityType = new EntityTypeImpl(
javaType,
superType,
persistentClass
);
context.registerEntityType( persistentClass, entityType );
context.popEntityWorkedOn( persistentClass );
return entityType;
}
public PojoInstantiator(PersistentClass persistentClass, ReflectionOptimizer.InstantiationOptimizer optimizer) {
this.mappedClass = persistentClass.getMappedClass();
this.proxyInterface = persistentClass.getProxyInterface();
this.embeddedIdentifier = persistentClass.hasEmbeddedIdentifier();
this.optimizer = optimizer;
try {
constructor = ReflectHelper.getDefaultConstructor( mappedClass );
}
catch ( PropertyNotFoundException pnfe ) {
log.info(
"no default (no-argument) constructor for class: " +
mappedClass.getName() +
" (class must be instantiated by Interceptor)"
);
constructor = null;
}
}
/**
* Returns the {@link Interleaved} annotation on a table if it exists.
*/
public static Interleaved getInterleaveAnnotation(Table table, Metadata metadata) {
for (PersistentClass pc : metadata.getEntityBindings()) {
if (pc.getTable().equals(table) && pc.getMappedClass() != null) {
Class<?> entityClass = pc.getMappedClass();
return entityClass.getAnnotation(Interleaved.class);
}
}
return null;
}
public static ProxyDefinitions createFromMetadata(Metadata storeableMetadata, PreGeneratedProxies preGeneratedProxies) {
//Check upfront for any need across all metadata: would be nice to avoid initializing the Bytecode provider.
LazyBytecode lazyBytecode = new LazyBytecode();
if (needAnyProxyDefinitions(storeableMetadata)) {
final HashMap<Class<?>, ProxyClassDetailsHolder> proxyDefinitionMap = new HashMap<>();
try {
for (PersistentClass persistentClass : storeableMetadata.getEntityBindings()) {
if (needsProxyGeneration(persistentClass)) {
final Class mappedClass = persistentClass.getMappedClass();
final Class proxyClassDefinition = generateProxyClass(persistentClass, lazyBytecode,
preGeneratedProxies);
if (proxyClassDefinition == null) {
continue;
}
final boolean overridesEquals = ReflectHelper.overridesEquals(mappedClass);
try {
proxyDefinitionMap.put(mappedClass,
new ProxyClassDetailsHolder(overridesEquals, proxyClassDefinition.getConstructor()));
} catch (NoSuchMethodException e) {
throw new HibernateException(
"Failed to generate Enhanced Proxy: default constructor is missing for entity '"
+ mappedClass.getName() + "'. Please add a default constructor explicitly.");
}
}
}
} finally {
lazyBytecode.close();
}
return new ProxyDefinitions(proxyDefinitionMap);
} else {
return new ProxyDefinitions(Collections.emptyMap());
}
}
private PersistentClass getCommonPersistentClass(PersistentClass clazz1, PersistentClass clazz2) {
while ( clazz2 != null && clazz2.getMappedClass() != null && clazz1.getMappedClass() != null && !clazz2.getMappedClass()
.isAssignableFrom( clazz1.getMappedClass() ) ) {
clazz2 = clazz2.getSuperclass();
}
return clazz2;
}
public PojoEntityInstantiator(
EntityMetamodel entityMetamodel,
PersistentClass persistentClass,
ReflectionOptimizer.InstantiationOptimizer optimizer) {
super(
persistentClass.getMappedClass(),
optimizer,
persistentClass.hasEmbeddedIdentifier()
);
this.entityMetamodel = entityMetamodel;
this.proxyInterface = persistentClass.getProxyInterface();
this.applyBytecodeInterception = PersistentAttributeInterceptable.class.isAssignableFrom( persistentClass.getMappedClass() );
}
public static BytecodeEnhancementMetadata from(PersistentClass persistentClass) {
final Class mappedClass = persistentClass.getMappedClass();
final boolean enhancedForLazyLoading = PersistentAttributeInterceptable.class.isAssignableFrom( mappedClass );
final LazyAttributesMetadata lazyAttributesMetadata = enhancedForLazyLoading
? LazyAttributesMetadata.from( persistentClass )
: LazyAttributesMetadata.nonEnhanced( persistentClass.getEntityName() );
return new BytecodeEnhancementMetadataPojoImpl(
persistentClass.getEntityName(),
mappedClass,
enhancedForLazyLoading,
lazyAttributesMetadata
);
}
public PojoEntityTuplizer(EntityMetamodel entityMetamodel, PersistentClass mappedEntity) {
super( entityMetamodel, mappedEntity );
this.mappedClass = mappedEntity.getMappedClass();
this.proxyInterface = mappedEntity.getProxyInterface();
this.lifecycleImplementor = Lifecycle.class.isAssignableFrom( mappedClass );
this.isBytecodeEnhanced = entityMetamodel.getBytecodeEnhancementMetadata().isEnhancedForLazyLoading();
String[] getterNames = new String[propertySpan];
String[] setterNames = new String[propertySpan];
Class[] propTypes = new Class[propertySpan];
for ( int i = 0; i < propertySpan; i++ ) {
getterNames[i] = getters[i].getMethodName();
setterNames[i] = setters[i].getMethodName();
propTypes[i] = getters[i].getReturnType();
}
if ( hasCustomAccessors || !Environment.useReflectionOptimizer() ) {
optimizer = null;
}
else {
// todo : YUCK!!!
optimizer = Environment.getBytecodeProvider().getReflectionOptimizer(
mappedClass,
getterNames,
setterNames,
propTypes
);
// optimizer = getFactory().getSettings().getBytecodeProvider().getReflectionOptimizer(
// mappedClass, getterNames, setterNames, propTypes
// );
}
}
public PojoEntityTuplizer(EntityMetamodel entityMetamodel, PersistentClass mappedEntity) {
super( entityMetamodel, mappedEntity );
this.mappedClass = mappedEntity.getMappedClass();
this.proxyInterface = mappedEntity.getProxyInterface();
this.lifecycleImplementor = Lifecycle.class.isAssignableFrom( mappedClass );
this.validatableImplementor = Validatable.class.isAssignableFrom( mappedClass );
Iterator iter = mappedEntity.getPropertyClosureIterator();
while ( iter.hasNext() ) {
Property property = (Property) iter.next();
if ( property.isLazy() ) {
lazyPropertyNames.add( property.getName() );
}
}
String[] getterNames = new String[propertySpan];
String[] setterNames = new String[propertySpan];
Class[] propTypes = new Class[propertySpan];
for ( int i = 0; i < propertySpan; i++ ) {
getterNames[i] = getters[i].getMethodName();
setterNames[i] = setters[i].getMethodName();
propTypes[i] = getters[i].getReturnType();
}
if ( hasCustomAccessors || !Environment.useReflectionOptimizer() ) {
optimizer = null;
}
else {
// todo : YUCK!!!
optimizer = Environment.getBytecodeProvider().getReflectionOptimizer( mappedClass, getterNames, setterNames, propTypes );
// optimizer = getFactory().getSettings().getBytecodeProvider().getReflectionOptimizer(
// mappedClass, getterNames, setterNames, propTypes
// );
}
}
private static boolean needsProxyGeneration(PersistentClass persistentClass) {
//Only lazy entities need a proxy, and only class-mapped classed can be proxies (Envers!)
return persistentClass.isLazy() && (persistentClass.getMappedClass() != null);
}
private static Class<?> generateProxyClass(PersistentClass persistentClass,
Supplier<ByteBuddyProxyHelper> byteBuddyProxyHelper,
PreGeneratedProxies preGeneratedProxies) {
final String entityName = persistentClass.getEntityName();
final Class mappedClass = persistentClass.getMappedClass();
if ((mappedClass.getModifiers() & ACC_FINAL) == ACC_FINAL) {
LOGGER.warn("Could not generate an enhanced proxy for entity '" + entityName + "' (class='"
+ mappedClass.getCanonicalName()
+ "') as it's final. Your application might perform better if we're allowed to extend it.");
return null;
}
final Set<Class> proxyInterfaces = ProxyFactoryHelper.extractProxyInterfaces(persistentClass, entityName);
PreGeneratedProxies.ProxyClassDetailsHolder preProxy = preGeneratedProxies.getProxies()
.get(persistentClass.getClassName());
Class<?> preGeneratedProxy = null;
boolean match = true;
if (preProxy != null) {
match = proxyInterfaces.size() == preProxy.getProxyInterfaces().size();
if (match) {
for (Class i : proxyInterfaces) {
if (!preProxy.getProxyInterfaces().contains(i.getName())) {
match = false;
break;
}
}
}
if (match) {
try {
preGeneratedProxy = Class.forName(preProxy.getClassName(), false,
Thread.currentThread().getContextClassLoader());
} catch (ClassNotFoundException e) {
//should never happen
throw new RuntimeException("Unable to load proxy class", e);
}
}
}
if (preGeneratedProxy == null) {
if (match) {
LOGGER.warnf("Unable to find a build time generated proxy for entity %s",
persistentClass.getClassName());
} else {
//TODO: this should be changed to an exception after 1.4
//really it should be an exception now
LOGGER.errorf(
"Unable to use a build time generated proxy for entity %s, as the build time proxy " +
"interfaces %s are different to the runtime ones %s. This should not happen, please open an " +
"issue at https://github.com/quarkusio/quarkus/issues",
persistentClass.getClassName(), preProxy.getProxyInterfaces(), proxyInterfaces);
}
Class<?> proxyDef = byteBuddyProxyHelper.get().buildProxy(mappedClass, toArray(proxyInterfaces));
return proxyDef;
} else {
return preGeneratedProxy;
}
}
@Override
protected ProxyFactory buildProxyFactory(PersistentClass persistentClass, Getter idGetter, Setter idSetter) {
// determine the id getter and setter methods from the proxy interface (if any)
// determine all interfaces needed by the resulting proxy
/*
* We need to preserve the order of the interfaces they were put into the set, since javassist will choose the
* first one's class-loader to construct the proxy class with. This is also the reason why HibernateProxy.class
* should be the last one in the order (on JBossAS7 its class-loader will be org.hibernate module's class-
* loader, which will not see the classes inside deployed apps. See HHH-3078
*/
Set<Class> proxyInterfaces = new java.util.LinkedHashSet<Class>();
Class mappedClass = persistentClass.getMappedClass();
Class proxyInterface = persistentClass.getProxyInterface();
if ( proxyInterface != null && !mappedClass.equals( proxyInterface ) ) {
if ( !proxyInterface.isInterface() ) {
throw new MappingException(
"proxy must be either an interface, or the class itself: " + getEntityName()
);
}
proxyInterfaces.add( proxyInterface );
}
if ( mappedClass.isInterface() ) {
proxyInterfaces.add( mappedClass );
}
Iterator<Subclass> subclasses = persistentClass.getSubclassIterator();
while ( subclasses.hasNext() ) {
final Subclass subclass = subclasses.next();
final Class subclassProxy = subclass.getProxyInterface();
final Class subclassClass = subclass.getMappedClass();
if ( subclassProxy != null && !subclassClass.equals( subclassProxy ) ) {
if ( !subclassProxy.isInterface() ) {
throw new MappingException(
"proxy must be either an interface, or the class itself: " + subclass.getEntityName()
);
}
proxyInterfaces.add( subclassProxy );
}
}
proxyInterfaces.add( HibernateProxy.class );
Iterator properties = persistentClass.getPropertyIterator();
Class clazz = persistentClass.getMappedClass();
while ( properties.hasNext() ) {
Property property = (Property) properties.next();
Method method = property.getGetter( clazz ).getMethod();
if ( method != null && Modifier.isFinal( method.getModifiers() ) ) {
LOG.gettersOfLazyClassesCannotBeFinal( persistentClass.getEntityName(), property.getName() );
}
method = property.getSetter( clazz ).getMethod();
if ( method != null && Modifier.isFinal( method.getModifiers() ) ) {
LOG.settersOfLazyClassesCannotBeFinal( persistentClass.getEntityName(), property.getName() );
}
}
Method idGetterMethod = idGetter == null ? null : idGetter.getMethod();
Method idSetterMethod = idSetter == null ? null : idSetter.getMethod();
Method proxyGetIdentifierMethod = idGetterMethod == null || proxyInterface == null ?
null :
ReflectHelper.getMethod( proxyInterface, idGetterMethod );
Method proxySetIdentifierMethod = idSetterMethod == null || proxyInterface == null ?
null :
ReflectHelper.getMethod( proxyInterface, idSetterMethod );
ProxyFactory pf = buildProxyFactoryInternal( persistentClass, idGetter, idSetter );
try {
pf.postInstantiate(
getEntityName(),
mappedClass,
proxyInterfaces,
proxyGetIdentifierMethod,
proxySetIdentifierMethod,
persistentClass.hasEmbeddedIdentifier() ?
(CompositeType) persistentClass.getIdentifier().getType() :
null
);
}
catch (HibernateException he) {
LOG.unableToCreateProxyFactory( getEntityName(), he );
pf = null;
}
return pf;
}
protected ProxyFactory buildProxyFactory(PersistentClass persistentClass, Getter idGetter, Setter idSetter) {
// determine the id getter and setter methods from the proxy interface (if any)
// determine all interfaces needed by the resulting proxy
HashSet proxyInterfaces = new HashSet();
proxyInterfaces.add( HibernateProxy.class );
Class mappedClass = persistentClass.getMappedClass();
Class proxyInterface = persistentClass.getProxyInterface();
if ( proxyInterface!=null && !mappedClass.equals( proxyInterface ) ) {
if ( !proxyInterface.isInterface() ) {
throw new MappingException(
"proxy must be either an interface, or the class itself: " +
getEntityName()
);
}
proxyInterfaces.add( proxyInterface );
}
if ( mappedClass.isInterface() ) {
proxyInterfaces.add( mappedClass );
}
Iterator iter = persistentClass.getSubclassIterator();
while ( iter.hasNext() ) {
Subclass subclass = ( Subclass ) iter.next();
Class subclassProxy = subclass.getProxyInterface();
Class subclassClass = subclass.getMappedClass();
if ( subclassProxy!=null && !subclassClass.equals( subclassProxy ) ) {
if ( !proxyInterface.isInterface() ) {
throw new MappingException(
"proxy must be either an interface, or the class itself: " +
subclass.getEntityName()
);
}
proxyInterfaces.add( subclassProxy );
}
}
Iterator properties = persistentClass.getPropertyIterator();
Class clazz = persistentClass.getMappedClass();
while ( properties.hasNext() ) {
Property property = (Property) properties.next();
Method method = property.getGetter(clazz).getMethod();
if ( method != null && Modifier.isFinal( method.getModifiers() ) ) {
log.error(
"Getters of lazy classes cannot be final: " + persistentClass.getEntityName() +
"." + property.getName()
);
}
method = property.getSetter(clazz).getMethod();
if ( method != null && Modifier.isFinal( method.getModifiers() ) ) {
log.error(
"Setters of lazy classes cannot be final: " + persistentClass.getEntityName() +
"." + property.getName()
);
}
}
Method idGetterMethod = idGetter==null ? null : idGetter.getMethod();
Method idSetterMethod = idSetter==null ? null : idSetter.getMethod();
Method proxyGetIdentifierMethod = idGetterMethod==null || proxyInterface==null ?
null :
ReflectHelper.getMethod(proxyInterface, idGetterMethod);
Method proxySetIdentifierMethod = idSetterMethod==null || proxyInterface==null ?
null :
ReflectHelper.getMethod(proxyInterface, idSetterMethod);
ProxyFactory pf = buildProxyFactoryInternal( persistentClass, idGetter, idSetter );
try {
pf.postInstantiate(
getEntityName(),
mappedClass,
proxyInterfaces,
proxyGetIdentifierMethod,
proxySetIdentifierMethod,
persistentClass.hasEmbeddedIdentifier() ?
(AbstractComponentType) persistentClass.getIdentifier().getType() :
null
);
}
catch ( HibernateException he ) {
log.warn( "could not create proxy factory for:" + getEntityName(), he );
pf = null;
}
return pf;
}