Search in sources :

Example 1 with QueryException

use of com.hazelcast.query.QueryException in project hazelcast by hazelcast.

the class PredicateBuilderImpl method and.

@Override
public PredicateBuilder and(Predicate predicate) {
    if (predicate != PredicateBuilderImpl.this) {
        throw new QueryException("Illegal and statement expected: " + PredicateBuilderImpl.class.getSimpleName() + ", found: " + ((predicate == null) ? "null" : predicate.getClass().getSimpleName()));
    }
    int index = lsPredicates.size() - 2;
    Predicate first = lsPredicates.remove(index);
    Predicate second = lsPredicates.remove(index);
    return addPredicate(Predicates.and(first, second));
}
Also used : QueryException(com.hazelcast.query.QueryException) IndexAwarePredicate(com.hazelcast.query.impl.predicates.IndexAwarePredicate) VisitablePredicate(com.hazelcast.query.impl.predicates.VisitablePredicate) Predicate(com.hazelcast.query.Predicate)

Example 2 with QueryException

use of com.hazelcast.query.QueryException in project hazelcast by hazelcast.

the class ReflectionHelper method createGetter.

public static Getter createGetter(Object obj, String attribute) {
    if (obj == null || obj == IndexImpl.NULL) {
        return NULL_GETTER;
    }
    final Class targetClazz = obj.getClass();
    Class clazz = targetClazz;
    Getter getter;
    try {
        Getter parent = null;
        List<String> possibleMethodNames = new ArrayList<String>(INITIAL_CAPACITY);
        for (final String fullname : attribute.split("\\.")) {
            String baseName = removeModifierSuffix(fullname);
            String modifier = getModifierSuffix(fullname, baseName);
            Getter localGetter = null;
            possibleMethodNames.clear();
            possibleMethodNames.add(baseName);
            final String camelName = Character.toUpperCase(baseName.charAt(0)) + baseName.substring(1);
            possibleMethodNames.add("get" + camelName);
            possibleMethodNames.add("is" + camelName);
            if (baseName.equals(THIS_ATTRIBUTE_NAME.value())) {
                localGetter = GetterFactory.newThisGetter(parent, obj);
            } else {
                if (parent != null) {
                    clazz = parent.getReturnType();
                }
                for (String methodName : possibleMethodNames) {
                    try {
                        final Method method = clazz.getMethod(methodName);
                        method.setAccessible(true);
                        localGetter = GetterFactory.newMethodGetter(obj, parent, method, modifier);
                        if (localGetter == NULL_GETTER) {
                            return localGetter;
                        }
                        clazz = method.getReturnType();
                        break;
                    } catch (NoSuchMethodException ignored) {
                        EmptyStatement.ignore(ignored);
                    }
                }
                if (localGetter == null) {
                    try {
                        final Field field = clazz.getField(baseName);
                        localGetter = GetterFactory.newFieldGetter(obj, parent, field, modifier);
                        if (localGetter == NULL_GETTER) {
                            return localGetter;
                        }
                        clazz = field.getType();
                    } catch (NoSuchFieldException ignored) {
                        EmptyStatement.ignore(ignored);
                    }
                }
                if (localGetter == null) {
                    Class c = clazz;
                    while (!c.isInterface() && !Object.class.equals(c)) {
                        try {
                            final Field field = c.getDeclaredField(baseName);
                            field.setAccessible(true);
                            localGetter = GetterFactory.newFieldGetter(obj, parent, field, modifier);
                            if (localGetter == NULL_GETTER) {
                                return NULL_GETTER;
                            }
                            clazz = field.getType();
                            break;
                        } catch (NoSuchFieldException ignored) {
                            c = c.getSuperclass();
                        }
                    }
                }
            }
            if (localGetter == null) {
                throw new IllegalArgumentException("There is no suitable accessor for '" + baseName + "' on class '" + clazz + "'");
            }
            parent = localGetter;
        }
        getter = parent;
        return getter;
    } catch (Throwable e) {
        throw new QueryException(e);
    }
}
Also used : Field(java.lang.reflect.Field) QueryException(com.hazelcast.query.QueryException) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method)

Example 3 with QueryException

use of com.hazelcast.query.QueryException in project hazelcast by hazelcast.

the class ReflectionHelper method createGetter.

public static Getter createGetter(Object obj, String attribute, boolean failOnMissingAttribute) {
    if (obj == null || obj == NULL) {
        return NULL_GETTER;
    }
    final Class targetClazz = obj.getClass();
    Class clazz = targetClazz;
    Getter getter;
    try {
        Getter parent = null;
        List<String> possibleMethodNames = new ArrayList<String>(INITIAL_CAPACITY);
        for (final String fullname : attribute.split("\\.")) {
            String baseName = removeModifierSuffix(fullname);
            String modifier = getModifierSuffix(fullname, baseName);
            Getter localGetter = null;
            possibleMethodNames.clear();
            possibleMethodNames.add(baseName);
            final String camelName = Character.toUpperCase(baseName.charAt(0)) + baseName.substring(1);
            possibleMethodNames.add("get" + camelName);
            possibleMethodNames.add("is" + camelName);
            if (baseName.equals(THIS_ATTRIBUTE_NAME.value())) {
                localGetter = GetterFactory.newThisGetter(parent, obj);
            } else {
                if (parent != null) {
                    clazz = parent.getReturnType();
                }
                for (String methodName : possibleMethodNames) {
                    try {
                        final Method method = clazz.getMethod(methodName);
                        method.setAccessible(true);
                        localGetter = GetterFactory.newMethodGetter(obj, parent, method, modifier);
                        if (localGetter == NULL_GETTER || localGetter == NULL_MULTIVALUE_GETTER) {
                            return localGetter;
                        }
                        clazz = method.getReturnType();
                        break;
                    } catch (NoSuchMethodException ignored) {
                        ignore(ignored);
                    }
                }
                if (localGetter == null) {
                    try {
                        final Field field = clazz.getField(baseName);
                        localGetter = GetterFactory.newFieldGetter(obj, parent, field, modifier);
                        if (localGetter == NULL_GETTER || localGetter == NULL_MULTIVALUE_GETTER) {
                            return localGetter;
                        }
                        clazz = field.getType();
                    } catch (NoSuchFieldException ignored) {
                        ignore(ignored);
                    }
                }
                if (localGetter == null) {
                    Class c = clazz;
                    while (!c.isInterface() && !Object.class.equals(c)) {
                        try {
                            final Field field = c.getDeclaredField(baseName);
                            field.setAccessible(true);
                            localGetter = GetterFactory.newFieldGetter(obj, parent, field, modifier);
                            if (localGetter == NULL_GETTER || localGetter == NULL_MULTIVALUE_GETTER) {
                                return localGetter;
                            }
                            clazz = field.getType();
                            break;
                        } catch (NoSuchFieldException ignored) {
                            c = c.getSuperclass();
                        }
                    }
                }
            }
            if (localGetter == null) {
                if (failOnMissingAttribute) {
                    throw new IllegalArgumentException("There is no suitable accessor for '" + baseName + "' on class '" + clazz.getName() + "'");
                } else {
                    return NULL_GETTER;
                }
            }
            parent = localGetter;
        }
        getter = parent;
        return getter;
    } catch (Throwable e) {
        throw new QueryException(e);
    }
}
Also used : Field(java.lang.reflect.Field) QueryException(com.hazelcast.query.QueryException) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method)

Example 4 with QueryException

use of com.hazelcast.query.QueryException in project hazelcast by hazelcast.

the class ParallelPartitionScanExecutorTest method execute_fail.

@Test
public void execute_fail() {
    PartitionScanRunner runner = mock(PartitionScanRunner.class);
    ParallelPartitionScanExecutor executor = executor(runner);
    Predicate predicate = Predicates.equal("attribute", 1);
    QueryResult queryResult = new QueryResult(IterationType.ENTRY, null, null, Long.MAX_VALUE, false);
    doThrow(new QueryException()).when(runner).run(anyString(), eq(predicate), anyInt(), isA(QueryResult.class));
    expected.expect(QueryException.class);
    executor.execute("Map", predicate, asList(1, 2, 3), queryResult);
}
Also used : QueryException(com.hazelcast.query.QueryException) Predicate(com.hazelcast.query.Predicate) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 5 with QueryException

use of com.hazelcast.query.QueryException in project hazelcast by hazelcast.

the class CallerRunsPartitionScanExecutorTest method execute_fail.

@Test
public void execute_fail() {
    PartitionScanRunner runner = mock(PartitionScanRunner.class);
    CallerRunsPartitionScanExecutor executor = new CallerRunsPartitionScanExecutor(runner);
    Predicate predicate = Predicates.equal("attribute", 1);
    QueryResult queryResult = new QueryResult(IterationType.ENTRY, null, null, Long.MAX_VALUE, false);
    doThrow(new QueryException()).when(runner).run(anyString(), eq(predicate), anyInt(), eq(queryResult));
    expected.expect(QueryException.class);
    executor.execute("Map", predicate, asList(1, 2, 3), queryResult);
}
Also used : QueryException(com.hazelcast.query.QueryException) Predicate(com.hazelcast.query.Predicate) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

QueryException (com.hazelcast.query.QueryException)5 Predicate (com.hazelcast.query.Predicate)3 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)2 QuickTest (com.hazelcast.test.annotation.QuickTest)2 Field (java.lang.reflect.Field)2 Method (java.lang.reflect.Method)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 IndexAwarePredicate (com.hazelcast.query.impl.predicates.IndexAwarePredicate)1 VisitablePredicate (com.hazelcast.query.impl.predicates.VisitablePredicate)1