Search in sources :

Example 31 with ClassNotResolvedException

use of org.datanucleus.exceptions.ClassNotResolvedException in project datanucleus-core by datanucleus.

the class KeyMetaData method populate.

/**
 * Populate the MetaData.
 * @param clr Class loader to use
 * @param primary the primary ClassLoader to use (or null)
 */
public void populate(ClassLoaderResolver clr, ClassLoader primary) {
    AbstractMemberMetaData mmd = (AbstractMemberMetaData) parent;
    if (mmd.getMap() == null) {
        // TODO Use InvalidMemberMetaDataException
        throw new NucleusUserException("The field " + mmd.getFullFieldName() + " is defined with <key>, however no <map> definition was found.").setFatal();
    }
    // Populate the key metadata
    if (hasExtension(MetaData.EXTENSION_MEMBER_TYPE_CONVERTER_NAME)) {
        if (mmd.getMap().key.embedded == null) {
            // Default to embedded since the converter process requires it
            mmd.getMap().key.embedded = Boolean.TRUE;
        }
    }
    mmd.getMap().key.populate(mmd.getAbstractClassMetaData().getPackageName(), clr, primary);
    // Make sure key type is set and is valid
    String keyType = mmd.getMap().getKeyType();
    Class keyTypeClass = null;
    try {
        keyTypeClass = clr.classForName(keyType, primary);
    } catch (ClassNotResolvedException cnre) {
        throw new InvalidMemberMetaDataException("044147", mmd.getClassName(), mmd.getName(), keyType);
    }
    if (embeddedMetaData != null && (keyTypeClass.isInterface() || keyTypeClass.getName().equals("java.lang.Object"))) {
        throw new InvalidMemberMetaDataException("044152", mmd.getClassName(), mmd.getName(), keyTypeClass.getName());
    }
    // that MapMetaData can call when it is populated
    if (embeddedMetaData == null && mmd.hasMap() && mmd.getMap().isEmbeddedKey() && mmd.getJoinMetaData() != null && mmd.getMap().keyIsPersistent()) {
        // User has specified that the key is embedded in a join table but not how we embed it
        // so add a dummy definition
        embeddedMetaData = new EmbeddedMetaData();
        embeddedMetaData.parent = this;
    }
    super.populate(clr, primary);
}
Also used : NucleusUserException(org.datanucleus.exceptions.NucleusUserException) ClassNotResolvedException(org.datanucleus.exceptions.ClassNotResolvedException)

Example 32 with ClassNotResolvedException

use of org.datanucleus.exceptions.ClassNotResolvedException in project datanucleus-core by datanucleus.

the class DataNucleusEnhancer method addClasses.

/**
 * Method to add the specified classes to the list of classes to enhance.
 * @param classNames Names of the classes
 * @return The enhancer
 */
public DataNucleusEnhancer addClasses(String... classNames) {
    if (classNames == null) {
        return this;
    }
    Collection names = new HashSet();
    for (int i = 0; i < classNames.length; i++) {
        if (classNames[i].endsWith(".class")) {
            // Absolute/relative class file (should really be via addFiles())
            String name = classNames[i];
            String msg = null;
            if (!StringUtils.getFileForFilename(classNames[i]).exists()) {
                msg = Localiser.msg("005003", classNames[i]);
                addMessage(msg, true);
                name = null;
            } else {
                // Extra the name of the class from the name of the file
                name = ClassEnhancerImpl.getClassNameForFileName(classNames[i]);
            }
            if (name != null) {
                names.add(name);
            }
        } else {
            // Assumed to be actual class name ("mydomain.MyClass")
            try {
                // Check for class existence
                clr.classForName(classNames[i], false);
            } catch (ClassNotResolvedException cnre) {
                addMessage("Class " + classNames[i] + " not found in CLASSPATH! : " + cnre.getMessage(), true);
            }
            names.add(classNames[i]);
        }
    }
    if (!names.isEmpty()) {
        componentsToEnhance.add(new EnhanceComponent(EnhanceComponent.CLASS, names.toArray(new String[names.size()])));
    }
    return this;
}
Also used : Collection(java.util.Collection) ClassNotResolvedException(org.datanucleus.exceptions.ClassNotResolvedException) HashSet(java.util.HashSet)

Example 33 with ClassNotResolvedException

use of org.datanucleus.exceptions.ClassNotResolvedException in project tests by datanucleus.

the class JPAPersistenceTestCase method getEMF.

/**
 * Method to obtain the EMF to use allowing specification of custom user EMF properties.
 * Creates a new EMF on each call.
 * NOTE : THIS SETS THE "emf" AND "storeMgr" FIELDS.
 * @param unitName Name of the persistence unit to use (if any)
 * @param userProps The custom EMF props to use when creating the EMF
 * @return The EMF (also stored in the local "emf" variable)
 */
protected synchronized EntityManagerFactory getEMF(String unitName, Properties userProps) {
    emf = getEMF(1, unitName, userProps);
    storeMgr = emf.unwrap(PersistenceNucleusContext.class).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 emf;
}
Also used : ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) ClassNotResolvedException(org.datanucleus.exceptions.ClassNotResolvedException)

Example 34 with ClassNotResolvedException

use of org.datanucleus.exceptions.ClassNotResolvedException in project datanucleus-rdbms by datanucleus.

the class QueryToSQLMapper method processCreatorExpression.

/* (non-Javadoc)
     * @see org.datanucleus.query.evaluator.AbstractExpressionEvaluator#processCreatorExpression(org.datanucleus.query.expression.CreatorExpression)
     */
