Search in sources :

Example 16 with ClassNotResolvedException

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

the class PrimaryExpression method bind.

/**
 * Method to bind the expression to the symbol table as appropriate.
 * @param symtbl Symbol Table
 * @return The symbol for this expression
 */
public Symbol bind(SymbolTable symtbl) {
    if (left != null) {
        left.bind(symtbl);
    }
    // TODO Cater for finding field of left expression
    if (left == null && symtbl.hasSymbol(getId())) {
        symbol = symtbl.getSymbol(getId());
        if (symbol.getType() == Symbol.VARIABLE) {
            // This is a variable, so needs converting
            throw new PrimaryExpressionIsVariableException(symbol.getQualifiedName());
        }
        return symbol;
    }
    if (left != null) {
        return null;
    }
    if (symbol == null) {
        // Try with our symbol table
        try {
            Class symbolType = symtbl.getSymbolResolver().getType(tuples);
            symbol = new PropertySymbol(getId(), symbolType);
        } catch (NucleusUserException nue) {
        // Thrown if a field in the primary expression doesn't exist.
        }
    }
    if (symbol == null && symtbl.getParentSymbolTable() != null) {
        // Try parent symbol table if present
        try {
            Class symbolType = symtbl.getParentSymbolTable().getSymbolResolver().getType(tuples);
            symbol = new PropertySymbol(getId(), symbolType);
        } catch (NucleusUserException nue) {
        // Thrown if a field in the primary expression doesn't exist.
        }
    }
    if (symbol == null) {
        // This may be due to an entry like "org.jpox.samples.MyClass" used for "instanceof"
        String className = getId();
        try {
            // Try to find this as a complete class name (e.g as used in "instanceof")
            Class cls = symtbl.getSymbolResolver().resolveClass(className);
            // Represents a valid class so throw exception to get the PrimaryExpression swapped
            throw new PrimaryExpressionIsClassLiteralException(cls);
        } catch (ClassNotResolvedException cnre) {
            // Try to find classname.staticField
            if (className.indexOf('.') < 0) {
                // {candidateCls}.primary so "primary" is staticField ?
                Class primaryCls = symtbl.getSymbolResolver().getPrimaryClass();
                if (primaryCls == null) {
                    throw new NucleusUserException("Class name " + className + " could not be resolved");
                }
                try {
                    Field fld = primaryCls.getDeclaredField(className);
                    if (!Modifier.isStatic(fld.getModifiers())) {
                        throw new NucleusUserException("Identifier " + className + " is unresolved (not a static field)");
                    }
                    throw new PrimaryExpressionIsClassStaticFieldException(fld);
                } catch (NoSuchFieldException nsfe) {
                    if (symtbl.getSymbolResolver().supportsImplicitVariables() && left == null) {
                        // Implicit variable assumed so swap this primary for it
                        throw new PrimaryExpressionIsVariableException(className);
                    }
                    throw new NucleusUserException("Class name " + className + " could not be resolved");
                }
            }
            try {
                String staticFieldName = className.substring(className.lastIndexOf('.') + 1);
                className = className.substring(0, className.lastIndexOf('.'));
                Class cls = symtbl.getSymbolResolver().resolveClass(className);
                try {
                    Field fld = cls.getDeclaredField(staticFieldName);
                    if (!Modifier.isStatic(fld.getModifiers())) {
                        throw new NucleusUserException("Identifier " + className + "." + staticFieldName + " is unresolved (not a static field)");
                    }
                    throw new PrimaryExpressionIsClassStaticFieldException(fld);
                } catch (NoSuchFieldException nsfe) {
                    throw new NucleusUserException("Identifier " + className + "." + staticFieldName + " is unresolved (not a static field)");
                }
            } catch (ClassNotResolvedException cnre2) {
                if (getId().indexOf(".") > 0) {
                    Iterator<String> tupleIter = tuples.iterator();
                    Class cls = null;
                    while (tupleIter.hasNext()) {
                        String tuple = tupleIter.next();
                        if (cls == null) {
                            Symbol sym = symtbl.getSymbol(tuple);
                            if (sym == null) {
                                sym = symtbl.getSymbol("this");
                                if (sym == null) {
                                    // TODO Need to get hold of candidate alias
                                    break;
                                }
                            }
                            cls = sym.getValueType();
                        } else {
                            // Look for member of the current class
                            if (cls.isArray() && tuple.equals("length") && !tupleIter.hasNext()) {
                                // Special case of Array.length
                                PrimaryExpression primExpr = new PrimaryExpression(left, tuples.subList(0, tuples.size() - 1));
                                InvokeExpression invokeExpr = new InvokeExpression(primExpr, "size", null);
                                throw new PrimaryExpressionIsInvokeException(invokeExpr);
                            }
                            cls = ClassUtils.getClassForMemberOfClass(cls, tuple);
                        }
                    }
                    if (cls != null) {
                    // TODO Add symbol
                    }
                }
            }
            if (symtbl.getSymbolResolver().supportsImplicitVariables() && left == null) {
                // Implicit variable, so put as "left" and remove from tuples
                String varName = tuples.remove(0);
                VariableExpression varExpr = new VariableExpression(varName);
                varExpr.bind(symtbl);
                left = varExpr;
            } else {
                // Just throw the original exception
                throw new NucleusUserException("Cannot find type of (part of) " + getId() + " since symbol has no type; implicit variable?");
            }
        }
    }
    return symbol;
}
Also used : PropertySymbol(org.datanucleus.query.compiler.PropertySymbol) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) Symbol(org.datanucleus.query.compiler.Symbol) PropertySymbol(org.datanucleus.query.compiler.PropertySymbol) ClassNotResolvedException(org.datanucleus.exceptions.ClassNotResolvedException) Field(java.lang.reflect.Field)

