Skip to content

Instantly share code, notes, and snippets.

@b0c1
Created May 16, 2013 09:59
Show Gist options
  • Save b0c1/5590679 to your computer and use it in GitHub Desktop.
Save b0c1/5590679 to your computer and use it in GitHub Desktop.
Sime scala getter/setter wrapper for OrientDB
package test;
import com.orientechnologies.orient.object.enhancement.OObjectMethodFilter;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class CustomMethodFilter extends OObjectMethodFilter {
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;
}
@Override
public boolean isHandled(Method m) {
String name = m.getName();
Class<?> clz = m.getDeclaringClass();
if (!isScalaClass(clz)) {
return super.isHandled(m);
} else {
return getScalaFieldName(clz, m) != null;
}
}
@Override
public String getFieldName(Method m) {
String name = super.getFieldName(m);
Class<?> clz = m.getDeclaringClass();
if (isScalaClass(clz) && name == null) {
return getScalaFieldName(clz, m);
}
return null;
}
@Override
public boolean isSetterMethod(String fieldName, Method m) throws SecurityException, NoSuchFieldException {
return super.isSetterMethod(fieldName, m) || fieldName.endsWith("_$eq") == true; //To change body of overridden methods use File | Settings | File Templates.
}
@Override
public boolean isGetterMethod(String fieldName, Method m) throws SecurityException, NoSuchFieldException {
Class<?> clz = m.getDeclaringClass();
return super.isGetterMethod(fieldName, m) || (isScalaClass(clz) && fieldName.equals(m.getName()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment