Skip to content

Instantly share code, notes, and snippets.

@b0c1
Created May 16, 2013 13:17
Show Gist options
  • Save b0c1/5591645 to your computer and use it in GitHub Desktop.
Save b0c1/5591645 to your computer and use it in GitHub Desktop.
Merged OObjectMethodFilter
import com.orientechnologies.common.log.OLogManager;
import com.orientechnologies.orient.object.enhancement.OObjectEntitySerializer;
import javassist.util.proxy.MethodFilter;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class OObjectMethodFilter implements MethodFilter {
protected boolean isScalaClass(Class<?> clz) {
Annotation[] annotations = clz.getDeclaredAnnotations();
for (Annotation a : annotations) {
if ("scala.reflect.ScalaSignature".contains(a.annotationType().getName()) || "scala.reflect.ScalaLongSignature".contains(a.getClass().getName())) {
return true;
}
}
return false;
}
protected String getScalaFieldName(Class<?> clz, Method m) {
String name = m.getName();
Field[] fields = clz.getDeclaredFields();
for (Field field : fields) {
if (name.equals(field.getName() + "_$eq")) {
return field.getName();
} else if (name.equals(field.getName())) {
return field.getName();
}
}
return null;
}
public boolean isHandled(Method m) {
String name = m.getName();
Class<?> clz = m.getDeclaringClass();
if (!isScalaClass(clz)) {
return isHandledInJava(m);
} else {
return getScalaFieldName(clz, m) != null;
}
}
public String getFieldName(Method m) {
String name = getJavaFieldName(m);
Class<?> clz = m.getDeclaringClass();
if (isScalaClass(clz) && name == null) {
return getScalaFieldName(clz, m);
}
return null;
}
public boolean isSetterMethod(String fieldName, Method m) throws SecurityException, NoSuchFieldException {
return isJavaSetterMethod(fieldName, m) || fieldName.endsWith("_$eq") == true; //To change body of overridden methods use File | Settings | File Templates.
}
public boolean isGetterMethod(String fieldName, Method m) throws SecurityException, NoSuchFieldException {
Class<?> clz = m.getDeclaringClass();
return isJavaGetterMethod(fieldName, m) || (isScalaClass(clz) && fieldName.equals(m.getName()));
}
public boolean isHandledInJava(final Method m) {
final String methodName = m.getName();
final String fieldName = getFieldName(m);
if (fieldName == null)
return false;
try {
if (!OObjectEntitySerializer.isClassField(m.getDeclaringClass(), fieldName))
return false;
return (isSetterMethod(methodName, m) || isGetterMethod(methodName, m));
} catch (NoSuchFieldException nsfe) {
OLogManager.instance().warn(this, "Error handling the method %s in class %s", nsfe, m.getName(),
m.getDeclaringClass().getName());
return false;
} catch (SecurityException se) {
OLogManager.instance().warn(this, "", se, m.getName(), m.getDeclaringClass().getName());
return false;
}
}
public String getJavaFieldName(final Method m) {
final String methodName = m.getName();
if (methodName.startsWith("get"))
return getFieldName(methodName, "get");
else if (methodName.startsWith("set"))
return getFieldName(methodName, "set");
else if (methodName.startsWith("is"))
return getFieldName(methodName, "is");
// NO FIELD
return null;
}
protected String getFieldName(final String methodName, final String prefix) {
final StringBuffer fieldName = new StringBuffer();
fieldName.append(Character.toLowerCase(methodName.charAt(prefix.length())));
fieldName.append(methodName.substring(prefix.length() + 1));
return fieldName.toString();
}
public boolean isJavaSetterMethod(final String fieldName, final Method m) throws SecurityException, NoSuchFieldException {
if (!fieldName.startsWith("set") || !checkIfFirstCharAfterPrefixIsUpperCase(fieldName, "set"))
return false;
if (m.getParameterTypes() != null && m.getParameterTypes().length != 1)
return false;
return !OObjectEntitySerializer.isTransientField(m.getDeclaringClass(), getFieldName(m));
}
public boolean isJavaGetterMethod(String fieldName, Method m) throws SecurityException, NoSuchFieldException {
int prefixLength;
if (fieldName.startsWith("get") && checkIfFirstCharAfterPrefixIsUpperCase(fieldName, "get"))
prefixLength = "get".length();
else if (fieldName.startsWith("is") && checkIfFirstCharAfterPrefixIsUpperCase(fieldName, "is"))
prefixLength = "is".length();
else
return false;
if (m.getParameterTypes() != null && m.getParameterTypes().length > 0)
return false;
if (fieldName.length() <= prefixLength)
return false;
return !OObjectEntitySerializer.isTransientField(m.getDeclaringClass(), getFieldName(m));
}
private boolean checkIfFirstCharAfterPrefixIsUpperCase(String methodName, String prefix) {
return methodName.length() > prefix.length() ? Character.isUpperCase(methodName.charAt(prefix.length())) : false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment