Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix UNICODE_CHARACTER_CLASS flag not supported #1211

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions src/main/java/com/hubspot/jinjava/el/android/BeanInfoUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.hubspot.jinjava.el.android;


import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class BeanInfoUtil {

public static List<PropertyDescriptor> getPropertyDescriptors(Class<?> clazz) throws IntrospectionException {
List<PropertyDescriptor> propertyDescriptors = new ArrayList<>();

// 获取所有公共字段
// Field[] fields = clazz.getDeclaredFields();

// for (Field field : fields) {
// if (!Modifier.isStatic(field.getModifiers())) {
// String fieldName = field.getName();
// if (field.getType() == boolean.class) {
// PropertyDescriptor pd = new PropertyDescriptor(fieldName, clazz,"is" + NameGenerator.capitalize(fieldName), null);
// propertyDescriptors.add(pd);
// } else {
// PropertyDescriptor pd = new PropertyDescriptor(fieldName, clazz, "get" + NameGenerator.capitalize(fieldName), null);
// propertyDescriptors.add(pd);
// }
// }
// }

// 获取所有公共方法
Method[] methods = clazz.getMethods();

ArrayList<String> addedProperties = new ArrayList<>();
for (Method method : methods) {
String methodName = method.getName();
if (methodName.startsWith("get") || methodName.startsWith("is")) {
String propertyName = getPropertyName(methodName);
if (propertyName != null) {
propertyName = propertyName.toLowerCase();
PropertyDescriptor pd = new PropertyDescriptor(propertyName, clazz, methodName, null);
if (!propertyDescriptors.contains(pd)) {
propertyDescriptors.add(pd);
addedProperties.add(propertyName);
}
}
}
}
// new Throwable("addedProperties " + addedProperties).printStackTrace();

return propertyDescriptors;
}

private static String getPropertyName(String methodName) {
if (methodName.startsWith("get")) {
return methodName.substring(3);
} else if (methodName.startsWith("is")) {
return methodName.substring(2);
}
return null;
}
}
64 changes: 64 additions & 0 deletions src/main/java/com/hubspot/jinjava/el/android/NameGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.hubspot.jinjava.el.android;

import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Locale;
import java.util.Map;

public class NameGenerator {
private Map<Object, String> valueToName = new IdentityHashMap();
private Map<String, Integer> nameToCount = new HashMap();

public NameGenerator() {
}

public void clear() {
this.valueToName.clear();
this.nameToCount.clear();
}

public static String unqualifiedClassName(Class type) {
if (type.isArray()) {
return unqualifiedClassName(type.getComponentType()) + "Array";
} else {
String name = type.getName();
return name.substring(name.lastIndexOf(46) + 1);
}
}

public static String capitalize(String name) {
if (name != null && name.length() != 0) {
String var10000 = name.substring(0, 1).toUpperCase(Locale.ENGLISH);
return var10000 + name.substring(1);
} else {
return name;
}
}

public String instanceName(Object instance) {
if (instance == null) {
return "null";
} else if (instance instanceof Class) {
return unqualifiedClassName((Class)instance);
} else {
String result = (String)this.valueToName.get(instance);
if (result != null) {
return result;
} else {
Class<?> type = instance.getClass();
String className = unqualifiedClassName(type);
Integer size = (Integer)this.nameToCount.get(className);
int instanceNumber = size == null ? 0 : size + 1;
this.nameToCount.put(className, instanceNumber);
result = className + instanceNumber;
this.valueToName.put(instance, result);
return result;
}
}
}
}
12 changes: 5 additions & 7 deletions src/main/java/com/hubspot/jinjava/el/ext/BeanELResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@
*/
package com.hubspot.jinjava.el.ext;

import com.hubspot.jinjava.el.android.BeanInfoUtil;
import java.beans.FeatureDescriptor;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import java.lang.reflect.*;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
Expand Down Expand Up @@ -66,7 +64,7 @@ protected static final class BeanProperties {
public BeanProperties(Class<?> baseClass) {
PropertyDescriptor[] descriptors;
try {
descriptors = Introspector.getBeanInfo(baseClass).getPropertyDescriptors();
descriptors = BeanInfoUtil.getPropertyDescriptors(baseClass).toArray(new PropertyDescriptor[0]);
} catch (IntrospectionException e) {
throw new ELException(e);
}
Expand Down Expand Up @@ -236,7 +234,7 @@ public Iterator<FeatureDescriptor> getFeatureDescriptors(
if (isResolvable(base)) {
final PropertyDescriptor[] properties;
try {
properties = Introspector.getBeanInfo(base.getClass()).getPropertyDescriptors();
properties = BeanInfoUtil.getPropertyDescriptors(base.getClass()).toArray(new PropertyDescriptor[0]);
} catch (IntrospectionException e) {
return Collections.<FeatureDescriptor>emptyList().iterator();
}
Expand Down
18 changes: 14 additions & 4 deletions src/main/java/com/hubspot/jinjava/lib/filter/WordCountFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@
)
public class WordCountFilter implements Filter {

private static Pattern WORD_RE = Pattern.compile(
"\\w+", Pattern.MULTILINE
);

static {
try {
WORD_RE = Pattern.compile(
"\\w+",
Pattern.UNICODE_CHARACTER_CLASS | Pattern.MULTILINE
);
} catch (Throwable e) {
}
}

@Override
public String getName() {
return "wordcount";
Expand All @@ -43,8 +57,4 @@ public Object filter(Object var, JinjavaInterpreter interpreter, String... args)
return Integer.valueOf(count);
}

private static final Pattern WORD_RE = Pattern.compile(
"\\w+",
Pattern.UNICODE_CHARACTER_CLASS | Pattern.MULTILINE
);
}
6 changes: 2 additions & 4 deletions src/main/java/com/hubspot/jinjava/lib/tag/ForTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.hubspot.jinjava.doc.annotations.JinjavaParam;
import com.hubspot.jinjava.doc.annotations.JinjavaSnippet;
import com.hubspot.jinjava.doc.annotations.JinjavaTextMateSnippet;
import com.hubspot.jinjava.el.android.BeanInfoUtil;
import com.hubspot.jinjava.el.ext.DeferredParsingException;
import com.hubspot.jinjava.interpret.DeferredValue;
import com.hubspot.jinjava.interpret.DeferredValueException;
Expand All @@ -41,7 +42,6 @@
import com.hubspot.jinjava.util.HelperStringTokenizer;
import com.hubspot.jinjava.util.LengthLimitingStringBuilder;
import com.hubspot.jinjava.util.ObjectIterator;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.util.ConcurrentModificationException;
import java.util.List;
Expand Down Expand Up @@ -241,9 +241,7 @@ public String renderForCollection(
interpreter.getContext().put(loopVar, entryVal);
} else {
try {
PropertyDescriptor[] valProps = Introspector
.getBeanInfo(val.getClass())
.getPropertyDescriptors();
PropertyDescriptor[] valProps = BeanInfoUtil.getPropertyDescriptors(val.getClass()).toArray(new PropertyDescriptor[0]);
for (PropertyDescriptor valProp : valProps) {
if (loopVar.equals(valProp.getName())) {
interpreter
Expand Down
Loading