package org.jruby.javasupport.binding;
import org.jruby.Ruby;
import org.jruby.RubyClass;
import org.jruby.RubyModule;
import org.jruby.javasupport.Java;
final class ClassInitializer extends Initializer {
ClassInitializer(Ruby runtime, Class<?> javaClass) {
super(runtime, javaClass);
}
@Override
public RubyClass initialize(final RubyModule proxy) {
final RubyClass proxyClass = (RubyClass) proxy;
proxy.setJavaProxy(true);
proxy.getSingletonClass().setJavaProxy(true);
final RubyModule parent;
final Class<?> enclosingClass = javaClass.getEnclosingClass();
if ( enclosingClass != null ) {
parent = Java.getProxyClass(runtime, enclosingClass);
} else {
parent = Java.getJavaPackageModule(runtime, javaClass.getPackage());
}
proxy.setParent(parent);
if ( javaClass.isMemberClass() ) {
proxy.setBaseName( javaClass.getSimpleName() );
}
else {
String baseName = javaClass.getSimpleName();
if ( enclosingClass != null ) {
final String className = javaClass.getName();
final int length = className.length();
final int offset = enclosingClass.getName().length();
if ( length > offset && className.charAt(offset) != '$' ) {
baseName = className.substring( offset );
}
else if ( length > offset + 1 ) {
baseName = className.substring( offset + 1 );
}
}
proxy.setBaseName( baseName );
}
proxyClass.getName();
final MethodGatherer state = new MethodGatherer(runtime, javaClass.getSuperclass());
state.initialize(javaClass, proxy);
return proxyClass;
}
}