package org.jruby.javasupport.ext;
import org.jruby.*;
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
import org.jruby.runtime.Helpers;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import java.lang.reflect.InvocationTargetException;
import static org.jruby.javasupport.JavaUtil.convertArguments;
import static org.jruby.javasupport.JavaUtil.convertJavaToUsableRubyObject;
import static org.jruby.javasupport.JavaUtil.unwrapJavaObject;
public abstract class JavaLangReflect {
public static void define(final Ruby runtime) {
JavaExtensions.put(runtime, java.lang.reflect.Constructor.class, (proxyClass) -> Constructor.define(runtime, (RubyClass) proxyClass));
JavaExtensions.put(runtime, java.lang.reflect.Field.class, (proxyClass) -> Field.define(runtime, (RubyClass) proxyClass));
JavaExtensions.put(runtime, java.lang.reflect.Method.class, (proxyClass) -> Method.define(runtime, (RubyClass) proxyClass));
}
@JRubyClass(name = "Java::JavaLangReflect::Constructor")
public static class Constructor {
static RubyClass define(final Ruby runtime, final RubyClass proxy) {
proxy.defineAnnotatedMethods(Constructor.class);
return proxy;
}
@JRubyMethod
public static IRubyObject return_type(final ThreadContext context, final IRubyObject self) {
return context.nil;
}
@JRubyMethod
public static IRubyObject argument_types(final ThreadContext context, final IRubyObject self) {
final java.lang.reflect.Constructor thiz = unwrapJavaObject(self);
return convertJavaToUsableRubyObject(context.runtime, thiz.getParameterTypes());
}
@JRubyMethod
public static IRubyObject inspect(final ThreadContext context, final IRubyObject self) {
final java.lang.reflect.AccessibleObject thiz = unwrapJavaObject(self);
return RubyString.newString(context.runtime, thiz.toString());
}
@JRubyMethod(name = "public?")
public static IRubyObject public_p(final IRubyObject self) {
final java.lang.reflect.Constructor thiz = unwrapJavaObject(self);
return isPublic(self, thiz.getModifiers());
}
@JRubyMethod(name = "protected?")
public static IRubyObject protected_p(final IRubyObject self) {
final java.lang.reflect.Constructor thiz = unwrapJavaObject(self);
return isProtected(self, thiz.getModifiers());
}
@JRubyMethod(name = "private?")
public static IRubyObject private_p(final IRubyObject self) {
final java.lang.reflect.Constructor thiz = unwrapJavaObject(self);
return isPrivate(self, thiz.getModifiers());
}
@JRubyMethod(name = "final?")
public static IRubyObject final_p(final IRubyObject self) {
final java.lang.reflect.Constructor thiz = unwrapJavaObject(self);
return isFinal(self, thiz.getModifiers());
}
@JRubyMethod(name = "static?")
public static IRubyObject static_p(final IRubyObject self) {
final java.lang.reflect.Constructor thiz = unwrapJavaObject(self);
return isStatic(self, thiz.getModifiers());
}
}
@JRubyClass(name = "Java::JavaLangReflect::Method")
public static class Method {
static RubyClass define(final Ruby runtime, final RubyClass proxy) {
proxy.defineAnnotatedMethods(Method.class);
return proxy;
}
@JRubyMethod
public static IRubyObject return_type(final ThreadContext context, final IRubyObject self) {
final java.lang.reflect.Method thiz = unwrapJavaObject(self);
return convertJavaToUsableRubyObject(context.runtime, thiz.getReturnType());
}
@JRubyMethod
public static IRubyObject argument_types(final ThreadContext context, final IRubyObject self) {
final java.lang.reflect.Method thiz = unwrapJavaObject(self);
return convertJavaToUsableRubyObject(context.runtime, thiz.getParameterTypes());
}
@JRubyMethod(rest = true)
public static IRubyObject invoke_static(final ThreadContext context, final IRubyObject self, final IRubyObject[] args) {
final java.lang.reflect.Method thiz = unwrapJavaObject(self);
final Object[] javaArgs = convertArguments(args, thiz.getParameterTypes());
try {
return convertJavaToUsableRubyObject(context.runtime, thiz.invoke(null, javaArgs));
}
catch (IllegalAccessException|InvocationTargetException e) {
Helpers.throwException(e); return null;
}
}
@JRubyMethod
public static IRubyObject inspect(final ThreadContext context, final IRubyObject self) {
final java.lang.reflect.AccessibleObject thiz = unwrapJavaObject(self);
return RubyString.newString(context.runtime, thiz.toString());
}
@JRubyMethod(name = "abstract?")
public static IRubyObject abstract_p(final IRubyObject self) {
final java.lang.reflect.Field thiz = unwrapJavaObject(self);
return isAbstract(self, thiz.getModifiers());
}
@JRubyMethod(name = "public?")
public static IRubyObject public_p(final IRubyObject self) {
final java.lang.reflect.Method thiz = unwrapJavaObject(self);
return isPublic(self, thiz.getModifiers());
}
@JRubyMethod(name = "protected?")
public static IRubyObject protected_p(final IRubyObject self) {
final java.lang.reflect.Method thiz = unwrapJavaObject(self);
return isProtected(self, thiz.getModifiers());
}
@JRubyMethod(name = "private?")
public static IRubyObject private_p(final IRubyObject self) {
final java.lang.reflect.Method thiz = unwrapJavaObject(self);
return isPrivate(self, thiz.getModifiers());
}
@JRubyMethod(name = "final?")
public static IRubyObject final_p(final IRubyObject self) {
final java.lang.reflect.Method thiz = unwrapJavaObject(self);
return isFinal(self, thiz.getModifiers());
}
@JRubyMethod(name = "static?")
public static IRubyObject static_p(final IRubyObject self) {
final java.lang.reflect.Method thiz = unwrapJavaObject(self);
return isStatic(self, thiz.getModifiers());
}
}
@JRubyClass(name = "Java::JavaLangReflect::Field")
public static class Field {
static RubyClass define(final Ruby runtime, final RubyClass proxy) {
proxy.defineAnnotatedMethods(Field.class);
return proxy;
}
@JRubyMethod
public static IRubyObject value_type(final ThreadContext context, final IRubyObject self) {
final java.lang.reflect.Field field = unwrapJavaObject(self);
return convertJavaToUsableRubyObject(context.runtime, field.getName());
}
@JRubyMethod
public static IRubyObject value(final ThreadContext context, final IRubyObject self, final IRubyObject obj) {
final java.lang.reflect.Field field = unwrapJavaObject(self);
try {
return convertJavaToUsableRubyObject(context.runtime, field.get(unwrapJavaObject(obj)));
}
catch (IllegalAccessException e) {
Helpers.throwException(e); return null;
}
}
@JRubyMethod
public static IRubyObject set_value(final ThreadContext context, final IRubyObject self, final IRubyObject obj,
final IRubyObject value) {
final java.lang.reflect.Field field = unwrapJavaObject(self);
try {
final Object val = value.toJava(field.getType());
field.set(unwrapJavaObject(obj), val);
}
catch (IllegalAccessException e) {
Helpers.throwException(e); return null;
}
return context.nil;
}
@JRubyMethod
public static IRubyObject static_value(final ThreadContext context, final IRubyObject self) {
final java.lang.reflect.Field field = unwrapJavaObject(self);
try {
return convertJavaToUsableRubyObject(context.runtime, field.get(null));
}
catch (IllegalAccessException e) {
Helpers.throwException(e); return null;
}
}
@JRubyMethod
public static IRubyObject set_static_value(final ThreadContext context, final IRubyObject self, final IRubyObject value) {
final java.lang.reflect.Field field = unwrapJavaObject(self);
try {
final Object val = value.toJava(field.getType());
field.set(null, val);
}
catch (IllegalAccessException e) {
Helpers.throwException(e); return null;
}
return context.nil;
}
@JRubyMethod
public static IRubyObject inspect(final ThreadContext context, final IRubyObject self) {
final java.lang.reflect.AccessibleObject thiz = unwrapJavaObject(self);
return RubyString.newString(context.runtime, thiz.toString());
}
@JRubyMethod(name = "public?")
public static IRubyObject public_p(final IRubyObject self) {
final java.lang.reflect.Field thiz = unwrapJavaObject(self);
return isPublic(self, thiz.getModifiers());
}
@JRubyMethod(name = "protected?")
public static IRubyObject protected_p(final IRubyObject self) {
final java.lang.reflect.Field thiz = unwrapJavaObject(self);
return isProtected(self, thiz.getModifiers());
}
@JRubyMethod(name = "private?")
public static IRubyObject private_p(final IRubyObject self) {
final java.lang.reflect.Field thiz = unwrapJavaObject(self);
return isPrivate(self, thiz.getModifiers());
}
@JRubyMethod(name = "final?")
public static IRubyObject final_p(final IRubyObject self) {
final java.lang.reflect.Field thiz = unwrapJavaObject(self);
return isFinal(self, thiz.getModifiers());
}
@JRubyMethod(name = "static?")
public static IRubyObject static_p(final IRubyObject self) {
final java.lang.reflect.Field thiz = unwrapJavaObject(self);
return isStatic(self, thiz.getModifiers());
}
}
static RubyBoolean isAbstract(final IRubyObject self, final int mod) {
return self.getRuntime().newBoolean(java.lang.reflect.Modifier.isAbstract(mod));
}
static RubyBoolean isPublic(final IRubyObject self, final int mod) {
return self.getRuntime().newBoolean(java.lang.reflect.Modifier.isPublic(mod));
}
static RubyBoolean isProtected(final IRubyObject self, final int mod) {
return self.getRuntime().newBoolean(java.lang.reflect.Modifier.isProtected(mod));
}
static RubyBoolean isPrivate(final IRubyObject self, final int mod) {
return self.getRuntime().newBoolean(java.lang.reflect.Modifier.isPrivate(mod));
}
static RubyBoolean isFinal(final IRubyObject self, final int mod) {
return self.getRuntime().newBoolean(java.lang.reflect.Modifier.isFinal(mod));
}
static RubyBoolean isStatic(final IRubyObject self, final int mod) {
return self.getRuntime().newBoolean(java.lang.reflect.Modifier.isStatic(mod));
}
}