@Override
protected Object processCreatorExpression(CreatorExpression expr) {
    String className = expr.getId();
    Class cls = null;
    try {
        cls = clr.classForName(className);
    } catch (ClassNotResolvedException cnre) {
        if (importsDefinition != null) {
            cls = importsDefinition.resolveClassDeclaration(className, clr, null);
        }
    }
    List<SQLExpression> ctrArgExprs = null;
    List<String> ctrArgAliases = null;
    boolean hasAliases = false;
    List args = expr.getArguments();
    if (args != null) {
        Class[] ctrArgTypes = new Class[args.size()];
        boolean[] ctrArgTypeCheck = new boolean[args.size()];
        ctrArgExprs = new ArrayList<>(args.size());
        ctrArgAliases = new ArrayList<>(args.size());
        Iterator iter = args.iterator();
        int i = 0;
        while (iter.hasNext()) {
            Expression argExpr = (Expression) iter.next();
            String argAlias = argExpr.getAlias();
            if (argAlias != null) {
                hasAliases = true;
            }
            SQLExpression sqlExpr = (SQLExpression) evaluate(argExpr);
            // TODO Cater for "SQL_function" mapping on to ANY type of constructor argument at that position since we don't know the precise type
            if (argExpr instanceof InvokeExpression && ((InvokeExpression) argExpr).getOperation().equalsIgnoreCase("SQL_function")) {
                ctrArgTypeCheck[i] = false;
            } else {
                ctrArgTypeCheck[i] = true;
            }
            ctrArgExprs.add(sqlExpr);
            ctrArgAliases.add(argAlias != null ? argAlias : "####");
            if (sqlExpr instanceof NewObjectExpression) {
                ctrArgTypes[i] = ((NewObjectExpression) sqlExpr).getNewClass();
            } else if (sqlExpr.getJavaTypeMapping() instanceof DatastoreIdMapping || sqlExpr.getJavaTypeMapping() instanceof PersistableMapping) {
                ctrArgTypes[i] = clr.classForName(sqlExpr.getJavaTypeMapping().getType());
            } else {
                ctrArgTypes[i] = sqlExpr.getJavaTypeMapping().getJavaType();
            }
            i++;
        }
        // Check that this class has the required constructor
        Constructor ctr = ClassUtils.getConstructorWithArguments(cls, ctrArgTypes, ctrArgTypeCheck);
        if (ctr == null) {
            throw new NucleusUserException(Localiser.msg("021033", className, StringUtils.objectArrayToString(ctrArgTypes)));
        }
    }
    // TODO Retain the selected constructor (above)
    NewObjectExpression newExpr = new NewObjectExpression(stmt, cls, ctrArgExprs);
    if (hasAliases) {
        newExpr.setArgAliases(ctrArgAliases);
    }
    stack.push(newExpr);
    return newExpr;
}
Also used : InvokeExpression(org.datanucleus.store.query.expression.InvokeExpression) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) Constructor(java.lang.reflect.Constructor) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) ClassNotResolvedException(org.datanucleus.exceptions.ClassNotResolvedException) NewObjectExpression(org.datanucleus.store.rdbms.sql.expression.NewObjectExpression) PersistableMapping(org.datanucleus.store.rdbms.mapping.java.PersistableMapping) DatastoreIdMapping(org.datanucleus.store.rdbms.mapping.java.DatastoreIdMapping) PrimaryExpression(org.datanucleus.store.query.expression.PrimaryExpression) BooleanSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.BooleanSubqueryExpression) TypeConverterExpression(org.datanucleus.store.rdbms.sql.expression.TypeConverterExpression) StringExpression(org.datanucleus.store.rdbms.sql.expression.StringExpression) NumericSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.NumericSubqueryExpression) StringSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.StringSubqueryExpression) MapExpression(org.datanucleus.store.rdbms.sql.expression.MapExpression) ArrayExpression(org.datanucleus.store.query.expression.ArrayExpression) CaseExpression(org.datanucleus.store.query.expression.CaseExpression) CreatorExpression(org.datanucleus.store.query.expression.CreatorExpression) NewObjectExpression(org.datanucleus.store.rdbms.sql.expression.NewObjectExpression) OrderExpression(org.datanucleus.store.query.expression.OrderExpression) TemporalSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.TemporalSubqueryExpression) ClassExpression(org.datanucleus.store.query.expression.ClassExpression) BooleanExpression(org.datanucleus.store.rdbms.sql.expression.BooleanExpression) Expression(org.datanucleus.store.query.expression.Expression) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) ParameterExpression(org.datanucleus.store.query.expression.ParameterExpression) UnboundExpression(org.datanucleus.store.rdbms.sql.expression.UnboundExpression) TemporalExpression(org.datanucleus.store.rdbms.sql.expression.TemporalExpression) SubqueryExpression(org.datanucleus.store.query.expression.SubqueryExpression) ResultAliasExpression(org.datanucleus.store.rdbms.sql.expression.ResultAliasExpression) VariableExpression(org.datanucleus.store.query.expression.VariableExpression) TypeExpression(org.datanucleus.store.query.expression.TypeExpression) NumericExpression(org.datanucleus.store.rdbms.sql.expression.NumericExpression) CollectionExpression(org.datanucleus.store.rdbms.sql.expression.CollectionExpression) DyadicExpression(org.datanucleus.store.query.expression.DyadicExpression) ColumnExpression(org.datanucleus.store.rdbms.sql.expression.ColumnExpression) JoinExpression(org.datanucleus.store.query.expression.JoinExpression) InvokeExpression(org.datanucleus.store.query.expression.InvokeExpression) ListIterator(java.util.ListIterator) Iterator(java.util.Iterator) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) FetchPlanForClass(org.datanucleus.FetchPlanForClass) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

ClassNotResolvedException (org.datanucleus.exceptions.ClassNotResolvedException)34 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)18 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)13 NucleusException (org.datanucleus.exceptions.NucleusException)10 HashSet (java.util.HashSet)8 ArrayList (java.util.ArrayList)6 Iterator (java.util.Iterator)6 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)6 IOException (java.io.IOException)5 Collection (java.util.Collection)5 Method (java.lang.reflect.Method)4 List (java.util.List)4 ApiAdapter (org.datanucleus.api.ApiAdapter)4 Field (java.lang.reflect.Field)3 NucleusObjectNotFoundException (org.datanucleus.exceptions.NucleusObjectNotFoundException)3 SCOID (org.datanucleus.identity.SCOID)3 ObjectProvider (org.datanucleus.state.ObjectProvider)3 Constructor (java.lang.reflect.Constructor)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 URL (java.net.URL)2