package org.jruby.common;
import java.util.EnumSet;
import java.util.Set;
import org.joni.WarnCallback;
import org.jruby.Ruby;
import org.jruby.RubyModule;
import org.jruby.RubyString;
import org.jruby.anno.JRubyMethod;
import org.jruby.lexer.yacc.ISourcePosition;
import org.jruby.runtime.JavaSites;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.backtrace.RubyStackTraceElement;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.TypeConverter;
public class RubyWarnings implements IRubyWarnings, WarnCallback {
private final Ruby runtime;
private final Set<ID> oncelers = EnumSet.allOf(IRubyWarnings.ID.class);
public RubyWarnings(Ruby runtime) {
this.runtime = runtime;
}
public static RubyModule createWarningModule(Ruby runtime) {
RubyModule warning = runtime.defineModule("Warning");
warning.defineAnnotatedMethods(RubyWarnings.class);
warning.extend_object(warning);
return warning;
}
@Override
public void warn(String message) {
warn(ID.MISCELLANEOUS, message);
}
@Override
public Ruby getRuntime() {
return runtime;
}
@Override
public boolean isVerbose() {
return runtime.isVerbose();
}
@Override
@Deprecated
public void warn(ID id, ISourcePosition position, String message) {
if (!runtime.warningsEnabled()) return;
warn(id, position.getFile(), position.getLine(), message);
}
@Override
public void warn(ID id, String fileName, int lineNumber, String message) {
if (!runtime.warningsEnabled()) return;
StringBuilder buffer = new StringBuilder(100);
buffer.append(fileName).append(':').append(lineNumber + 1).append(": ");
buffer.append("warning: ").append(message).append('\n');
RubyString errorString = runtime.newString(buffer.toString());
writeWarningDyncall(runtime.getCurrentContext(), errorString);
}
public static void writeWarningDyncall(ThreadContext context, RubyString errorString) {
RubyModule warning = context.runtime.getWarning();
sites(context).warn.call(context, warning, warning, errorString);
}
public static void writeWarningToError(ThreadContext context, RubyString errorString) {
Ruby runtime = context.runtime;
IRubyObject errorStream = runtime.getGlobalVariables().get("$stderr");
RubyModule warning = runtime.getWarning();
sites(context).write.call(context, warning, errorStream, errorString);
}
@Override
public void warn(ID id, String message) {
if (!runtime.warningsEnabled()) return;
RubyStackTraceElement stack = runtime.getCurrentContext().getSingleBacktrace();
String file;
int line;
if (stack == null) {
file = "(unknown)";
line = 0;
} else {
file = stack.getFileName();
line = stack.getLineNumber();
}
warn(id, file, line - 1, message);
}
public void warn(String filename, String message) {
if (!runtime.warningsEnabled()) return;
StringBuilder buffer = new StringBuilder(100);
buffer.append(filename).append(": ").append(message).append('\n');
RubyString errorString = runtime.newString(buffer.toString());
writeWarningDyncall(runtime.getCurrentContext(), errorString);
}
public void warnOnce(ID id, String message) {
if (!runtime.warningsEnabled()) return;
if (oncelers.contains(id)) return;
oncelers.add(id);
warn(id, message);
}
public void warning(String message) {
if (!isVerbose()) return;
if (!runtime.warningsEnabled()) return;
warning(ID.MISCELLANEOUS, message);
}
@Override
public void warning(ID id, String message) {
if (!runtime.warningsEnabled() || !runtime.isVerbose()) return;
writeWarning(runtime, id, message);
}
private static void writeWarning(Ruby runtime, ID id, String message) {
RubyStackTraceElement stack = runtime.getCurrentContext().getSingleBacktrace();
String file;
int line;
if (stack == null) {
file = "(unknown)";
line = -1;
} else {
file = stack.getFileName();
line = stack.getLineNumber();
}
runtime.getWarnings().warning(id, file, line, message);
}
@Override
@Deprecated
public void warning(ID id, ISourcePosition position, String message) {
warning(id, position.getFile(), position.getLine(), message);
}
@Override
public void warning(ID id, String fileName, int lineNumber, String message) {
if (!runtime.warningsEnabled() || !runtime.isVerbose()) return;
warn(id, fileName, lineNumber, message);
}
@JRubyMethod
public static IRubyObject warn(ThreadContext context, IRubyObject recv, IRubyObject arg) {
Ruby runtime = context.runtime;
if (!runtime.warningsEnabled()) return context.nil;
TypeConverter.checkType(context, arg, runtime.getString());
RubyString str = (RubyString) arg;
if (!str.getEncoding().isAsciiCompatible()) {
throw runtime.newEncodingCompatibilityError("ASCII incompatible encoding: " + str.getEncoding());
}
writeWarningToError(runtime.getCurrentContext(), str);
return context.nil;
}
private static JavaSites.WarningSites sites(ThreadContext context) {
return context.sites.Warning;
}
@Override
@Deprecated
public void warn(ID id, String fileName, String message) {
if (!runtime.warningsEnabled()) return;
StringBuilder buffer = new StringBuilder(100);
buffer.append(fileName).append(' ');
buffer.append("warning: ").append(message).append('\n');
IRubyObject errorStream = runtime.getGlobalVariables().get("$stderr");
errorStream.callMethod(runtime.getCurrentContext(), "write", runtime.newString(buffer.toString()));
}
}