entityintrospector.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 843 行 · 第 1/2 页

JAVA
843
字号
/* * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Resin Open Source is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty * of NON-INFRINGEMENT.  See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the * *   Free Software Foundation, Inc. *   59 Temple Place, Suite 330 *   Boston, MA 02111-1307  USA * * @author Scott Ferguson */package com.caucho.amber.cfg;import com.caucho.amber.AmberTableCache;import com.caucho.amber.manager.AmberPersistenceUnit;import com.caucho.amber.table.AmberTable;import com.caucho.amber.type.*;import com.caucho.amber.field.SubId;import com.caucho.config.ConfigException;import com.caucho.config.types.Period;import com.caucho.util.L10N;import java.lang.annotation.Annotation;import java.lang.reflect.Method;import java.lang.reflect.Modifier;import javax.persistence.InheritanceType;import javax.persistence.AttributeOverrides;import javax.persistence.DiscriminatorType;import javax.persistence.DiscriminatorValue;import java.sql.SQLException;import java.util.ArrayList;import java.util.HashMap;import java.util.logging.Logger;import javax.persistence.AttributeOverride;import javax.persistence.Basic;import javax.persistence.Column;import javax.persistence.DiscriminatorColumn;import javax.persistence.Embeddable;import javax.persistence.Entity;import javax.persistence.IdClass;import javax.persistence.Inheritance;import javax.persistence.JoinColumn;import javax.persistence.MappedSuperclass;import javax.persistence.PrimaryKeyJoinColumn;import javax.persistence.SecondaryTable;import javax.persistence.Table;/** * Configuration for an entity bean */public class EntityIntrospector extends BaseConfigIntrospector {  private static final L10N L = new L10N(EntityIntrospector.class);  private static final Logger log    = Logger.getLogger(EntityIntrospector.class.getName());  /**   * Creates the introspector.   */  EntityIntrospector(AmberConfigManager manager)  {    super(manager);  }  /**   * Returns true for entity type.   */  public boolean isEntity(Class type)  {    getInternalEntityConfig(type, _annotationCfg);     return (! _annotationCfg.isNull());  }  /**   * Introspects.   */  public BeanType introspect(Class type)    throws ConfigException, SQLException  {    BeanType beanType = null;        try {      EntityType entityType = null;      EntityType parentType = introspectParent(type.getSuperclass());      entityType = introspectEntityType(type, parentType);      if (entityType == null)	entityType = introspectMappedType(type, parentType);      if (entityType == null)	return introspectEmbeddableType(type);            beanType = entityType;            // jpa/0ge2      entityType.setInstanceClassName(type.getName() + "__ResinExt");      entityType.setEnhanced(true);      MappedSuperclassConfig mappedSuperOrEntityConfig	= introspectEntityConfig(type);				       entityType.setParentType(parentType);      introspectTable(type, entityType, parentType);      // inheritance must be after table since it adds columns      introspectInheritance(type, entityType, parentType);            introspectTableCache(entityType, type);      getInternalIdClassConfig(type, _annotationCfg);      IdClass idClassAnn = (IdClass) _annotationCfg.getAnnotation();      IdClassConfig idClassConfig = _annotationCfg.getIdClassConfig();      Class idClass = null;      if (! _annotationCfg.isNull()) {        if (idClassAnn != null)          idClass = idClassAnn.value();        else {          String s = idClassConfig.getClassName();          idClass = _persistenceUnit.loadTempClass(s);        }        // XXX: temp. introspects idClass as an embeddable type.	_persistenceUnit.addEntityClass(idClass.getName(), idClass);        // jpa/0i49 vs jpa/0i40        // embeddable.setFieldAccess(isField);      }      if (entityType.getId() != null) {      }      else if (entityType.isFieldAccess())        introspectIdField(_persistenceUnit, entityType, parentType,                          type, idClass, mappedSuperOrEntityConfig);      else {        introspectIdMethod(_persistenceUnit, entityType, parentType,                           type, idClass, mappedSuperOrEntityConfig);      }      HashMap<String, IdConfig> idMap = null;      AttributesConfig attributes = null;      if (mappedSuperOrEntityConfig != null) {        attributes = mappedSuperOrEntityConfig.getAttributes();        if (attributes != null)          idMap = attributes.getIdMap();      }      // if ((idMap == null) || (idMap.size() == 0)) {      //   idMap = entityType.getSuperClass();      // }      if (entityType.isEntity() && (entityType.getId() == null) && ((idMap == null) || (idMap.size() == 0)))        throw new ConfigException(L.l("{0} does not have any primary keys.  Entities must have at least one @Id or exactly one @EmbeddedId field.",                                      entityType.getName()));      // Introspect overridden attributes. (jpa/0ge2)      introspectAttributeOverrides(entityType, type);      introspectSecondaryTable(entityType, type);      if (entityType.isFieldAccess())        introspectFields(_persistenceUnit, entityType, parentType, type,                         mappedSuperOrEntityConfig, false);      else        introspectMethods(_persistenceUnit, entityType, parentType, type,                          mappedSuperOrEntityConfig);      introspectCallbacks(type, entityType);            // Adds entity listeners, if any.      introspectEntityListeners(type, entityType, _persistenceUnit);      // Adds sql result set mappings, if any.      introspectSqlResultSetMappings(type, entityType, entityType.getName());      // Adds named queries, if any.      introspectNamedQueries(type, entityType.getName());      introspectNamedNativeQueries(type, entityType.getName());    } catch (ConfigException e) {      if (beanType != null)        beanType.setConfigException(e);      throw e;    } catch (SQLException e) {      if (beanType != null)        beanType.setConfigException(e);      throw e;    } catch (RuntimeException e) {      if (beanType != null)        beanType.setConfigException(e);      throw e;    }    return beanType;  }  private EntityType introspectEntityType(Class type,					  EntityType parentType)    throws SQLException  {    getInternalEntityConfig(type, _annotationCfg);          Entity entityAnn = (Entity) _annotationCfg.getAnnotation();    EntityConfig entityConfig = _annotationCfg.getEntityConfig();    boolean isEntity = ! _annotationCfg.isNull();    if (! isEntity)      return null;        EntityType entityType;    String typeName;        if (entityConfig != null)      typeName = entityConfig.getClassName();    else      typeName = entityAnn.name();    // Validates the type    String entityName;    Inheritance inheritanceAnn = null;    InheritanceConfig inheritanceConfig = null;    Class rootClass = type;    Entity rootEntityAnn = null;    EntityConfig rootEntityConfig = null;    validateType(type, true);      // jpa/0ge2      // if (hasInheritance) {    if (entityConfig == null)      entityName = entityAnn.name();    else {      entityName = entityConfig.getClassName();      int p = entityName.lastIndexOf('.');      if (p > 0)	entityName = entityName.substring(p + 1);    }    if ((entityName == null) || "".equals(entityName)) {      entityName = type.getSimpleName();    }    entityType = _persistenceUnit.createEntity(entityName, type);    _configManager.addType(type, new EntityConfig(type.getName(), this, entityType));    boolean isField = isField(type, entityConfig, false);    if (isField)      entityType.setFieldAccess(true);    return entityType;  }  private EntityType introspectMappedType(Class type, EntityType parentType)    throws SQLException  {    EntityType entityType;    boolean isMappedSuperclass = false;    MappedSuperclass mappedSuperAnn = null;    MappedSuperclassConfig mappedSuperConfig = null;    String typeName;    MappedSuperclassConfig mappedSuperOrEntityConfig = null;    getInternalMappedSuperclassConfig(type, _annotationCfg);    mappedSuperAnn = (MappedSuperclass) _annotationCfg.getAnnotation();    mappedSuperConfig = _annotationCfg.getMappedSuperclassConfig();    isMappedSuperclass = ! _annotationCfg.isNull();    if (! isMappedSuperclass)      return null;        mappedSuperOrEntityConfig = mappedSuperConfig;    if (mappedSuperConfig != null)      typeName = mappedSuperConfig.getClassName();/*    else      typeName = mappedSuperAnn.name();*/    // Validates the type    String entityName;    Inheritance inheritanceAnn = null;    InheritanceConfig inheritanceConfig = null;    Class rootClass = type;    Entity rootEntityAnn = null;    EntityConfig rootEntityConfig = null;    validateType(type, false);    // jpa/0ge2    // if (hasInheritance) {    if (mappedSuperConfig == null)      entityName = null;//mappedSuperAnn.name();    else {      entityName = mappedSuperConfig.getSimpleClassName();    }    if ((entityName == null) || "".equals(entityName)) {      entityName = type.getSimpleName();    }    entityType = _persistenceUnit.createMappedSuperclass(entityName, type);    _configManager.addType(type, new EntityConfig(type.getName(), this, entityType));    boolean isField = isField(type, mappedSuperOrEntityConfig, false);    if (isField)      entityType.setFieldAccess(true);    // jpa/0ge2    entityType.setInstanceClassName(type.getName() + "__ResinExt");    entityType.setEnhanced(true);    return entityType;  }  /**   * Introspects.   */  private EmbeddableType introspectEmbeddableType(Class type)    throws ConfigException, SQLException  {    getInternalEmbeddableConfig(type, _annotationCfg);    Embeddable embeddableAnn = (Embeddable) _annotationCfg.getAnnotation();    EmbeddableConfig embeddableConfig = _annotationCfg.getEmbeddableConfig();    /*    if (_annotationCfg.isNull())      return null;    */    String typeName = type.getName();    EmbeddableType embeddableType      = _persistenceUnit.createEmbeddable(typeName, type);    _configManager.addType(type, new EmbeddableConfig(type.getName(), this, embeddableType));    try {      boolean isField = isField(type, embeddableConfig);      if (isField)        embeddableType.setFieldAccess(true);      // XXX: jpa/0u21      Embeddable ann = (Embeddable) type.getAnnotation(javax.persistence.Embeddable.class);      if (ann == null) {        isField = true;        embeddableType.setIdClass(true);      	_persistenceUnit.getAmberContainer().addEmbeddable(typeName,							   embeddableType);      }      embeddableType.setInstanceClassName(type.getName() +                                          "__ResinExt");      embeddableType.setEnhanced(true);      if (isField)        introspectFields(_persistenceUnit, embeddableType, null,                         type, embeddableConfig, true);      else        introspectMethods(_persistenceUnit, embeddableType, null,                          type, embeddableConfig);    } catch (ConfigException e) {      if (embeddableType != null)	embeddableType.setConfigException(e);      throw e;    } catch (RuntimeException e) {      if (embeddableType != null)	embeddableType.setConfigException(e);      throw e;    }    return embeddableType;  }  private MappedSuperclassConfig introspectEntityConfig(Class type)    throws SQLException  {    getInternalEntityConfig(type, _annotationCfg);      

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?