diff --git a/src/com/wentch/redkale/source/DataDefaultSource.java b/src/com/wentch/redkale/source/DataDefaultSource.java index c008c22a3..1a2da1d98 100644 --- a/src/com/wentch/redkale/source/DataDefaultSource.java +++ b/src/com/wentch/redkale/source/DataDefaultSource.java @@ -1402,7 +1402,7 @@ public final class DataDefaultSource implements DataSource, Nameable, Function * @param clazz @@ -1502,7 +1502,7 @@ public final class DataDefaultSource implements DataSource, Nameable, Function * @param clazz diff --git a/src/com/wentch/redkale/source/EntityCache.java b/src/com/wentch/redkale/source/EntityCache.java index cdb2b29a5..66a06d7df 100644 --- a/src/com/wentch/redkale/source/EntityCache.java +++ b/src/com/wentch/redkale/source/EntityCache.java @@ -6,7 +6,6 @@ package com.wentch.redkale.source; import java.io.*; -import java.lang.reflect.Field; import java.util.*; import java.util.concurrent.*; import java.util.function.*; @@ -54,13 +53,12 @@ public final class EntityCache { this.primary = info.primary; this.needcopy = true; this.reproduce = Reproduce.create(type, type, (m) -> { - char[] mn = m.getName().substring(3).toCharArray(); - if (mn.length < 2 || Character.isLowerCase(mn[1])) mn[0] = Character.toLowerCase(mn[0]); try { - Field f = type.getDeclaredField(new String(mn)); - return f.getAnnotation(Transient.class) != null; + Transient t = type.getDeclaredField(m).getAnnotation(Transient.class); + if (t != null) return false; + return true; } catch (Exception e) { - return false; + return true; } }); } @@ -102,7 +100,7 @@ public final class EntityCache { if (selects == null) return (needcopy ? reproduce.copy(this.creator.create(), rs) : rs); T t = this.creator.create(); for (Attribute attr : this.info.attributes) { - if (selects.validate(attr.field())) attr.set(t, attr.get(rs)); + if (selects.test(attr.field())) attr.set(t, attr.get(rs)); } return t; } @@ -122,7 +120,7 @@ public final class EntityCache { T rs = opt.get(); T t = this.creator.create(); for (Attribute attr : this.info.attributes) { - if (selects.validate(attr.field())) attr.set(t, attr.get(rs)); + if (selects.test(attr.field())) attr.set(t, attr.get(rs)); } return t; } @@ -324,7 +322,7 @@ public final class EntityCache { } else { final List> attrs = new ArrayList<>(); info.forEachAttribute((k, v) -> { - if (selects.validate(k)) attrs.add(v); + if (selects.test(k)) attrs.add(v); }); Consumer action = x -> { final T item = creator.create(); diff --git a/src/com/wentch/redkale/source/EntityInfo.java b/src/com/wentch/redkale/source/EntityInfo.java index e28608322..867ea3efb 100644 --- a/src/com/wentch/redkale/source/EntityInfo.java +++ b/src/com/wentch/redkale/source/EntityInfo.java @@ -343,7 +343,7 @@ public final class EntityInfo { public T getValue(final SelectColumn sels, final ResultSet set) throws SQLException { T obj = creator.create(); for (Attribute attr : queryAttributes) { - if (sels == null || sels.validate(attr.field())) { + if (sels == null || sels.test(attr.field())) { Serializable o = (Serializable) set.getObject(this.getSQLColumn(null, attr.field())); if (o != null) { Class t = attr.type(); diff --git a/src/com/wentch/redkale/source/SelectColumn.java b/src/com/wentch/redkale/source/SelectColumn.java deleted file mode 100644 index 4d95061c1..000000000 --- a/src/com/wentch/redkale/source/SelectColumn.java +++ /dev/null @@ -1,139 +0,0 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ -package com.wentch.redkale.source; - -import java.util.*; - -/** - * - * @author zhangjx - */ -public final class SelectColumn { - - private String[] startWithColumns; - - private String[] endWithColumns; - - private String[] columns; - - private boolean excludable; //是否排除 - - public SelectColumn() { - } - - private SelectColumn(String[] columns0, boolean excludable) { - this.excludable = excludable; - final int len = columns0.length; - if (len < 1) return; - boolean reg = false; - int slen = 0, elen = 0; - for (String col : columns0) { - if (col.charAt(0) == '^') { - slen++; - reg = true; - } else if (col.charAt(col.length() - 1) == '$') { - elen++; - reg = true; - } - } - if (reg) { - if (slen == len) { - this.startWithColumns = new String[len]; - for (int i = 0; i < len; i++) { - this.startWithColumns[i] = columns0[i].substring(1); - } - } else if (elen == len) { - this.endWithColumns = new String[len]; - for (int i = 0; i < len; i++) { - this.endWithColumns[i] = columns0[i].substring(0, columns0[i].length() - 1); - } - } else { - List starts = new ArrayList<>(); - List ends = new ArrayList<>(); - List strings = new ArrayList<>(); - for (String col : columns0) { - if (col.charAt(0) == '^') { - starts.add(col.substring(1)); - } else if (col.charAt(col.length() - 1) == '$') { - ends.add(col.substring(0, col.length() - 1)); - } else { - strings.add(col); - } - } - if (!starts.isEmpty()) this.startWithColumns = starts.toArray(new String[starts.size()]); - if (!ends.isEmpty()) this.endWithColumns = ends.toArray(new String[ends.size()]); - if (!strings.isEmpty()) this.columns = strings.toArray(new String[strings.size()]); - } - } else { - this.columns = columns0; - } - } - - /** - * class中的字段名 - * - * @param columns - * @return - */ - public static SelectColumn createIncludes(String... columns) { - return new SelectColumn(columns, false); - } - - /** - * class中的字段名 - * - * @param columns - * @return - */ - public static SelectColumn createExcludes(String... columns) { - return new SelectColumn(columns, true); - } - - public boolean validate(String column) { - if (this.columns != null) { - for (String col : this.columns) { - if (col.equalsIgnoreCase(column)) return !excludable; - } - } - if (this.startWithColumns != null) { - for (String col : this.startWithColumns) { - if (column.startsWith(col)) return !excludable; - } - } - if (this.endWithColumns != null) { - for (String col : this.endWithColumns) { - if (column.endsWith(col)) return !excludable; - } - } - return excludable; - } - - public boolean isEmpty() { - return this.columns == null || this.columns.length == 0; - } - - public String[] getColumns() { - return columns; - } - - public void setColumns(String[] columns) { - this.columns = columns; - } - - public boolean isExcludable() { - return excludable; - } - - public void setExcludable(boolean excludable) { - this.excludable = excludable; - } - - @Override - public String toString() { - return "SelectColumn{" + "columns=" + Arrays.toString(columns) + ", excludable=" + excludable + '}'; - } - -} diff --git a/src/com/wentch/redkale/util/Reproduce.java b/src/com/wentch/redkale/util/Reproduce.java index 3efd59a14..32f227cea 100644 --- a/src/com/wentch/redkale/util/Reproduce.java +++ b/src/com/wentch/redkale/util/Reproduce.java @@ -14,7 +14,7 @@ public interface Reproduce { } @SuppressWarnings("unchecked") - public static Reproduce create(final Class destClass, final Class srcClass, final Predicate excludeSetterPredicate) { + public static Reproduce create(final Class destClass, final Class srcClass, final Predicate columnPredicate) { // ------------------------------------------------------------------------------ final String supDynName = Reproduce.class.getName().replace('.', '/'); final String destName = destClass.getName().replace('.', '/'); @@ -60,7 +60,15 @@ public interface Reproduce { boolean is = getter.getName().startsWith("is"); try { setter = destClass.getMethod(getter.getName().replaceFirst(is ? "is" : "get", "set"), getter.getReturnType()); - if (excludeSetterPredicate != null && excludeSetterPredicate.test(setter)) continue; + if (columnPredicate != null) { + String col = setter.getName().substring(3); + if (col.length() < 2 || Character.isLowerCase(col.charAt(1))) { + char[] cs = col.toCharArray(); + cs[0] = Character.toLowerCase(cs[0]); + col = new String(cs); + } + if (!columnPredicate.test(col)) continue; + } } catch (Exception e) { continue; } diff --git a/src/com/wentch/redkale/util/SelectColumn.java b/src/com/wentch/redkale/util/SelectColumn.java new file mode 100644 index 000000000..991b5f0d9 --- /dev/null +++ b/src/com/wentch/redkale/util/SelectColumn.java @@ -0,0 +1,136 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.wentch.redkale.util; + +import java.util.*; +import java.util.function.*; +import java.util.regex.*; + +/** + * + * @author zhangjx + */ +public class SelectColumn implements Predicate { + + private Pattern[] patterns; + + private String[] columns; + + private boolean excludable; //是否排除 + + public SelectColumn() { + } + + protected SelectColumn(final String[] columns0, final boolean excludable) { + this.excludable = excludable; + final int len = columns0.length; + if (len < 1) return; + Pattern[] regs = null; + String[] cols = null; + int regcount = 0; + int colcount = 0; + for (String col : columns0) { + boolean reg = false; + for (int i = 0; i < col.length(); i++) { + char ch = col.charAt(i); + if (ch == '^' || ch == '$' || ch == '*' || ch == '?' || ch == '+' || ch == '[' || ch == '(') { + reg = true; + break; + } + } + if (reg) { + if (regs == null) regs = new Pattern[len]; + regs[regcount++] = Pattern.compile(col); + } else { + if (cols == null) cols = new String[len]; + cols[colcount++] = col; + } + } + if (regs != null) { + if (regcount == len) { + this.patterns = regs; + } else { + this.patterns = Arrays.copyOf(regs, regcount); + } + } + if (cols != null) { + if (colcount == len) { + this.columns = cols; + } else { + this.columns = Arrays.copyOf(cols, colcount); + } + } + } + + /** + * class中的字段名 + * + * @param columns + * @return + */ + public static SelectColumn createIncludes(String... columns) { + return new SelectColumn(columns, false); + } + + /** + * class中的字段名 + * + * @param columns + * @return + */ + public static SelectColumn createExcludes(String... columns) { + return new SelectColumn(columns, true); + } + + @Override + public boolean test(final String column) { + if (this.columns != null) { + for (String col : this.columns) { + if (col.equalsIgnoreCase(column)) return !excludable; + } + } + if (this.patterns != null) { + for (Pattern reg : this.patterns) { + if (reg.matcher(column).find()) return !excludable; + } + } + return excludable; + } + + public String[] getColumns() { + return columns; + } + + public void setColumns(String[] columns) { + this.columns = columns; + } + + public boolean isExcludable() { + return excludable; + } + + public void setExcludable(boolean excludable) { + this.excludable = excludable; + } + + public Pattern[] getPatterns() { + return patterns; + } + + public void setPatterns(Pattern[] patterns) { + this.patterns = patterns; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append(getClass().getSimpleName()).append("{excludable=").append(excludable); + if (columns != null) sb.append(", columns=").append(Arrays.toString(columns)); + if (patterns != null) sb.append(", patterns=").append(Arrays.toString(patterns)); + return sb.append('}').toString(); + } + +}