use of com.sun.jdo.api.persistence.model.jdo.PersistenceFieldElement in project Payara by payara.
the class ModelValidator method createKeyClassComponent.
/**
* Create a validation component which can check whether the key class
* of the persistence capable class is valid. This includes:
* <ul>
* <li> The key class must be public.
* <li> The key class must implement Serializable.
* <li> If the key class is an inner class, it must be static.
* <li> The key class must have a public constructor, which might be
* the default constructor or a no-arg constructor.
* <li> The field types of all non-static fields in the key class must be
* of valid types.
* <li> All serializable non-static fields in the key class must be public.
* <li> The names of the non-static fields in the key class must include the
* names of the primary key fields in the JDO class, and the types of the
* common fields must be identical
* <li> The key class must redefine equals and hashCode.
* </ul>
*/
protected ValidationComponent createKeyClassComponent(final String className) {
return new ValidationComponent() {
/**
* The class element of the key class
*/
private Object keyClass;
/**
* The fully qualified name of the key class
*/
private String keyClassName;
public void validate() throws ModelValidationException {
// checks the key class name
keyClassName = validateKeyClassName(className);
// initilialize keyClass field
keyClass = getModel().getClass(keyClassName, getClassLoader());
validateClass();
validateConstructor();
validateFields();
validateMethods();
}
/**
* Helper method validating the key class itself:
* public, serializable, static.
*/
private void validateClass() throws ModelValidationException {
Model model = getModel();
int modifiers = model.getModifiersForClass(keyClassName);
boolean hasKeyClassName = !StringHelper.isEmpty(keyClassName);
boolean isInnerClass = (hasKeyClassName && (keyClassName.indexOf('$') != -1));
String pcClassName = getClassName();
// check for key class existence
if (keyClass == null) {
throw new ModelValidationException(ModelValidationException.WARNING, model.getClass(pcClassName), I18NHelper.getMessage(getMessages(), // NOI18N
"util.validation.key_class_missing", keyClassName, pcClassName));
}
// check for public class modifier
if (!Modifier.isPublic(modifiers)) {
throw new ModelValidationException(keyClass, I18NHelper.getMessage(getMessages(), // NOI18N
"util.validation.key_class_public", keyClassName, pcClassName));
}
// if inner class it must be static
if (isInnerClass && !Modifier.isStatic(modifiers)) {
throw new ModelValidationException(keyClass, I18NHelper.getMessage(getMessages(), // NOI18N
"util.validation.key_class_static", keyClassName, pcClassName));
}
}
/**
* Helper method validating the fields of the key class.
*/
private void validateFields() throws ModelValidationException {
String pcClassName = getClassName();
Model model = getModel();
// check for valid typed public non-static fields
List keyClassFieldNames = model.getAllFields(keyClassName);
Map keyFields = getKeyFields();
for (Iterator i = keyClassFieldNames.iterator(); i.hasNext(); ) {
String keyClassFieldName = (String) i.next();
Object keyClassField = getKeyClassField(keyClassName, keyClassFieldName);
int keyClassFieldModifiers = model.getModifiers(keyClassField);
String keyClassFieldType = model.getType(keyClassField);
Object keyField = keyFields.get(keyClassFieldName);
if (Modifier.isStatic(keyClassFieldModifiers))
// we are not interested in static fields
continue;
if (!model.isValidKeyType(keyClassName, keyClassFieldName)) {
throw new ModelValidationException(keyClassField, I18NHelper.getMessage(getMessages(), // NOI18N
"util.validation.key_field_type_invalid", keyClassFieldName, keyClassName));
}
if (!Modifier.isPublic(keyClassFieldModifiers)) {
throw new ModelValidationException(keyClassField, I18NHelper.getMessage(getMessages(), // NOI18N
"util.validation.key_field_public", keyClassFieldName, keyClassName));
}
if (keyField == null)
continue;
if (!keyClassFieldType.equals(model.getType(keyField))) {
throw new ModelValidationException(keyClassField, I18NHelper.getMessage(getMessages(), // NOI18N
"util.validation.key_field_type_mismatch", keyClassFieldName, keyClassName, pcClassName));
}
// remove handled keyField from the list of keyFields
keyFields.remove(keyClassFieldName);
}
// check whether there are any unhandled key fields
if (!keyFields.isEmpty()) {
Object pcClass = model.getClass(pcClassName);
String fieldNames = StringHelper.arrayToSeparatedList(new ArrayList(keyFields.keySet()));
throw new ModelValidationException(pcClass, I18NHelper.getMessage(getMessages(), // NOI18N
"util.validation.key_field_missing", pcClassName, keyClassName, fieldNames));
}
}
/**
* Helper method validating the key class constructors.
*/
private void validateConstructor() throws ModelValidationException {
// no constructor or no arg constructor
Model model = getModel();
boolean hasConstr = model.hasConstructor(keyClassName);
Object noArgConstr = model.getConstructor(keyClassName, Model.NO_ARGS);
int modifiers = model.getModifiers(noArgConstr);
if (hasConstr && ((noArgConstr == null) || !Modifier.isPublic(modifiers))) {
throw new ModelValidationException(keyClass, I18NHelper.getMessage(getMessages(), // NOI18N
"util.validation.key_class_constructor", keyClassName, getClassName()));
}
}
/**
* Helper method validating the key class methods.
*/
private void validateMethods() throws ModelValidationException {
Model model = getModel();
Object equalsMethod = getNonObjectMethod(keyClassName, "equals", // NOI18N
Model.getEqualsArgs());
Object hashCodeMethod = getNonObjectMethod(keyClassName, "hashCode", // NOI18N
Model.NO_ARGS);
// check equals method
if (!matchesMethod(equalsMethod, Modifier.PUBLIC, 0, // NOI18N
"boolean")) {
throw new ModelValidationException(keyClass, I18NHelper.getMessage(getMessages(), // NOI18N
"util.validation.key_class_equals", keyClassName, getClassName()));
}
// check hashCode method
if (!matchesMethod(hashCodeMethod, Modifier.PUBLIC, 0, // NOI18N
"int")) {
throw new ModelValidationException(keyClass, I18NHelper.getMessage(getMessages(), // NOI18N
"util.validation.key_class_hashcode", keyClassName, getClassName()));
}
}
/**
* Helper method validating the name of the key class.
*/
private String validateKeyClassName(String keyClassName) throws ModelValidationException {
String pcClassName = getClassName();
Model model = getModel();
boolean hasKeyClassName = !StringHelper.isEmpty(keyClassName);
boolean hasPrefix;
String nameSuffix;
boolean isOIDNameSuffix;
// check for existence of key class name
if (!hasKeyClassName) {
throw new ModelValidationException(ModelValidationException.WARNING, model.getClass(pcClassName), I18NHelper.getMessage(getMessages(), // NOI18N
"util.validation.key_class_unset", pcClassName));
}
keyClassName = keyClassName.trim();
hasPrefix = keyClassName.startsWith(pcClassName);
nameSuffix = (hasPrefix ? keyClassName.substring(pcClassName.length()) : keyClassName);
isOIDNameSuffix = (// NOI18N
nameSuffix.equalsIgnoreCase(".OID") || // NOI18N
nameSuffix.equalsIgnoreCase("$OID"));
if (!hasPrefix || (// NOI18N
!nameSuffix.equalsIgnoreCase("Key") && !isOIDNameSuffix)) {
Object pcClass = getModel().getClass(pcClassName);
throw new ModelValidationException(pcClass, I18NHelper.getMessage(getMessages(), // NOI18N
"util.validation.key_class_invalid", keyClassName, pcClassName));
}
if (isOIDNameSuffix) {
StringBuilder buf = new StringBuilder(keyClassName);
buf.setCharAt(keyClassName.length() - 4, '$');
return buf.toString();
}
return keyClassName;
}
// helper method which returns a field object from the
// given class or one of its superclasses
private Object getKeyClassField(String keyClassName, String keyClassFieldName) {
Model model = getModel();
Object keyClassField = model.getField(keyClassName, keyClassFieldName);
if (// this is an inherited field
keyClassField == null) {
keyClassField = model.getInheritedField(keyClassName, keyClassFieldName);
}
return keyClassField;
}
/**
* Helper method returning the key fields of the pc class as a map.
*/
private Map getKeyFields() {
Model model = getModel();
String pcClassName = getClassName();
PersistenceClassElement pce = model.getPersistenceClass(pcClassName);
PersistenceFieldElement[] fields = pce.getFields();
Map keyFields = new HashMap();
if (fields != null) {
for (int i = 0; i < fields.length; i++) {
PersistenceFieldElement pfe = fields[i];
if (pfe.isKey()) {
String name = pfe.getName();
keyFields.put(name, model.getField(pcClassName, name));
}
}
}
return keyFields;
}
// helper method which returns a method object from the
// given class or one of its superclasses provided it
// is not java.lang.Object
private Object getNonObjectMethod(String className, String methodName, String[] argTypeNames) {
Model model = getModel();
Object method = model.getMethod(className, methodName, argTypeNames);
if (// look for an inherited method
method == null) {
method = model.getInheritedMethod(className, methodName, argTypeNames);
if ((method != null) && model.getDeclaringClass(method).equals(// NOI18N
"java.lang.Object")) {
method = null;
}
}
return method;
}
};
}
use of com.sun.jdo.api.persistence.model.jdo.PersistenceFieldElement in project Payara by payara.
the class JDOConcreteBean20Generator method generateGetSetMethods.
/**
* Adds getters and setters.
*/
private void generateGetSetMethods(PersistenceFieldElement[] fields) throws IOException {
int i, count = ((fields != null) ? fields.length : 0);
// reset to null to clean it up.
setPKField = null;
cascadeDelete = new StringBuilder();
// jdoCleanCollectionRef() body
StringBuilder cmrcleanbodyBuf = new StringBuilder(CMP20TemplateFormatter.none_);
for (i = 0; i < count; i++) {
PersistenceFieldElement pfe = fields[i];
if (PersistenceFieldElement.PERSISTENT == pfe.getPersistenceType()) {
// Reset the strings.
gbody = null;
sbody = null;
FieldInfo fieldInfo = new FieldInfo(model, nameMapper, pfe, beanName, pcname);
if (fieldInfo.isGeneratedField) {
// a 2 way managed relationship.
if (fieldInfo.isKey) {
// This is an extra field for the unknown PK class.
// PK setter name is used to generate the line for ejbCreate
// to set the PK value in _JDOState.
setPKField = fieldInfo.setter;
}
continue;
}
if (!(pfe instanceof RelationshipElement)) {
generateCMPGetSetBodies(fieldInfo);
} else {
// CMR
if (isUpdateable) {
generateCMRGetSetBodies(fieldInfo, cmrcleanbodyBuf);
} else {
logger.log(Logger.WARNING, I18NHelper.getMessage(messages, "CMG.CMRAccessNotAllowed", beanName, // NOI18N
fieldInfo.name));
gbody = CMPROTemplateFormatter.accessNotAllowedTemplate;
sbody = CMPROTemplateFormatter.updateNotAllowedTemplate;
}
}
// Now generate getter and setter:
CMPTemplateFormatter.addGenericMethod(fieldInfo.getter, Modifier.PUBLIC, fieldInfo.type, CMP20TemplateFormatter.getBodyAsStrings(gbody), concreteImplWriter);
oneParam[0] = fieldInfo.type;
// name
concreteImplWriter.addMethod(// name
fieldInfo.setter, // modifiers
Modifier.PUBLIC, // returnType
CMP20TemplateFormatter.void_, // parameterNames
param0, // parameterTypes
oneParam, // exceptions
null, // body
CMP20TemplateFormatter.getBodyAsStrings(sbody), // comments
null);
}
}
// Now generate jdoCleanCollectionRef method
CMPTemplateFormatter.addGenericMethod(CMP20TemplateFormatter.jdoCleanCollectionRef_, CMP20TemplateFormatter.getBodyAsStrings(cmrcleanbodyBuf.toString()), concreteImplWriter);
}
use of com.sun.jdo.api.persistence.model.jdo.PersistenceFieldElement in project Payara by payara.
the class ModelValidator method createFieldConsistencyComponent.
/**
* Create a validation component which can check whether the field is
* consistent (if in mapping model but not jdo, it is a problem).
* @param field the field whose consistency is being checked
* @return the validation component
*/
protected ValidationComponent createFieldConsistencyComponent(final MappingFieldElement field) {
return new ValidationComponent() {
public void validate() throws ModelValidationException {
if (field != null) {
String fieldName = field.getName();
PersistenceClassElement persistenceClass = getPersistenceClass(getClassName());
PersistenceFieldElement persistenceElement = ((persistenceClass != null) ? persistenceClass.getField(fieldName) : null);
if (persistenceElement == null) {
throw constructFieldException(fieldName, // NOI18N
"util.validation.field_model_inconsistent");
}
}
}
};
}
use of com.sun.jdo.api.persistence.model.jdo.PersistenceFieldElement in project Payara by payara.
the class ModelValidator method getFullValidationList.
/**
* Computes and returns a collection of ValidationComponents
* representing the tests to be performed during validation. These
* include all those in the basic list plus those which check
* cardinality and the related classes in more detail.
* @return a collection of ValidationComponents representing the
* tests to be performed during validation.
* @see #getRelatedClassValidationList
* @see #getBasicValidationList
*/
public Collection getFullValidationList() {
ArrayList list = new ArrayList(getBasicValidationList());
String className = getClassName();
PersistenceClassElement persistenceClass = getPersistenceClass(className);
if (persistenceClass != null) {
PersistenceFieldElement[] fields = persistenceClass.getFields();
int i, count = ((fields != null) ? fields.length : 0);
list.add(createSerializableClassComponent(className));
list.add(createKeyClassComponent(persistenceClass.getKeyClass()));
list.add(createClassMappingComponent(persistenceClass));
list.add(createKeyColumnMappingComponent(persistenceClass));
for (i = 0; i < count; i++) {
PersistenceFieldElement field = fields[i];
list.add(createFieldCardinalityComponent(field));
list.add(createFieldMappingComponent(field));
list.add(createFieldBlobMappingComponent(field));
list.addAll(getRelatedClassValidationList(field));
}
}
return Collections.unmodifiableCollection(list);
}
use of com.sun.jdo.api.persistence.model.jdo.PersistenceFieldElement in project Payara by payara.
the class ModelValidator method createKeyColumnMappingComponent.
/**
* Create a validation component which can check whether the class
* contains field mappings for all primary key columns.
* @param persistenceClass the class whose mapping is being checked
* @return the validation component
*/
protected ValidationComponent createKeyColumnMappingComponent(final PersistenceClassElement persistenceClass) {
return new ValidationComponent() {
public void validate() throws ModelValidationException {
String className = getClassName();
MappingClassElement mappingClass = getMappingClass(className);
if (mappingClass != null) {
List tables = mappingClass.getTables();
if (tables.size() > 0) {
String tableName = ((MappingTableElement) tables.get(0)).getName();
TableElement table = getTable(tableName, getSchemaForClass(className));
List columns = getUnmappedColumnNames(((table != null) ? table.getPrimaryKey() : null), mappingClass);
if ((columns != null) && (columns.size() > 0)) {
throw new ModelValidationException(ModelValidationException.WARNING, getOffendingObject(null), I18NHelper.getMessage(getMessages(), // NOI18N
"util.validation.class_key_column_missing", className, tableName, StringHelper.arrayToSeparatedList(columns)));
}
}
}
}
private List getUnmappedColumnNames(KeyElement primaryKey, MappingClassElement mappingClass) {
List unmappedColumns = null;
if (// check if primary table has a pk
primaryKey != null) {
ColumnElement[] columns = primaryKey.getColumns();
int count = ((columns != null) ? columns.length : 0);
// all columns in the pk should be mapped to key fields
if (count > 0) {
List mappingFields = mappingClass.getFields();
Iterator iterator = mappingFields.iterator();
unmappedColumns = getRelativeColumnNames(columns);
while (iterator.hasNext()) {
MappingFieldElement field = (MappingFieldElement) iterator.next();
if (isKeyField(field))
unmappedColumns.removeAll(field.getColumns());
}
}
}
return unmappedColumns;
}
private List getRelativeColumnNames(ColumnElement[] columns) {
int i, count = ((columns != null) ? columns.length : 0);
List columnNames = new ArrayList(count);
for (i = 0; i < count; i++) {
columnNames.add(NameUtil.getRelativeMemberName(columns[i].getName().getFullName()));
}
return columnNames;
}
private boolean isKeyField(MappingFieldElement field) {
PersistenceFieldElement persistenceField = persistenceClass.getField(field.getName());
return ((persistenceField != null) && persistenceField.isKey());
}
};
}
Aggregations