Example 17 with ClassNotResolvedException

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

the class InMemoryExpressionEvaluator method processIsExpression.

/* (non-Javadoc)
     * @see org.datanucleus.query.evaluator.AbstractExpressionEvaluator#processIsExpression(org.datanucleus.query.expression.Expression)
     */
protected Object processIsExpression(Expression expr) {
    // field instanceof className
    Object right = stack.pop();
    Object left = stack.pop();
    if (left instanceof InMemoryFailure || right instanceof InMemoryFailure) {
        stack.push(Boolean.FALSE);
        return stack.peek();
    }
    if (!(right instanceof Class)) {
        throw new NucleusException("Attempt to invoke instanceof with argument of type " + right.getClass().getName() + " has to be Class");
    }
    try {
        Boolean result = ((Class) right).isAssignableFrom(left.getClass()) ? Boolean.TRUE : Boolean.FALSE;
        stack.push(result);
        return result;
    } catch (ClassNotResolvedException cnre) {
        throw new NucleusException("Attempt to invoke instanceof with " + right + " yet class was not found!");
    }
}
Also used : NucleusException(org.datanucleus.exceptions.NucleusException) ClassNotResolvedException(org.datanucleus.exceptions.ClassNotResolvedException)

Example 18 with ClassNotResolvedException

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

the class Imports method resolveClassDeclaration.

/**
 * Utility to resolve a class declaration.
 * @param classDecl The class declaration
 * @param clr ClassLoaderResolver
 * @param primaryClassLoader The primary ClassLoader for the class
 * @return The class
 * @throws ClassNotResolvedException If there is a problem loading the class
 * @throws NucleusUserException if a type is duplicately defined
 */
public Class resolveClassDeclaration(String classDecl, ClassLoaderResolver clr, ClassLoader primaryClassLoader) {
    boolean isArray = classDecl.indexOf('[') >= 0;
    if (isArray) {
        classDecl = classDecl.substring(0, classDecl.indexOf('['));
    }
    Class c;
    if (classDecl.indexOf('.') < 0) {
        c = primitives.get(classDecl);
        if (c == null) {
            String cd = importedClassesByName.get(classDecl);
            if (cd != null) {
                c = clr.classForName(cd, primaryClassLoader);
            }
        }
        if (c == null) {
            Iterator packageNames = importedPackageNames.iterator();
            while (packageNames.hasNext()) {
                String packageName = (String) packageNames.next();
                try {
                    Class c1 = clr.classForName(packageName + '.' + classDecl, primaryClassLoader);
                    if (c != null && c1 != null) {
                        // Duplicate definition of type
                        throw new NucleusUserException(Localiser.msg("021008", c.getName(), c1.getName()));
                    }
                    c = c1;
                } catch (ClassNotResolvedException e) {
                // Do nothing
                }
            }
            if (c == null) {
                throw new ClassNotResolvedException(classDecl);
            }
            if (NucleusLogger.GENERAL.isDebugEnabled()) {
                NucleusLogger.GENERAL.debug(Localiser.msg("021010", classDecl, c.getName()));
            }
        }
    } else {
        c = clr.classForName(classDecl, primaryClassLoader);
    }
    if (isArray) {
        c = Array.newInstance(c, 0).getClass();
    }
    return c;
}
Also used : NucleusUserException(org.datanucleus.exceptions.NucleusUserException) Iterator(java.util.Iterator) ClassNotResolvedException(org.datanucleus.exceptions.ClassNotResolvedException)

Example 19 with ClassNotResolvedException

use of org.datanucleus.exceptions.ClassNotResolvedException in project datanucleus-api-jdo by datanucleus.

the class JDOPersistenceManager method newNamedQuery.

/**
 * Construct a query instance with the candidate class and the query name.
 * @param cls The class to query
 * @param queryName Name of the query.
 * @return The query
 * @param <T> Candidate type for the query
 */
