@JRubyMethod public static IRubyObject inherited(ThreadContext context, IRubyObject self, IRubyObject subclass) { if ( ! ( subclass instanceof RubyClass ) ) { throw context.runtime.newTypeError(subclass, context.runtime.getClassClass()); } JavaInterfaceTemplate.addRealImplClassNew((RubyClass) subclass); return context.nil; } };
@JRubyMethod(frame = true) public static IRubyObject append_features(ThreadContext context, IRubyObject self, IRubyObject clazz, Block block) { if (clazz instanceof RubyClass) { appendFeaturesToClass(context, self, (RubyClass)clazz); } else if (clazz instanceof RubyModule) { appendFeaturesToModule(context, self, (RubyModule)clazz); } else { throw context.runtime.newTypeError("received " + clazz + ", expected Class/Module"); } return Helpers.invokeSuper(context, self, clazz, block); }
private static void appendFeaturesToClass(ThreadContext context, final IRubyObject self, final RubyClass clazz) { final Ruby runtime = context.runtime; checkAlreadyReified(clazz, runtime); final JavaClass javaClass = getJavaClassForInterface(self); RubyArray javaInterfaces; if ( ! clazz.hasInstanceVariable("@java_interfaces") ) { javaInterfaces = RubyArray.newArray(runtime, javaClass); clazz.setInstanceVariable("@java_interfaces", javaInterfaces); initInterfaceImplMethods(context, clazz); } else { javaInterfaces = (RubyArray) clazz.getInstanceVariable("@java_interfaces"); // we've already done the above priming logic, just add another interface // to the list of intentions unless we're past the point of no return or // already intend to implement the given interface if ( ! ( javaInterfaces.isFrozen() || javaInterfaces.includes(context, javaClass) ) ) { javaInterfaces.append(javaClass); } } }
private static void appendFeaturesToClass(ThreadContext context, IRubyObject self, final RubyClass clazz) { Ruby runtime = context.runtime; checkAlreadyReified(clazz, runtime); IRubyObject javaClassObj = Helpers.getInstanceVariable(self, runtime, "@java_class"); IRubyObject javaInterfaces; if (!clazz.hasInstanceVariable("@java_interfaces")) { javaInterfaces = RubyArray.newArray(runtime, javaClassObj); Helpers.setInstanceVariable(javaInterfaces, clazz, "@java_interfaces"); initInterfaceImplMethods(context, clazz); } else { javaInterfaces = Helpers.getInstanceVariable(clazz, runtime, "@java_interfaces"); // we've already done the above priming logic, just add another interface // to the list of intentions unless we're past the point of no return or // already intend to implement the given interface if (!(javaInterfaces.isFrozen() || ((RubyArray)javaInterfaces).includes(context, javaClassObj))) { ((RubyArray)javaInterfaces).append(javaClassObj); } } }
JavaInterfaceTemplate.createJavaInterfaceTemplateModule(context);
private static void appendFeaturesToModule(ThreadContext context, final IRubyObject self, final RubyModule module) { // assuming the user wants a collection of interfaces that can be // included together. make it so. final Ruby runtime = context.runtime; final IRubyObject java_class = module.getInstanceVariables().getInstanceVariable("@java_class"); // not allowed for existing Java interface modules if (java_class != null && java_class.isTrue()) { throw runtime.newTypeError("can not add Java interface to existing Java interface"); } // To turn a module into an "interface collection" we add a class instance // variable to hold the list of interfaces, and modify append_features // for this module to call append_features on each of those interfaces as // well synchronized (module) { if ( initInterfaceModules(self, module) ) { // true - initialized final RubyClass singleton = module.getSingletonClass(); singleton.addMethod("append_features", new AppendFeatures(singleton)); } else { // already set up append_features, just add the interface if we haven't already final RubyArray interfaceModules = getInterfaceModules(module); if ( ! interfaceModules.includes(context, self) ) { interfaceModules.append(self); } } } }
@Deprecated // not used - should go away in >= 9.2 // not intended to be called directly by users (private) // OLD TODO from Ruby code: // This should be implemented in JavaClass.java, where we can // check for reserved Ruby names, conflicting methods, etc. @JRubyMethod(visibility = Visibility.PRIVATE) public static IRubyObject implement(ThreadContext context, IRubyObject self, IRubyObject clazz) { if ( ! (clazz instanceof RubyModule) ) { final Ruby runtime = context.runtime; throw runtime.newTypeError(clazz, runtime.getModule()); } final RubyModule targetModule = (RubyModule) clazz; final JavaClass javaClass = getJavaClassForInterface(self); final Method[] javaInstanceMethods = javaClass.javaClass().getMethods(); final DynamicMethod dummyMethodImpl = new DummyMethodImpl(targetModule); for (int i = 0; i < javaInstanceMethods.length; i++) { final Method javaMethod = javaInstanceMethods[i]; final String name = javaMethod.getName(); if ( targetModule.searchMethod(name).isUndefined() ) { targetModule.addMethod(name, dummyMethodImpl); // only those not-defined } } return context.nil; }
private static void appendFeaturesToClass(ThreadContext context, IRubyObject self, final RubyClass clazz) { Ruby runtime = context.runtime; checkAlreadyReified(clazz, runtime); IRubyObject javaClassObj = Helpers.getInstanceVariable(self, runtime, "@java_class"); IRubyObject javaInterfaces; if (!clazz.hasInstanceVariable("@java_interfaces")) { javaInterfaces = RubyArray.newArray(runtime, javaClassObj); Helpers.setInstanceVariable(javaInterfaces, clazz, "@java_interfaces"); initInterfaceImplMethods(context, clazz); } else { javaInterfaces = Helpers.getInstanceVariable(clazz, runtime, "@java_interfaces"); // we've already done the above priming logic, just add another interface // to the list of intentions unless we're past the point of no return or // already intend to implement the given interface if (!(javaInterfaces.isFrozen() || ((RubyArray)javaInterfaces).includes(context, javaClassObj))) { ((RubyArray)javaInterfaces).append(javaClassObj); } } }
JavaInterfaceTemplate.createJavaInterfaceTemplateModule(context);
private static void appendFeaturesToModule(ThreadContext context, final IRubyObject self, final RubyModule module) { // assuming the user wants a collection of interfaces that can be // included together. make it so. final Ruby runtime = context.runtime; final IRubyObject java_class = module.getInstanceVariables().getInstanceVariable("@java_class"); // not allowed for existing Java interface modules if (java_class != null && java_class.isTrue()) { throw runtime.newTypeError("can not add Java interface to existing Java interface"); } // To turn a module into an "interface collection" we add a class instance // variable to hold the list of interfaces, and modify append_features // for this module to call append_features on each of those interfaces as // well synchronized (module) { if ( initInterfaceModules(self, module) ) { // true - initialized final RubyClass singleton = module.getSingletonClass(); singleton.addMethod("append_features", new AppendFeatures(singleton)); } else { // already set up append_features, just add the interface if we haven't already final RubyArray interfaceModules = getInterfaceModules(module); if ( ! interfaceModules.includes(context, self) ) { interfaceModules.append(self); } } } }
@Deprecated // not used - should go away in >= 9.2 // not intended to be called directly by users (private) // OLD TODO from Ruby code: // This should be implemented in JavaClass.java, where we can // check for reserved Ruby names, conflicting methods, etc. @JRubyMethod(visibility = Visibility.PRIVATE) public static IRubyObject implement(ThreadContext context, IRubyObject self, IRubyObject clazz) { if ( ! (clazz instanceof RubyModule) ) { final Ruby runtime = context.runtime; throw runtime.newTypeError(clazz, runtime.getModule()); } final RubyModule targetModule = (RubyModule) clazz; final JavaClass javaClass = getJavaClassForInterface(self); final Method[] javaInstanceMethods = javaClass.javaClass().getMethods(); final DynamicMethod dummyMethodImpl = new DummyMethodImpl(targetModule); for (int i = 0; i < javaInstanceMethods.length; i++) { final Method javaMethod = javaInstanceMethods[i]; final String name = javaMethod.getName(); if ( targetModule.searchMethod(name).isUndefined() ) { targetModule.addMethod(name, dummyMethodImpl); // only those not-defined } } return context.nil; }
@JRubyMethod(frame = true) public static IRubyObject append_features(ThreadContext context, IRubyObject self, IRubyObject clazz, Block block) { if (clazz instanceof RubyClass) { appendFeaturesToClass(context, self, (RubyClass)clazz); } else if (clazz instanceof RubyModule) { appendFeaturesToModule(context, self, (RubyModule)clazz); } else { throw context.runtime.newTypeError("received " + clazz + ", expected Class/Module"); } return Helpers.invokeSuper(context, self, clazz, block); }
private static void appendFeaturesToClass(ThreadContext context, final IRubyObject self, final RubyClass clazz) { final Ruby runtime = context.runtime; checkAlreadyReified(clazz, runtime); final JavaClass javaClass = getJavaClassForInterface(self); RubyArray javaInterfaces; if ( ! clazz.hasInstanceVariable("@java_interfaces") ) { javaInterfaces = RubyArray.newArray(runtime, javaClass); clazz.setInstanceVariable("@java_interfaces", javaInterfaces); initInterfaceImplMethods(context, clazz); } else { javaInterfaces = (RubyArray) clazz.getInstanceVariable("@java_interfaces"); // we've already done the above priming logic, just add another interface // to the list of intentions unless we're past the point of no return or // already intend to implement the given interface if ( ! ( javaInterfaces.isFrozen() || javaInterfaces.includes(context, javaClass) ) ) { javaInterfaces.append(javaClass); } } }
@JRubyMethod public static IRubyObject inherited(ThreadContext context, IRubyObject self, IRubyObject subclass) { if ( ! ( subclass instanceof RubyClass ) ) { throw context.runtime.newTypeError(subclass, context.runtime.getClassClass()); } JavaInterfaceTemplate.addRealImplClassNew((RubyClass) subclass); return context.nil; } };
JavaInterfaceTemplate.createJavaInterfaceTemplateModule(context);
@JRubyMethod(frame = true) // framed for invokeSuper public static IRubyObject append_features(ThreadContext context, IRubyObject self, IRubyObject clazz, Block block) { if ( clazz instanceof RubyClass ) { appendFeaturesToClass(context, self, (RubyClass) clazz); } else if ( clazz instanceof RubyModule ) { appendFeaturesToModule(context, self, (RubyModule) clazz); } else { throw context.runtime.newTypeError("received " + clazz + ", expected Class/Module"); } return Helpers.invokeSuper(context, self, clazz, block); }
@JRubyMethod public static IRubyObject inherited(IRubyObject recv, IRubyObject arg0) { if (!(arg0 instanceof RubyClass)) { throw recv.getRuntime().newTypeError(arg0, recv.getRuntime().getClassClass()); } JavaInterfaceTemplate.addRealImplClassNew((RubyClass)arg0); return recv.getRuntime().getNil(); } };
JavaInterfaceTemplate.createJavaInterfaceTemplateModule(context);
@JRubyMethod(frame = true) // framed for invokeSuper public static IRubyObject append_features(ThreadContext context, IRubyObject self, IRubyObject clazz, Block block) { if ( clazz instanceof RubyClass ) { appendFeaturesToClass(context, self, (RubyClass) clazz); } else if ( clazz instanceof RubyModule ) { appendFeaturesToModule(context, self, (RubyModule) clazz); } else { throw context.runtime.newTypeError("received " + clazz + ", expected Class/Module"); } return Helpers.invokeSuper(context, self, clazz, block); }
@JRubyMethod public static IRubyObject inherited(IRubyObject recv, IRubyObject arg0) { if (!(arg0 instanceof RubyClass)) { throw recv.getRuntime().newTypeError(arg0, recv.getRuntime().getClassClass()); } JavaInterfaceTemplate.addRealImplClassNew((RubyClass)arg0); return recv.getRuntime().getNil(); } };