From bd8f8994784969f20df9f61aec2281dff92b7a4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20Westk=C3=A4mper?= Date: Fri, 3 Oct 2014 21:10:41 +0300 Subject: [PATCH] Add PathBuilderValidator --- .../mysema/query/types/path/PathBuilder.java | 85 ++++++++++++------- .../types/path/PathBuilderValidator.java | 76 +++++++++++++++++ 2 files changed, 130 insertions(+), 31 deletions(-) create mode 100644 querydsl-core/src/main/java/com/mysema/query/types/path/PathBuilderValidator.java diff --git a/querydsl-core/src/main/java/com/mysema/query/types/path/PathBuilder.java b/querydsl-core/src/main/java/com/mysema/query/types/path/PathBuilder.java index 4d414fc3c..19df7d9ec 100644 --- a/querydsl-core/src/main/java/com/mysema/query/types/path/PathBuilder.java +++ b/querydsl-core/src/main/java/com/mysema/query/types/path/PathBuilder.java @@ -13,7 +13,11 @@ */ package com.mysema.query.types.path; +import java.lang.reflect.Array; +import java.util.Collection; +import java.util.List; import java.util.Map; +import java.util.Set; import com.google.common.collect.Maps; import com.mysema.query.types.EntityPath; @@ -44,6 +48,19 @@ public class PathBuilder extends EntityPathBase { private final Map, Object> propertyMetadata = Maps.newHashMap(); + private final PathBuilderValidator validator; + + /** + * Creates a new PathBuilder instance + * + * @param type + * @param pathMetadata + */ + public PathBuilder(Class type, PathMetadata pathMetadata, PathBuilderValidator validator) { + super(type, pathMetadata); + this.validator = validator; + } + /** * Creates a new PathBuilder instance * @@ -51,7 +68,18 @@ public class PathBuilder extends EntityPathBase { * @param pathMetadata */ public PathBuilder(Class type, PathMetadata pathMetadata) { - super(type, pathMetadata); + this(type, pathMetadata, PathBuilderValidator.DEFAULT); + } + + + /** + * Creates a new PathBuilder instance + * + * @param type + * @param variable + */ + public PathBuilder(Class type, String variable, PathBuilderValidator validator) { + this(type, PathMetadataFactory.forVariable(variable), validator); } /** @@ -61,7 +89,7 @@ public class PathBuilder extends EntityPathBase { * @param variable */ public PathBuilder(Class type, String variable) { - super(type, PathMetadataFactory.forVariable(variable)); + this(type, PathMetadataFactory.forVariable(variable), PathBuilderValidator.DEFAULT); } /** @@ -77,13 +105,8 @@ public class PathBuilder extends EntityPathBase { return newPath; } - /** - * Override this method to do some validation of the properties created - * - * @param property - */ - protected void validate(String property) { - // do nothing + private void validate(String property, Class propertyType) { + validator.validate(getType(), property, propertyType); } @Override @@ -101,8 +124,8 @@ public class PathBuilder extends EntityPathBase { public PathBuilder get(String property) { PathBuilder path = (PathBuilder) properties.get(property); if (path == null) { - validate(property); - path = new PathBuilder(Object.class, forProperty(property)); + validate(property, Object.class); + path = new PathBuilder(Object.class, forProperty(property), validator); properties.put(property, path); } return path; @@ -120,8 +143,8 @@ public class PathBuilder extends EntityPathBase { public PathBuilder get(String property, Class type) { PathBuilder path = (PathBuilder) properties.get(property); if (path == null || !type.isAssignableFrom(path.getType())) { - validate(property); - path = new PathBuilder(type, forProperty(property)); + validate(property, type); + path = new PathBuilder(type, forProperty(property), validator); properties.put(property, path); } return path; @@ -137,7 +160,7 @@ public class PathBuilder extends EntityPathBase { * @return */ public ArrayPath getArray(String property, Class type) { - validate(property); + validate(property, Array.newInstance(type, 0).getClass()); return super.createArray(property, type); } @@ -157,7 +180,7 @@ public class PathBuilder extends EntityPathBase { * @return */ public BooleanPath getBoolean(String propertyName) { - validate(propertyName); + validate(propertyName, Boolean.class); return super.createBoolean(propertyName); } @@ -170,7 +193,7 @@ public class PathBuilder extends EntityPathBase { * @return */ public CollectionPath> getCollection(String property, Class type) { - validate(property); + validate(property, Collection.class); return super.>createCollection(property, type, PathBuilder.class, PathInits.DIRECT); } @@ -185,7 +208,7 @@ public class PathBuilder extends EntityPathBase { * @return */ public > CollectionPath getCollection(String property, Class type, Class queryType) { - validate(property); + validate(property, Collection.class); return super.createCollection(property, type, queryType, PathInits.DIRECT); } @@ -209,7 +232,7 @@ public class PathBuilder extends EntityPathBase { * @return */ public > ComparablePath getComparable(String property, Class type) { - validate(property); + validate(property, type); return super.createComparable(property, type); } @@ -233,7 +256,7 @@ public class PathBuilder extends EntityPathBase { * @return */ public > DatePath getDate(String property, Class type) { - validate(property); + validate(property, type); return super.createDate(property, type); } @@ -257,7 +280,7 @@ public class PathBuilder extends EntityPathBase { * @return */ public > DateTimePath getDateTime(String property, Class type) { - validate(property); + validate(property, type); return super.createDateTime(property, type); } @@ -270,7 +293,7 @@ public class PathBuilder extends EntityPathBase { * @return */ public > EnumPath getEnum(String property, Class type) { - validate(property); + validate(property, type); return super.createEnum(property, type); } @@ -294,7 +317,7 @@ public class PathBuilder extends EntityPathBase { * @return */ public ListPath> getList(String property, Class type) { - validate(property); + validate(property, List.class); return super.>createList(property, type, PathBuilder.class, PathInits.DIRECT); } @@ -309,7 +332,7 @@ public class PathBuilder extends EntityPathBase { * @return */ public > ListPath getList(String property, Class type, Class queryType) { - validate(property); + validate(property, List.class); return super.createList(property, type, queryType, PathInits.DIRECT); } @@ -324,7 +347,7 @@ public class PathBuilder extends EntityPathBase { * @return */ public MapPath> getMap(String property, Class key, Class value) { - validate(property); + validate(property, Map.class); return super.>createMap(property, key, value, PathBuilder.class); } @@ -341,7 +364,7 @@ public class PathBuilder extends EntityPathBase { * @return */ public > MapPath getMap(String property, Class key, Class value, Class queryType) { - validate(property); + validate(property, Map.class); return super.createMap(property, key, value, queryType); } @@ -365,7 +388,7 @@ public class PathBuilder extends EntityPathBase { * @return */ public > NumberPath getNumber(String property, Class type) { - validate(property); + validate(property, type); return super.createNumber(property, type); } @@ -378,7 +401,7 @@ public class PathBuilder extends EntityPathBase { * @return */ public SetPath> getSet(String property, Class type) { - validate(property); + validate(property, type); return super.>createSet(property, type, PathBuilder.class, PathInits.DIRECT); } @@ -393,7 +416,7 @@ public class PathBuilder extends EntityPathBase { * @return */ public > SetPath getSet(String property, Class type, Class queryType) { - validate(property); + validate(property, Set.class); return super.createSet(property, type, queryType, PathInits.DIRECT); } @@ -417,7 +440,7 @@ public class PathBuilder extends EntityPathBase { * @return */ public SimplePath getSimple(String property, Class type) { - validate(property); + validate(property, type); return super.createSimple(property, type); } @@ -437,7 +460,7 @@ public class PathBuilder extends EntityPathBase { * @return */ public StringPath getString(String property) { - validate(property); + validate(property, String.class); return super.createString(property); } @@ -460,7 +483,7 @@ public class PathBuilder extends EntityPathBase { * @return */ public > TimePath getTime(String property, Class type) { - validate(property); + validate(property, type); return super.createTime(property, type); } diff --git a/querydsl-core/src/main/java/com/mysema/query/types/path/PathBuilderValidator.java b/querydsl-core/src/main/java/com/mysema/query/types/path/PathBuilderValidator.java new file mode 100644 index 000000000..5974d13b5 --- /dev/null +++ b/querydsl-core/src/main/java/com/mysema/query/types/path/PathBuilderValidator.java @@ -0,0 +1,76 @@ +/* + * Copyright 2014, Timo Westkämper + * + * 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.mysema.query.types.path; + +import com.sun.xml.internal.ws.util.StringUtils; + +/** + * PathBuilderValidator validates PathBuilder properties at creation time + */ +public interface PathBuilderValidator { + + /** + * Validates the given property of given class + * + * @param parent + * @param property + * @param propertyType + */ + void validate(Class parent, String property, Class propertyType); + + public final PathBuilderValidator DEFAULT = new PathBuilderValidator() { + @Override + public void validate(Class parent, String property, Class propertyType) { + // do nothing + } + }; + + public final PathBuilderValidator FIELDS = new PathBuilderValidator() { + @Override + public void validate(Class parent, String property, Class propertyType) { + boolean found = false; + while (found || !parent.equals(Object.class)) { + try { + parent.getDeclaredField(property); + found = true; + } catch (NoSuchFieldException e) { + parent = parent.getSuperclass(); + } + } + if (!found) { + throw new IllegalArgumentException("Illegal property " + property); + } + } + }; + + public final PathBuilderValidator PROPERTIES = new PathBuilderValidator() { + @Override + public void validate(Class parent, String property, Class propertyType) { + boolean found = false; + String accessor = "get" + StringUtils.capitalize(property); + while (found || !parent.equals(Object.class)) { + try { + parent.getDeclaredMethod(accessor); + found = true; + } catch (NoSuchMethodException e) { + parent = parent.getSuperclass(); + } + } + if (!found) { + throw new IllegalArgumentException("Illegal property " + property); + } + } + }; + +}