
com.querydsl.collections.CollQuerySerializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of querydsl-collections Show documentation
Show all versions of querydsl-collections Show documentation
Collections support for Querydsl
The newest version!
/*
* Copyright 2015, The Querydsl Team (http://www.querydsl.com/team)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.querydsl.collections;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import com.querydsl.codegen.Serializer;
import com.querydsl.core.QueryException;
import com.querydsl.core.support.SerializerBase;
import com.querydsl.core.types.*;
import com.querydsl.core.util.PrimitiveUtils;
/**
* {@code CollQuerySerializer} is a {@link Serializer} implementation for the Java language
*
* @author tiwe
*/
public final class CollQuerySerializer extends SerializerBase {
private static final Map OPERATOR_SYMBOLS = new IdentityHashMap<>();
private static final Map, String> CAST_SUFFIXES = new HashMap<>();
static {
OPERATOR_SYMBOLS.put(Ops.EQ, " == ");
OPERATOR_SYMBOLS.put(Ops.NE, " != ");
OPERATOR_SYMBOLS.put(Ops.GT, " > ");
OPERATOR_SYMBOLS.put(Ops.LT, " < ");
OPERATOR_SYMBOLS.put(Ops.GOE, " >= ");
OPERATOR_SYMBOLS.put(Ops.LOE, " <= ");
OPERATOR_SYMBOLS.put(Ops.ADD, " + ");
OPERATOR_SYMBOLS.put(Ops.SUB, " - ");
OPERATOR_SYMBOLS.put(Ops.MULT, " * ");
OPERATOR_SYMBOLS.put(Ops.DIV, " / ");
CAST_SUFFIXES.put(Boolean.class, ".booleanValue()");
CAST_SUFFIXES.put(Byte.class, ".byteValue()");
CAST_SUFFIXES.put(Character.class, ".charValue()");
CAST_SUFFIXES.put(Double.class, ".doubleValue()");
CAST_SUFFIXES.put(Float.class, ".floatValue()");
CAST_SUFFIXES.put(Integer.class, ".intValue()");
CAST_SUFFIXES.put(Long.class, ".longValue()");
CAST_SUFFIXES.put(Short.class, ".shortValue()");
CAST_SUFFIXES.put(String.class, ".toString()");
}
public CollQuerySerializer(CollQueryTemplates templates) {
super(templates);
}
@Override
public Void visit(Path> path, Void context) {
final PathType pathType = path.getMetadata().getPathType();
if (pathType == PathType.PROPERTY) {
final Path> parent = path.getMetadata().getParent();
final String property = path.getMetadata().getName();
final Class> parentType = parent.getType();
try {
// getter
Method m = getAccessor(parentType, property);
if (m != null && Modifier.isPublic(m.getModifiers())) {
handle(parent);
append(".").append(m.getName()).append("()");
} else {
// field
Field f = getField(parentType, property);
if (f != null && Modifier.isPublic(f.getModifiers())) {
handle(parent);
append(".").append(property);
} else {
// field access by reflection
append(CollQueryFunctions.class.getName() + ".<");
append((path.getType()).getName()).append(">get(");
handle(parent);
append(", \"" + property + "\")");
}
}
} catch (Exception e) {
throw new QueryException(e);
}
} else if (pathType == PathType.DELEGATE) {
append("(");
append("(").append(path.getType().getName()).append(")");
path.getMetadata().getParent().accept(this, context);
append(")");
} else {
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy