baseconfigintrospector.java

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

JAVA
2,100
字号
    loop:    for (Method method : cl.getDeclaredMethods()) {      if (! method.getName().equals(name))	continue;      Class []types = method.getParameterTypes();      if (types.length != param.length)	continue;            for (int i = 0; i < types.length; i++) {	if (! param[i].equals(types[i]))	  continue loop;      }      return method;    }    return getMethod(cl.getSuperclass(), name, param);  }  /**   * Introspects the fields.   */  void introspectFields(AmberPersistenceUnit persistenceUnit,                        BeanType entityType,                        BeanType parentType,                        Class type,                        AbstractEnhancedConfig typeConfig,                        boolean isEmbeddable)    throws ConfigException  {    if (entityType.isEntity() && ((EntityType) entityType).getId() == null)      throw new ConfigException(L.l("{0} has no key", entityType));    for (Field field : type.getDeclaredFields()) {      String fieldName = field.getName();      if (containsFieldOrCompletion(parentType, fieldName)) {        continue;      }      if (Modifier.isStatic(field.getModifiers())          || Modifier.isTransient(field.getModifiers()))        continue;      Class fieldType = field.getType();      introspectField(persistenceUnit, entityType, field,                      fieldName, fieldType, typeConfig);    }  }  void introspectField(AmberPersistenceUnit persistenceUnit,                       BeanType sourceType,                       AccessibleObject field,                       String fieldName,                       Class fieldType,                       AbstractEnhancedConfig typeConfig)    throws ConfigException  {    EmbeddableConfig embeddableConfig = null;    MappedSuperclassConfig mappedSuperOrEntityConfig = null;    if (typeConfig instanceof EmbeddableConfig)      embeddableConfig = (EmbeddableConfig) typeConfig;    else if (typeConfig instanceof MappedSuperclassConfig)      mappedSuperOrEntityConfig = (MappedSuperclassConfig) typeConfig;    // jpa/0r37: interface fields must not be considered.    Class jClass;        if (field instanceof Field)      jClass = ((Field) field).getDeclaringClass();    else      jClass = ((Method) field).getDeclaringClass();    if (jClass.isInterface())      return;    // jpa/0r37: fields declared in non-entity superclasses    // must not be considered.    BeanType declaringType;    declaringType = _persistenceUnit.getEntityType(jClass.getName());    if (declaringType == null)      declaringType = _persistenceUnit.getEmbeddable(jClass.getName());    if (declaringType == null)      declaringType = _persistenceUnit.getMappedSuperclass(jClass.getName());    if (declaringType == null)      return;    AttributesConfig attributesConfig = null;    IdConfig idConfig = null;    BasicConfig basicConfig = null;    OneToOneConfig oneToOneConfig = null;    OneToManyConfig oneToManyConfig = null;    ManyToOneConfig manyToOneConfig = null;    ManyToManyConfig manyToManyConfig = null;    VersionConfig versionConfig = null;    ElementCollectionConfig elementCollectionConfig = null;    if (mappedSuperOrEntityConfig != null) {      attributesConfig = mappedSuperOrEntityConfig.getAttributes();      if (attributesConfig != null) {        idConfig = attributesConfig.getId(fieldName);        basicConfig = attributesConfig.getBasic(fieldName);        oneToOneConfig = attributesConfig.getOneToOne(fieldName);        oneToManyConfig = attributesConfig.getOneToMany(fieldName);	        elementCollectionConfig = null; // attributesConfig.getOneToMany(fieldName);        manyToOneConfig = attributesConfig.getManyToOne(fieldName);        manyToManyConfig = attributesConfig.getManyToMany(fieldName);        versionConfig = attributesConfig.getVersion(fieldName);      }    }    if (idConfig != null	|| field.isAnnotationPresent(javax.persistence.Id.class)) {      validateAnnotations(field, fieldName, "@Id", _idAnnotations);      if (! _idTypes.contains(fieldType.getName())) {        throw error(field, L.l("{0} is an invalid @Id type for {1}.",                               fieldType.getName(), fieldName));      }    }    else if (basicConfig != null	     || field.isAnnotationPresent(javax.persistence.Basic.class)) {      validateAnnotations(field, fieldName, "@Basic", _basicAnnotations);      BasicConfig basic        = new BasicConfig(this, sourceType, field, fieldName, fieldType);            basic.complete();    }    else if ((versionConfig != null)	     || field.isAnnotationPresent(javax.persistence.Version.class)) {      validateAnnotations(field, fieldName, "@Version", _versionAnnotations);      addVersion((EntityType) sourceType, field,                 fieldName, fieldType, versionConfig);    }    else if (field.isAnnotationPresent(javax.persistence.ManyToOne.class)) {      validateAnnotations(field, fieldName, "@ManyToOne", _manyToOneAnnotations);      ManyToOne ann = field.getAnnotation(ManyToOne.class);      Class targetEntity = null;      if (ann != null)        targetEntity = ann.targetEntity();      else {        targetEntity = manyToOneConfig.getTargetEntity();     }      if (targetEntity == null ||          targetEntity.getName().equals("void")) {        targetEntity = fieldType;      }      getInternalEntityConfig(targetEntity, _annotationCfg);      Annotation targetEntityAnn = _annotationCfg.getAnnotation();      EntityConfig targetEntityConfig = _annotationCfg.getEntityConfig();      if (_annotationCfg.isNull()) {        throw error(field, L.l("'{0}' is an illegal targetEntity for {1}.  @ManyToOne relations must target a valid @Entity.",                               targetEntity.getName(), fieldName));      }      if (! fieldType.isAssignableFrom(targetEntity)) {        throw error(field, L.l("'{0}' is an illegal targetEntity for {1}.  @ManyToOne targetEntity must be assignable to the field type '{2}'.",                               targetEntity.getName(),                               fieldName,                               fieldType.getName()));      }      EntityType entityType = (EntityType) sourceType;      entityType.setHasDependent(true);      _linkCompletions.add(new ManyToOneConfig(this,					       entityType,					       field,					       fieldName,					       fieldType));    }    else if (oneToManyConfig != null	     || field.isAnnotationPresent(javax.persistence.OneToMany.class)) {      validateAnnotations(field, fieldName, "@OneToMany", _oneToManyAnnotations);      if (field.isAnnotationPresent(javax.persistence.MapKey.class)) {        if (!fieldType.getName().equals("java.util.Map")) {          throw error(field, L.l("'{0}' is an illegal @OneToMany/@MapKey type for {1}. @MapKey must be a java.util.Map",                                 fieldType.getName(),                                 fieldName));        }      }      else if (! _oneToManyTypes.contains(fieldType.getName())) {        throw error(field, L.l("'{0}' is an illegal @OneToMany type for {1}.  @OneToMany must be a java.util.Collection, java.util.List or java.util.Map",                               fieldType.getName(),                               fieldName));      }      EntityType entityType = (EntityType) sourceType;      _depCompletions.add(new OneToManyConfig(this,					      entityType,					      field,					      fieldName,					      fieldType));    }    else if ((oneToOneConfig != null)	     || field.isAnnotationPresent(javax.persistence.OneToOne.class)) {      validateAnnotations(field, fieldName, "@OneToOne", _oneToOneAnnotations);      EntityType entityType = (EntityType) sourceType;      OneToOneConfig oneToOne        = new OneToOneConfig(this, entityType, field, fieldName, fieldType);            if (oneToOne.isOwningSide())        _linkCompletions.add(oneToOne);      else {        _depCompletions.add(0, oneToOne);        entityType.setHasDependent(true);      }    }    else if ((manyToManyConfig != null)	     || field.isAnnotationPresent(javax.persistence.ManyToMany.class)) {      if (field.isAnnotationPresent(javax.persistence.MapKey.class)) {        if (! fieldType.getName().equals("java.util.Map")) {          throw error(field, L.l("'{0}' is an illegal @ManyToMany/@MapKey type for {1}. @MapKey must be a java.util.Map",                                 fieldType.getName(),                                 fieldName));        }      }      ManyToManyConfig manyToMany	= new ManyToManyConfig(this, (EntityType) sourceType, field, fieldName, fieldType);      if (manyToMany.isOwningSide())        _linkCompletions.add(manyToMany);      else        _depCompletions.add(manyToMany);    }    else if (elementCollectionConfig != null	     || field.isAnnotationPresent(javax.persistence.ElementCollection.class)) {      validateAnnotations(field, fieldName, "@ElementCollection", _elementCollectionAnnotations);      if (! _elementCollectionTypes.contains(fieldType.getName())) {        throw error(field, L.l("'{0}' is an illegal @ElementCollection type for {1}.  @ElementCollection must be a java.util.Collection, java.util.List or java.util.Map",                               fieldType.getName(),                               fieldName));      }      EntityType entityType = (EntityType) sourceType;      ElementCollectionConfig comp	= new ElementCollectionConfig(entityType,				      field,				      fieldName,				      fieldType);      _depCompletions.add(comp);    }    else if (field.isAnnotationPresent(javax.persistence.Embedded.class)) {      validateAnnotations(field, fieldName, "@Embedded", _embeddedAnnotations);      EntityType entityType = (EntityType) sourceType;      entityType.setHasDependent(true);      _depCompletions.add(new EmbeddedCompletion(entityType,                                                 field,                                                 fieldName,                                                 fieldType,                                                 false));    }    else if (field.isAnnotationPresent(javax.persistence.EmbeddedId.class)) {      validateAnnotations(field, fieldName, "@EmbeddedId", _embeddedIdAnnotations);      _depCompletions.add(new EmbeddedCompletion((EntityType) sourceType,                                                 field,                                                 fieldName,                                                 fieldType,                                                 true));    }    else if (field.isAnnotationPresent(javax.persistence.Transient.class)) {    }    else {      BasicConfig basic        = new BasicConfig(this, sourceType, field, fieldName, fieldType);            basic.complete();    }  }  void addVersion(EntityType sourceType,                  AccessibleObject field,                  String fieldName,                  Class fieldType,                  VersionConfig versionConfig)    throws ConfigException  {    AmberPersistenceUnit persistenceUnit = sourceType.getPersistenceUnit();    Column columnAnn = field.getAnnotation(Column.class);    ColumnConfig columnConfig = null;    if (versionConfig != null)      columnConfig = versionConfig.getColumn();    if (! _versionTypes.contains(fieldType.getName())) {      throw error(field, L.l("{0} is an invalid @Version type for {1}.",                             fieldType.getName(), fieldName));    }    AmberType amberType = persistenceUnit.createType(fieldType);    AmberColumn fieldColumn = createColumn(sourceType, field, fieldName,                                      columnAnn, amberType, columnConfig);    VersionField version = new VersionField(sourceType, fieldName);    version.setColumn(fieldColumn);    sourceType.setVersionField(version);  }  private AmberColumn createColumn(BeanType beanType,                              AccessibleObject field,                              String fieldName,                              Column columnAnn,                              AmberType amberType,                              ColumnConfig columnConfig)    throws ConfigException  {    EntityType entityType = null;    if (beanType instanceof EntityType)      entityType = (EntityType) beanType;        String name;    if (columnAnn != null && ! columnAnn.name().equals(""))      name = (String) columnAnn.name();    else if (columnConfig != null && ! columnConfig.getName().equals(""))      name = columnConfig.getName();    else      name = toSqlName(fieldName);    AmberColumn column = null;    if (entityType == null) { // embeddable      column = new AmberColumn(null, name, amberType);    }    else if (columnAnn != null && ! columnAnn.table().equals("")) {      String tableName = columnAnn.table();      AmberTable table;      table = entityType.getSecondaryTable(tableName);      if (table == null)        throw error(field, L.l("{0} @Column(table='{1}') is an unknown secondary table.",                               fieldName,                               tableName));      column = table.createColumn(name, amberType);    }    else if (entityType.getTable() != null)      column = entityType.getTable().createColumn(name, amberType);    else { // jpa/0ge2: MappedSuperclassType      column = new AmberColumn(null, name, amberType);    }    if (column != null && columnAnn != null) {      // primaryKey = column.primaryKey();      column.setUnique(columnAnn.unique());      column.setNotNull(! columnAnn.nullable());      //insertable = column.insertable();      //updateable = column.updatable();      if (! "".equals(columnAnn.columnDefinition()))        column.setSQLType(columnAnn.columnDefinition());      column.setLength(columnAnn.length());      int precision = columnAnn.precision();      if (precision < 0) {        throw error(field, L.l("{0} @Column precision cannot be less than 0.",                               fieldName));      }      int scale = columnAnn.scale();      if (scale < 0) {        throw error(field, L.l("{0} @Column scale cannot be less than 0.",                               fieldName));      }      // this test implicitly works for case where      // precision is not set explicitly (ie: set to 0 by default)      // and scale is set      if (scale > precision) {        throw error(field, L.l("{0} @Column scale cannot be greater than precision. Must set precision to a non-zero value before setting scale.",                               fieldName));      }      if (precision > 0) {        column.setPrecision(precision);        column.setScale(scale);      }

⌨️ 快捷键说明

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