use of org.datanucleus.exceptions.ClassNotResolvedException in project datanucleus-core by datanucleus.
the class ClassMetaData method populateMemberMetaData.
/**
* Populate MetaData for all members.
* @param clr The ClassLoaderResolver
* @param cls This class
* @param pkMembers Process pk fields/properties (or non-PK if false)
* @param primary the primary ClassLoader to use (or null)
* @throws InvalidMetaDataException if the Class for a declared type in a field cannot be loaded by the <code>clr</code>
* @throws InvalidMetaDataException if a field declared in the MetaData does not exist in the Class
*/
protected void populateMemberMetaData(ClassLoaderResolver clr, Class cls, boolean pkMembers, ClassLoader primary) {
Collections.sort(members);
// Populate the real field values. This will populate any containers in these members also
Iterator<AbstractMemberMetaData> memberIter = members.iterator();
while (memberIter.hasNext()) {
AbstractMemberMetaData mmd = memberIter.next();
if (pkMembers == mmd.isPrimaryKey()) {
Class fieldCls = cls;
if (!mmd.fieldBelongsToClass()) {
// Field overrides a field in a superclass, so find the class
try {
fieldCls = clr.classForName(mmd.getClassName());
} catch (ClassNotResolvedException cnre) {
// Not found at specified location, so try the same package as this class
String fieldClassName = getPackageName() + "." + mmd.getClassName();
try {
fieldCls = clr.classForName(fieldClassName);
mmd.setClassName(fieldClassName);
} catch (ClassNotResolvedException cnre2) {
NucleusLogger.METADATA.error(Localiser.msg("044092", fullName, mmd.getFullFieldName(), fieldClassName));
throw new InvalidClassMetaDataException("044092", fullName, mmd.getFullFieldName(), fieldClassName);
}
}
}
boolean populated = false;
if (mmd instanceof PropertyMetaData) {
// User class must have a getter and setter for this property as per Java Beans
Method getMethod = null;
try {
// Find the getter
// a). Try as a standard form of getter (getXXX)
// Only public?
getMethod = fieldCls.getDeclaredMethod(ClassUtils.getJavaBeanGetterName(mmd.getName(), false));
} catch (Exception e) {
try {
// b). Try as a boolean form of getter (isXXX)
// Only public?
getMethod = fieldCls.getDeclaredMethod(ClassUtils.getJavaBeanGetterName(mmd.getName(), true));
} catch (Exception e2) {
}
}
if (getMethod != null && getMethod.getReturnType() == void.class) {
throw new InvalidClassMetaDataException("044166", fullName, mmd.getName());
}
if (getMethod == null && mmd.getPersistenceModifier() != FieldPersistenceModifier.NONE) {
// Property is persistent yet no getter!
throw new InvalidClassMetaDataException("044073", fullName, mmd.getName());
}
Method setMethod = null;
try {
// Find the setter
String setterName = ClassUtils.getJavaBeanSetterName(mmd.getName());
// Only gives public methods
Method[] methods = fieldCls.getMethods();
for (int i = 0; i < methods.length; i++) {
if (methods[i].getName().equals(setterName) && methods[i].getParameterTypes() != null && methods[i].getParameterTypes().length == 1) {
setMethod = methods[i];
break;
}
}
if (setMethod == null) {
// Also try protected/private of this class
methods = fieldCls.getDeclaredMethods();
for (int i = 0; i < methods.length; i++) {
if (methods[i].getName().equals(setterName) && methods[i].getParameterTypes() != null && methods[i].getParameterTypes().length == 1 && !methods[i].isBridge()) {
setMethod = methods[i];
break;
}
}
}
} catch (Exception e) {
}
if (setMethod == null && mmd.getPersistenceModifier() != FieldPersistenceModifier.NONE) {
// Property is persistent yet no setter!
throw new InvalidClassMetaDataException("044074", fullName, mmd.getName());
} else if (setMethod != null && getMethod != null) {
// Check types of getter/setter
Class getType = getMethod.getReturnType();
Class setType = setMethod.getParameterTypes()[0];
if (!getType.isAssignableFrom(setType) && !setType.isAssignableFrom(getType)) {
throw new InvalidMetaDataException("044167", fullName, mmd.getName(), getType.getName(), setType.getName());
}
}
// Populate the property using the getter
if (getMethod != null) {
mmd.populate(clr, null, getMethod, primary, mmgr);
populated = true;
}
} else {
Field clsField = null;
try {
clsField = fieldCls.getDeclaredField(mmd.getName());
} catch (Exception e) {
}
if (clsField != null) {
mmd.populate(clr, clsField, null, primary, mmgr);
populated = true;
}
}
if (!populated) {
// MetaData field doesn't exist in the class!
throw new InvalidClassMetaDataException("044071", fullName, mmd.getFullFieldName());
}
}
}
}
use of org.datanucleus.exceptions.ClassNotResolvedException in project datanucleus-core by datanucleus.
the class MetaDataManagerImpl method loadJar.
/* (non-Javadoc)
* @see org.datanucleus.metadata.MetaDataManager#loadJar(java.lang.String, java.lang.ClassLoader)
*/
@Override
public FileMetaData[] loadJar(String jarFileName, ClassLoader loader) {
if (!allowMetaDataLoad) {
return null;
}
boolean originatingLoadCall = false;
if (listenersLoadedMetaData == null && listeners != null) {
originatingLoadCall = true;
listenersLoadedMetaData = new ArrayList<AbstractClassMetaData>();
}
try {
if (originatingLoadCall) {
updateLock.lock();
}
if (NucleusLogger.METADATA.isDebugEnabled()) {
NucleusLogger.METADATA.debug(Localiser.msg("044009", jarFileName));
}
ClassLoaderResolver clr = nucleusContext.getClassLoaderResolver(loader);
List<FileMetaData> fileMetaData = new ArrayList<>();
// Generate list of package.jdo and classes present in the jar
Set<String> mappingFileNames = new HashSet<>();
if (allowXML) {
String[] packageJdoFiles = ClassUtils.getPackageJdoFilesForJarFile(jarFileName);
if (packageJdoFiles != null) {
for (String packageJdoFile : packageJdoFiles) {
mappingFileNames.add(packageJdoFile);
}
}
}
Set<String> classNames = new HashSet<>();
if (allowAnnotations) {
String[] jarClassNames = ClassUtils.getClassNamesForJarFile(jarFileName);
if (jarClassNames != null) {
for (String jarClassName : jarClassNames) {
classNames.add(jarClassName);
}
}
}
Set<Throwable> exceptions = new HashSet<>();
if (allowXML && !mappingFileNames.isEmpty()) {
// Load XML metadata
for (String mappingFileName : mappingFileNames) {
try {
Enumeration files = clr.getResources(mappingFileName, Thread.currentThread().getContextClassLoader());
while (files.hasMoreElements()) {
URL url = (URL) files.nextElement();
if (url != null && fileMetaDataByURLString.get(url.toString()) == null) {
FileMetaData filemd = parseFile(url);
if (filemd != null) {
// Register the file
registerFile(url.toString(), filemd, clr);
fileMetaData.add(filemd);
}
}
}
} catch (InvalidMetaDataException imde) {
// Error in the metadata for this file
NucleusLogger.METADATA.error(StringUtils.getStringFromStackTrace(imde));
exceptions.add(imde);
} catch (IOException ioe) {
NucleusLogger.METADATA.error(Localiser.msg("044027", jarFileName, mappingFileName, ioe.getMessage()), ioe);
}
}
}
if (allowAnnotations && !classNames.isEmpty()) {
// Load annotation metadata for all classes
for (String className : classNames) {
// Check for MetaData for this class (take precedence over annotations if they exist)
AbstractClassMetaData cmd = classMetaDataByClass.get(className);
if (cmd == null) {
// No MetaData so try annotations
try {
Class cls = clr.classForName(className);
FileMetaData filemd = loadAnnotationsForClass(cls, clr, true, false);
if (filemd != null) {
fileMetaData.add(filemd);
}
} catch (ClassNotResolvedException e) {
// log and ignore this exception
NucleusLogger.METADATA.error(StringUtils.getStringFromStackTrace(e));
} catch (Throwable e) {
exceptions.add(e);
}
} else {
// We have MetaData, and any annotations will be merged in during the populate process
}
}
}
if (!exceptions.isEmpty()) {
throw new NucleusUserException(Localiser.msg("044024", jarFileName), exceptions.toArray(new Throwable[exceptions.size()]));
}
if (!fileMetaData.isEmpty()) {
// Populate/Initialise all loaded FileMetaData
initialiseFileMetaDataForUse(fileMetaData, clr);
}
if (NucleusLogger.METADATA.isDebugEnabled()) {
NucleusLogger.METADATA.debug(Localiser.msg("044010"));
}
if (originatingLoadCall) {
processListenerLoadingCall();
}
return fileMetaData.toArray(new FileMetaData[fileMetaData.size()]);
} finally {
if (originatingLoadCall) {
updateLock.unlock();
}
}
}
use of org.datanucleus.exceptions.ClassNotResolvedException in project datanucleus-core by datanucleus.
the class EmbeddedMetaData method populate.
/**
* Method to populate the embedded MetaData.
* This performs checks on the validity of the field types for embedding.
* @param clr The class loader to use where necessary
* @param primary the primary ClassLoader to use (or null)
*/
public void populate(ClassLoaderResolver clr, ClassLoader primary) {
// Find the class that the embedded fields apply to
MetaDataManager mmgr = getMetaDataManager();
// Field that has <embedded>
AbstractMemberMetaData apmd = null;
// Definition for the embedded class
AbstractClassMetaData embCmd = null;
// Name of the embedded type
String embeddedType = null;
MetaData md = getParent();
if (md instanceof AbstractMemberMetaData) {
// PC embedded in PC object
apmd = (AbstractMemberMetaData) md;
embeddedType = apmd.getTypeName();
embCmd = mmgr.getMetaDataForClassInternal(apmd.getType(), clr);
if (embCmd == null && apmd.getFieldTypes() != null && apmd.getFieldTypes().length == 1) {
// The specified field is not embeddable, nor is it persistent-interface, so try field-type for embedding
embCmd = mmgr.getMetaDataForClassInternal(clr.classForName(apmd.getFieldTypes()[0]), clr);
}
if (embCmd == null) {
NucleusLogger.METADATA.error(Localiser.msg("044121", apmd.getFullFieldName(), apmd.getTypeName()));
throw new InvalidMemberMetaDataException("044121", apmd.getClassName(), apmd.getName(), apmd.getTypeName());
}
} else if (md instanceof ElementMetaData) {
// PC element embedded in collection
ElementMetaData elemmd = (ElementMetaData) md;
apmd = (AbstractMemberMetaData) elemmd.getParent();
embeddedType = apmd.getCollection().getElementType();
try {
Class cls = clr.classForName(embeddedType, primary);
embCmd = mmgr.getMetaDataForClassInternal(cls, clr);
} catch (ClassNotResolvedException cnre) {
// Should be caught by populating the Collection
}
if (embCmd == null) {
NucleusLogger.METADATA.error(Localiser.msg("044122", apmd.getFullFieldName(), embeddedType));
throw new InvalidMemberMetaDataException("044122", apmd.getClassName(), apmd.getName(), embeddedType);
}
} else if (md instanceof KeyMetaData) {
// PC key embedded in Map
KeyMetaData keymd = (KeyMetaData) md;
apmd = (AbstractMemberMetaData) keymd.getParent();
embeddedType = apmd.getMap().getKeyType();
try {
Class cls = clr.classForName(embeddedType, primary);
embCmd = mmgr.getMetaDataForClassInternal(cls, clr);
} catch (ClassNotResolvedException cnre) {
// Should be caught by populating the Map
}
if (embCmd == null) {
NucleusLogger.METADATA.error(Localiser.msg("044123", apmd.getFullFieldName(), embeddedType));
throw new InvalidMemberMetaDataException("044123", apmd.getClassName(), apmd.getName(), embeddedType);
}
} else if (md instanceof ValueMetaData) {
// PC value embedded in Map
ValueMetaData valuemd = (ValueMetaData) md;
apmd = (AbstractMemberMetaData) valuemd.getParent();
embeddedType = apmd.getMap().getValueType();
try {
Class cls = clr.classForName(embeddedType, primary);
embCmd = mmgr.getMetaDataForClassInternal(cls, clr);
} catch (ClassNotResolvedException cnre) {
// Should be caught by populating the Map
}
if (embCmd == null) {
NucleusLogger.METADATA.error(Localiser.msg("044124", apmd.getFullFieldName(), embeddedType));
throw new InvalidMemberMetaDataException("044124", apmd.getClassName(), apmd.getName(), embeddedType);
}
} else {
throw new InvalidMemberMetaDataException("044165", md.getClass().getName());
}
// Check that all "members" are of the correct type for the embedded object
for (AbstractMemberMetaData mmd : members) {
// TODO Should allow PropertyMetaData here I think
if (embCmd instanceof InterfaceMetaData && mmd instanceof FieldMetaData) {
// Cannot have a field within a persistent interface
throw new InvalidMemberMetaDataException("044129", apmd.getClassName(), apmd.getName(), mmd.getName());
}
}
Set<String> memberNames = new HashSet<>();
for (AbstractMemberMetaData mmd : members) {
memberNames.add(mmd.getName());
}
// Add fields for the class that aren't in the <embedded> block using Reflection.
// TODO Consider getting rid of this ... should fall back to the ClassMetaData for the embedded class
// NOTE 1 : We ignore fields in superclasses
// NOTE 2 : We ignore "enhanced" fields (added by the JDO enhancer)
// NOTE 3 : We ignore inner class fields (containing "$")
// NOTE 4 : We sort the fields into ascending alphabetical order
Class embeddedClass = null;
Collections.sort(members);
try {
// Load the embedded class
embeddedClass = clr.classForName(embeddedType, primary);
// TODO Cater for properties in the populating class when the user defines using setters
// Get all (reflected) fields in the populating class
Field[] cls_fields = embeddedClass.getDeclaredFields();
for (int i = 0; i < cls_fields.length; i++) {
// Limit to fields in this class, that aren't enhanced fields that aren't inner class fields, and that aren't static
if (cls_fields[i].getDeclaringClass().getName().equals(embeddedType) && !mmgr.isEnhancerField(cls_fields[i].getName()) && !ClassUtils.isInnerClass(cls_fields[i].getName()) && !Modifier.isStatic(cls_fields[i].getModifiers())) {
// Find if there is a AbstractMemberMetaData for this field.
if (!memberNames.contains(cls_fields[i].getName())) {
// Start from the metadata of the field in the owning class if available
AbstractMemberMetaData embMmd = embCmd.getMetaDataForMember(cls_fields[i].getName());
FieldMetaData omittedFmd = null;
if (embMmd != null) {
FieldPersistenceModifier fieldModifier = embMmd.getPersistenceModifier();
if (fieldModifier == FieldPersistenceModifier.DEFAULT) {
// Modifier not yet set, so work it out
fieldModifier = embMmd.getDefaultFieldPersistenceModifier(cls_fields[i].getType(), cls_fields[i].getModifiers(), mmgr.isFieldTypePersistable(cls_fields[i].getType()), mmgr);
}
if (fieldModifier == FieldPersistenceModifier.PERSISTENT) {
// Only add if the owning class defines it as persistent
omittedFmd = new FieldMetaData(this, embMmd);
// Embedded field can't default to being part of PK, user has to set that
omittedFmd.setPrimaryKey(false);
}
} else {
// No metadata defined, so add a default FieldMetaData for this field
omittedFmd = new FieldMetaData(this, cls_fields[i].getName());
}
if (omittedFmd != null) {
NucleusLogger.METADATA.debug(Localiser.msg("044125", apmd.getClassName(), cls_fields[i].getName(), embeddedType));
members.add(omittedFmd);
memberNames.add(omittedFmd.getName());
Collections.sort(members);
}
}
}
}
} catch (Exception e) {
NucleusLogger.METADATA.error(e.getMessage(), e);
throw new RuntimeException(e.getMessage());
}
// add properties of interface, only if interface
if (embCmd instanceof InterfaceMetaData) {
try {
// Get all (reflected) fields in the populating class
Method[] clsMethods = embeddedClass.getDeclaredMethods();
for (int i = 0; i < clsMethods.length; i++) {
// that aren't inner class fields, and that aren't static
if (clsMethods[i].getDeclaringClass().getName().equals(embeddedType) && (clsMethods[i].getName().startsWith("get") || clsMethods[i].getName().startsWith("is")) && !clsMethods[i].isBridge() && !ClassUtils.isInnerClass(clsMethods[i].getName())) {
// Find if there is a PropertyMetaData for this field
String fieldName = ClassUtils.getFieldNameForJavaBeanGetter(clsMethods[i].getName());
if (!memberNames.contains(fieldName)) {
// Add a default PropertyMetaData for this field.
NucleusLogger.METADATA.debug(Localiser.msg("044060", apmd.getClassName(), fieldName));
PropertyMetaData pmd = new PropertyMetaData(this, fieldName);
members.add(pmd);
memberNames.add(pmd.getName());
Collections.sort(members);
}
}
}
} catch (Exception e) {
NucleusLogger.METADATA.error(e.getMessage(), e);
throw new RuntimeException(e.getMessage());
}
}
Collections.sort(members);
Iterator<AbstractMemberMetaData> memberIter = members.iterator();
while (memberIter.hasNext()) {
Class embFmdClass = embeddedClass;
AbstractMemberMetaData fieldFmd = memberIter.next();
if (!fieldFmd.fieldBelongsToClass()) {
try {
embFmdClass = clr.classForName(fieldFmd.getClassName(true));
} catch (ClassNotResolvedException cnre) {
// Maybe the user specified just "classBasicName.fieldName", so try with package name of this
String fieldClsName = embeddedClass.getPackage().getName() + "." + fieldFmd.getClassName(true);
fieldFmd.setClassName(fieldClsName);
embFmdClass = clr.classForName(fieldClsName);
}
}
if (fieldFmd instanceof FieldMetaData) {
Field cls_field = null;
try {
cls_field = embFmdClass.getDeclaredField(fieldFmd.getName());
} catch (Exception e) {
// MetaData field doesn't exist in the class!
throw new InvalidMemberMetaDataException("044071", embFmdClass.getName(), fieldFmd.getName());
}
fieldFmd.populate(clr, cls_field, null, primary, mmgr);
} else {
Method cls_method = null;
try {
cls_method = embFmdClass.getDeclaredMethod(ClassUtils.getJavaBeanGetterName(fieldFmd.getName(), true));
} catch (Exception e) {
try {
cls_method = embFmdClass.getDeclaredMethod(ClassUtils.getJavaBeanGetterName(fieldFmd.getName(), false));
} catch (Exception e2) {
// MetaData field doesn't exist in the class!
throw new InvalidMemberMetaDataException("044071", embFmdClass.getName(), fieldFmd.getName());
}
}
fieldFmd.populate(clr, null, cls_method, primary, mmgr);
}
}
if (embCmd.isEmbeddedOnly()) {
// We do not support recursive embedding since if a 1-1 this would result in adding embedded columns infinite times, and for 1-N infinite join tables.
for (AbstractMemberMetaData mmd : members) {
if (mmd.getTypeName().equals(embCmd.getFullClassName())) {
throw new InvalidMetaDataException("044128", embCmd.getFullClassName(), mmd.getName());
} else if (mmd.hasCollection() && mmd.getCollection().getElementType().equals(embCmd.getFullClassName())) {
throw new InvalidMetaDataException("044128", embCmd.getFullClassName(), mmd.getName());
}
}
}
}
use of org.datanucleus.exceptions.ClassNotResolvedException in project datanucleus-core by datanucleus.
the class InterfaceMetaData method populatePropertyMetaData.
/**
* Populate PropertyMetaData.
* @param clr The ClassLoader
* @param cls This class
* @param pkFields Process pk fields (or non-PK fields if false)
* @param primary the primary ClassLoader to use (or null)
* @throws InvalidMetaDataException if the Class for a declared type in a field cannot be loaded by the <code>clr</code>
* @throws InvalidMetaDataException if a field declared in the MetaData does not exist in the Class
*/
protected void populatePropertyMetaData(ClassLoaderResolver clr, Class cls, boolean pkFields, ClassLoader primary) {
Collections.sort(members);
// Populate the AbstractMemberMetaData with their real field values
// This will populate any containers in these fields also
Iterator<AbstractMemberMetaData> fields_iter = members.iterator();
while (fields_iter.hasNext()) {
AbstractMemberMetaData fmd = fields_iter.next();
if (pkFields == fmd.isPrimaryKey()) {
Class fieldCls = cls;
if (!fmd.fieldBelongsToClass()) {
// Field overrides a field in a superclass, so find the class
try {
fieldCls = clr.classForName(fmd.getClassName(), primary);
} catch (ClassNotResolvedException cnre) {
// Not found at specified location, so try the same package as this class
String fieldClassName = getPackageName() + "." + fmd.getClassName();
try {
fieldCls = clr.classForName(fieldClassName, primary);
fmd.setClassName(fieldClassName);
} catch (ClassNotResolvedException cnre2) {
NucleusLogger.METADATA.error(Localiser.msg("044080", fieldClassName));
throw new InvalidClassMetaDataException("044080", fullName, fieldClassName);
}
}
}
Method cls_method = null;
try {
cls_method = fieldCls.getDeclaredMethod(ClassUtils.getJavaBeanGetterName(fmd.getName(), true));
} catch (Exception e) {
try {
cls_method = fieldCls.getDeclaredMethod(ClassUtils.getJavaBeanGetterName(fmd.getName(), false));
} catch (Exception e2) {
// MetaData method doesn't exist in the class!
throw new InvalidClassMetaDataException("044072", fullName, fmd.getFullFieldName());
}
}
fmd.populate(clr, null, cls_method, primary, mmgr);
}
}
}
use of org.datanucleus.exceptions.ClassNotResolvedException in project tests by datanucleus.
the class JDOPersistenceTestCase method getPMF.
/**
* Method to obtain the PMF to use allowing specification of custom user PMF properties. Creates a new PMF on each call.
* @param userProps The custom PMF props to use when creating the PMF
* @return The PMF (also stored in the local "pmf" variable)
*/
protected static synchronized PersistenceManagerFactory getPMF(Properties userProps) {
if (pmf != null) {
if (!pmf.isClosed()) {
// Close the current PMF first
pmf.close();
}
}
pmf = JDOPersistenceTestCase.getPMF(1, userProps);
freezePMF(pmf);
// Set up the StoreManager
storeMgr = ((JDOPersistenceManagerFactory) pmf).getNucleusContext().getStoreManager();
ClassLoaderResolver clr = storeMgr.getNucleusContext().getClassLoaderResolver(null);
try {
clr.classForName("org.datanucleus.store.rdbms.RDBMSStoreManager");
if (storeMgr instanceof org.datanucleus.store.rdbms.RDBMSStoreManager) {
// RDBMS datastores have a vendor id
vendorID = ((org.datanucleus.store.rdbms.RDBMSStoreManager) storeMgr).getDatastoreAdapter().getVendorID();
}
} catch (ClassNotResolvedException cnre) {
}
return pmf;
}
Aggregations