}
@JRubyMethod(required = 2, module = true, visibility = PRIVATE)
public static IRubyObject autoload(final IRubyObject recv, IRubyObject symbol, final IRubyObject file) {
Ruby runtime = recv.getRuntime();
final LoadService loadService = runtime.getLoadService();
String nonInternedName = symbol.asJavaString();
if (!IdUtil.isValidConstantName(nonInternedName)) {
throw runtime.newNameError("autoload must be constant name", nonInternedName);
}
if (!runtime.is1_9() && !(file instanceof RubyString)) throw runtime.newTypeError(file, runtime.getString());
RubyString fileString = RubyFile.get_path(runtime.getCurrentContext(), file);
if (fileString.isEmpty()) throw runtime.newArgumentError("empty file name");
final String baseName = symbol.asJavaString().intern(); // interned, OK for "fast" methods
final RubyModule module = getModuleForAutoload(runtime, recv);
String nm = module.getName() + "::" + baseName;
IRubyObject existingValue = module.fastFetchConstant(baseName);
if (existingValue != null && existingValue != RubyObject.UNDEF) return runtime.getNil();
module.fastStoreConstant(baseName, RubyObject.UNDEF);
loadService.addAutoload(nm, new IAutoloadMethod() {
public String file() {
return file.toString();
}
/**
* @see org.jruby.runtime.load.IAutoloadMethod#load(Ruby, String)
*/
public IRubyObject load(Ruby runtime, String name) {
boolean required = loadService.require(file());
// File to be loaded by autoload has already been or is being loaded.
if (!required) return null;
return module.fastGetConstant(baseName);