baseconfigintrospector.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 2,100 行 · 第 1/5 页
JAVA
2,100 行
} /** * Introspects named native queries. */ void introspectNamedNativeQueries(Class type, String typeName) { // jpa/0y2- getInternalNamedNativeQueryConfig(type, _annotationCfg); NamedNativeQuery namedNativeQueryAnn = (NamedNativeQuery) _annotationCfg.getAnnotation(); NamedNativeQueryConfig namedNativeQueryConfig = _annotationCfg.getNamedNativeQueryConfig(); NamedNativeQueries namedNativeQueriesAnn = (NamedNativeQueries) type.getAnnotation(NamedNativeQueries.class); if ((namedNativeQueryAnn == null) && (namedNativeQueriesAnn == null)) return; NamedNativeQuery namedNativeQueryArray[]; if ((namedNativeQueryAnn != null) && (namedNativeQueriesAnn != null)) { throw new ConfigException(L.l("{0} may not have both @NamedNativeQuery and @NamedNativeQueries", typeName)); } else if (namedNativeQueriesAnn != null) { namedNativeQueryArray = namedNativeQueriesAnn.value(); } else { namedNativeQueryArray = new NamedNativeQuery[] { namedNativeQueryAnn }; } for (int i=0; i < namedNativeQueryArray.length; i++) { namedNativeQueryAnn = namedNativeQueryArray[i]; NamedNativeQueryConfig nativeQueryConfig = new NamedNativeQueryConfig(); nativeQueryConfig.setQuery(namedNativeQueryAnn.query()); nativeQueryConfig.setResultClass(namedNativeQueryAnn.resultClass()); nativeQueryConfig.setResultSetMapping(namedNativeQueryAnn.resultSetMapping()); _persistenceUnit.addNamedNativeQuery(namedNativeQueryAnn.name(), nativeQueryConfig); } } /** * Introspects sql result set mappings. */ void introspectSqlResultSetMappings(Class type, EntityType entityType, String typeName) { // jpa/0y1- getInternalSqlResultSetMappingConfig(type, _annotationCfg); SqlResultSetMapping sqlResultSetMappingAnn = (SqlResultSetMapping) _annotationCfg.getAnnotation(); SqlResultSetMappingConfig sqlResultSetMappingConfig = _annotationCfg.getSqlResultSetMappingConfig(); SqlResultSetMappings sqlResultSetMappingsAnn = (SqlResultSetMappings) type.getAnnotation(SqlResultSetMappings.class); if ((sqlResultSetMappingAnn == null) && (sqlResultSetMappingsAnn == null)) return; SqlResultSetMapping sqlResultSetMappingArray[]; if ((sqlResultSetMappingAnn != null) && (sqlResultSetMappingsAnn != null)) { throw new ConfigException(L.l("{0} may not have both @SqlResultSetMapping and @SqlResultSetMappings", typeName)); } else if (sqlResultSetMappingsAnn != null) { sqlResultSetMappingArray = sqlResultSetMappingsAnn.value(); } else { sqlResultSetMappingArray = new SqlResultSetMapping[] { sqlResultSetMappingAnn }; } if (sqlResultSetMappingConfig != null) { _persistenceUnit.addSqlResultSetMapping(sqlResultSetMappingConfig.getName(), sqlResultSetMappingConfig); return; } for (int i=0; i < sqlResultSetMappingArray.length; i++) { sqlResultSetMappingAnn = sqlResultSetMappingArray[i]; String name = sqlResultSetMappingAnn.name(); EntityResult entities[] = sqlResultSetMappingAnn.entities(); ColumnResult columns[] = sqlResultSetMappingAnn.columns(); SqlResultSetMappingCompletion completion = new SqlResultSetMappingCompletion(entityType, name, entities, columns); _depCompletions.add(completion); } } /** * Completion callback for sql result set mappings. */ void addSqlResultSetMapping(String resultSetName, EntityResult entities[], ColumnResult columns[]) throws ConfigException { // jpa/0y1- SqlResultSetMappingConfig sqlResultSetMapping = new SqlResultSetMappingConfig(); // Adds @EntityResult. for (int i=0; i < entities.length; i++) { EntityResult entityResult = entities[i]; String className = entityResult.entityClass().getName(); EntityType resultType = _persistenceUnit.getEntityType(className); if (resultType == null) throw new ConfigException(L.l("entityClass '{0}' is not an @Entity bean for @SqlResultSetMapping '{1}'. The entityClass of an @EntityResult must be an @Entity bean.", className, resultSetName)); EntityResultConfig entityResultConfig = new EntityResultConfig(); entityResultConfig.setEntityClass(className); // @FieldResult annotations. FieldResult fields[] = entityResult.fields(); for (int j=0; j < fields.length; j++) { FieldResult fieldResult = fields[j]; String fieldName = fieldResult.name(); AmberField field = resultType.getField(fieldName); if (field == null) throw new ConfigException(L.l("@FieldResult with field name '{0}' is not a field for @EntityResult bean '{1}' in @SqlResultSetMapping '{2}'", fieldName, className, resultSetName)); String columnName = fieldResult.column(); if (columnName == null || columnName.length() == 0) throw new ConfigException(L.l("@FieldResult must have a column name defined and it must not be empty for '{0}' in @EntityResult '{1}' @SqlResultSetMapping '{2}'", fieldName, className, resultSetName)); FieldResultConfig fieldResultConfig = new FieldResultConfig(); fieldResultConfig.setName(fieldName); fieldResultConfig.setColumn(columnName); entityResultConfig.addFieldResult(fieldResultConfig); } sqlResultSetMapping.addEntityResult(entityResultConfig); } // Adds @ColumnResult. for (int i=0; i < columns.length; i++) { ColumnResult columnResult = columns[i]; String columnName = columnResult.name(); if (columnName == null || columnName.length() == 0) throw new ConfigException(L.l("@ColumnResult must have a column name defined and it must not be empty in @SqlResultSetMapping '{0}'", resultSetName)); ColumnResultConfig columnResultConfig = new ColumnResultConfig(); columnResultConfig.setName(columnName); sqlResultSetMapping.addColumnResult(columnResultConfig); } // Adds a global sql result set mapping to the persistence unit. _persistenceUnit.addSqlResultSetMapping(resultSetName, sqlResultSetMapping); } /** * Completes all partial bean introspection. */ public void configureLinks() throws ConfigException { RuntimeException exn = null; while (_linkCompletions.size() > 0) { Completion completion = _linkCompletions.remove(0); try { completion.complete(); } catch (Exception e) { if (e instanceof ConfigException) { log.warning(e.getMessage()); log.log(Level.FINEST, e.toString(), e); } else log.log(Level.WARNING, e.toString(), e); completion.getRelatedType().setConfigException(e); if (exn == null) exn = ConfigException.create(e); } } if (exn != null) throw exn; } /** * Completes all partial bean introspection. */ public void configureDependencies() throws ConfigException { RuntimeException exn = null; while (_depCompletions.size() > 0) { Completion completion = _depCompletions.remove(0); try { completion.complete(); } catch (Exception e) { if (e instanceof ConfigException) { log.warning(e.getMessage()); log.log(Level.FINEST, e.toString(), e); } else log.log(Level.WARNING, e.toString(), e); completion.getRelatedType().setConfigException(e); if (exn == null) exn = ConfigException.create(e); } } if (exn != null) throw exn; } /** * Introspects the fields. */ void introspectIdMethod(AmberPersistenceUnit persistenceUnit, EntityType entityType, EntityType parentType, Class type, Class idClass, MappedSuperclassConfig config) throws ConfigException, SQLException { ArrayList<IdField> keys = new ArrayList<IdField>(); IdField idField = null; AttributesConfig attributesConfig = null; if (config != null) attributesConfig = config.getAttributes(); for (Method method : type.getDeclaredMethods()) { String methodName = method.getName(); Class []paramTypes = method.getParameterTypes(); if (method.getDeclaringClass().equals(Object.class)) continue; if (! methodName.startsWith("get") || paramTypes.length != 0) { continue; } String fieldName = toFieldName(methodName.substring(3)); if (containsFieldOrCompletion(parentType, fieldName)) continue; getInternalIdConfig(type, method, fieldName, _annotationCfg); Annotation id = _annotationCfg.getAnnotation(); IdConfig idConfig = _annotationCfg.getIdConfig(); if (! _annotationCfg.isNull()) { idField = introspectId(persistenceUnit, entityType, method, fieldName, method.getReturnType(), idConfig); if (idField != null) keys.add(idField); } else { getInternalEmbeddedIdConfig(type, method, fieldName, _annotationCfg); Annotation embeddedId = _annotationCfg.getAnnotation(); EmbeddedIdConfig embeddedIdConfig = _annotationCfg.getEmbeddedIdConfig(); if (! _annotationCfg.isNull()) { idField = introspectEmbeddedId(persistenceUnit, entityType, method, fieldName, method.getReturnType()); break; } else { continue; } } } if (keys.size() == 0) { if (idField != null) { // @EmbeddedId was used. com.caucho.amber.field.EmbeddedId id = new com.caucho.amber.field.EmbeddedId(entityType, (EmbeddedIdField) idField); entityType.setId(id); } } else if (keys.size() == 1) { entityType.setId(new com.caucho.amber.field.Id(entityType, keys)); } else if (idClass == null) { throw new ConfigException(L.l("{0} has multiple @Id methods, but no @IdClass. Compound primary keys require either an @IdClass or exactly one @EmbeddedId field or property.", entityType.getName())); } else { CompositeId id = new CompositeId(entityType, keys); id.setKeyClass(idClass); entityType.setId(id); } } /** * Introspects the fields. */ void introspectIdField(AmberPersistenceUnit persistenceUnit, EntityType entityType, EntityType parentType, Class type, Class idClass, MappedSuperclassConfig config) throws ConfigException, SQLException { ArrayList<IdField> keys = new ArrayList<IdField>(); AttributesConfig attributesConfig = null; if (config != null) attributesConfig = config.getAttributes(); for (Field field : type.getDeclaredFields()) { String fieldName = field.getName(); if (containsFieldOrCompletion(parentType, fieldName)) continue; getInternalIdConfig(type, field, fieldName, _annotationCfg); Annotation id = _annotationCfg.getAnnotation(); IdConfig idConfig = _annotationCfg.getIdConfig(); if (_annotationCfg.isNull()) { getInternalEmbeddedIdConfig(type, field, fieldName, _annotationCfg); Annotation embeddedId = _annotationCfg.getAnnotation(); EmbeddedIdConfig embeddedIdConfig = _annotationCfg.getEmbeddedIdConfig(); if (_annotationCfg.isNull()) continue; } IdField idField = introspectId(persistenceUnit, entityType, field, fieldName, field.getType(), idConfig); if (idField != null) keys.add(idField); } if (keys.size() == 0) { } else if (keys.size() == 1) entityType.setId(new com.caucho.amber.field.Id(entityType, keys)); else if (idClass == null) { throw new ConfigException(L.l("{0} has multiple @Id fields, but no @IdClass. Compound primary keys require an @IdClass.", entityType.getName())); } else { CompositeId id = new CompositeId(entityType, keys); id.setKeyClass(idClass); entityType.setId(id); _configManager.introspect(idClass); } } /** * Check if it's field */ boolean isField(Class type, AbstractEnhancedConfig typeConfig, boolean isEmbeddable) throws ConfigException { if (type == null) return false;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?