public <T> Query<T> newNamedQuery(Class<T> cls, String queryName) {
    assertIsOpen();
    // Throw exception on incomplete input
    if (queryName == null) {
        throw new JDOUserException(Localiser.msg("011005", null, cls));
    }
    // Find the Query for the specified class
    ClassLoaderResolver clr = ec.getClassLoaderResolver();
    QueryMetaData qmd = ec.getMetaDataManager().getMetaDataForQuery(cls, clr, queryName);
    if (qmd == null) {
        throw new JDOUserException(Localiser.msg("011005", queryName, cls));
    }
    // Create the Query
    Query query = newQuery(qmd.getLanguage(), qmd.getQuery());
    if (cls != null) {
        query.setClass(cls);
        if (!ec.getStoreManager().managesClass(cls.getName())) {
            // Load the candidate class since not yet managed
            ec.getStoreManager().manageClasses(clr, cls.getName());
        }
    }
    // Optional args that should only be used with SQL
    if (qmd.getLanguage().equals(QueryLanguage.JDOQL.toString()) && (qmd.isUnique() || qmd.getResultClass() != null)) {
        throw new JDOUserException(Localiser.msg("011007", queryName));
    }
    if (qmd.isUnique()) {
        query.setUnique(true);
    }
    if (qmd.getResultClass() != null) {
        // Set the result class, allowing for it being in the same package as the candidate
        Class resultCls = null;
        try {
            resultCls = clr.classForName(qmd.getResultClass());
        } catch (ClassNotResolvedException cnre) {
            if (cls != null) {
                try {
                    String resultClassName = cls.getPackage().getName() + "." + qmd.getResultClass();
                    resultCls = clr.classForName(resultClassName);
                } catch (ClassNotResolvedException cnre2) {
                    throw new JDOUserException(Localiser.msg("011008", queryName, qmd.getResultClass()));
                }
            }
        }
        query.setResultClass(resultCls);
    }
    // Add any extensions
    Map<String, String> extmds = qmd.getExtensions();
    if (extmds != null) {
        Iterator<Entry<String, String>> entryIter = extmds.entrySet().iterator();
        while (entryIter.hasNext()) {
            Entry<String, String> entry = entryIter.next();
            query.addExtension(entry.getKey(), entry.getValue());
        }
    }
    if (qmd.isUnmodifiable()) {
        query.setUnmodifiable();
    }
    if (qmd.getFetchPlanName() != null) {
        // Apply any named FetchPlan to the query
        FetchPlanMetaData fpmd = ec.getMetaDataManager().getMetaDataForFetchPlan(qmd.getFetchPlanName());
        if (fpmd != null) {
            org.datanucleus.FetchPlan fp = new org.datanucleus.FetchPlan(ec, clr);
            fp.removeGroup(org.datanucleus.FetchPlan.DEFAULT);
            FetchGroupMetaData[] fgmds = fpmd.getFetchGroupMetaData();
            for (FetchGroupMetaData fgmd : fgmds) {
                fp.addGroup(fgmd.getName());
            }
            fp.setMaxFetchDepth(fpmd.getMaxFetchDepth());
            fp.setFetchSize(fpmd.getFetchSize());
            ((JDOQuery) query).getInternalQuery().setFetchPlan(fp);
        }
    }
    return query;
}
Also used : JDOQLTypedQuery(javax.jdo.JDOQLTypedQuery) Query(javax.jdo.Query) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) FetchPlanMetaData(org.datanucleus.metadata.FetchPlanMetaData) FetchPlan(javax.jdo.FetchPlan) JDOUserException(javax.jdo.JDOUserException) ClassNotResolvedException(org.datanucleus.exceptions.ClassNotResolvedException) QueryMetaData(org.datanucleus.metadata.QueryMetaData) Entry(java.util.Map.Entry) FetchGroupMetaData(org.datanucleus.metadata.FetchGroupMetaData)

Example 20 with ClassNotResolvedException

use of org.datanucleus.exceptions.ClassNotResolvedException in project datanucleus-api-jdo by datanucleus.

the class JDOPersistenceManager method getObjectIdClass.

/**
 * Accessor for the class of the object id given the class of object.
 * @param cls The class name of the object
 * @return The class name of the object id
 */
public Class getObjectIdClass(Class cls) {
    assertIsOpen();
    if (!ec.getNucleusContext().getApiAdapter().isPersistable(cls) || !hasPersistenceInformationForClass(cls)) {
        return null;
    }
    ClassLoaderResolver clr = ec.getClassLoaderResolver();
    AbstractClassMetaData cmd = ec.getMetaDataManager().getMetaDataForClass(cls, clr);
    if (cmd.getIdentityType() == IdentityType.DATASTORE) {
        return ec.getNucleusContext().getIdentityManager().getDatastoreIdClass();
    } else if (cmd.getIdentityType() == IdentityType.APPLICATION) {
        try {
            return this.ec.getClassLoaderResolver().classForName(ec.getMetaDataManager().getMetaDataForClass(cls, clr).getObjectidClass(), null);
        } catch (ClassNotResolvedException e) {
            String msg = Localiser.msg("011009", cls.getName());
            LOGGER.error(msg);
            throw new JDOException(msg);
        }
    } else {
        if (cmd.isRequiresExtent()) {
            return ec.getNucleusContext().getIdentityManager().getDatastoreIdClass();
        }
        return SCOID.class;
    }
}
Also used : ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) ClassNotResolvedException(org.datanucleus.exceptions.ClassNotResolvedException) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) JDOException(javax.jdo.JDOException)

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