Skip to content

Commit

Permalink
Clean up deprecated calls into JRuby
Browse files Browse the repository at this point in the history
The deprecations here in JRuby are at least two years old. Some of
these changes are not backward-compatible with JRuby versions
prior to that, but those JRuby versions are no longer supported.

Summary of changes:

* Use RubyException.toThrowable to construct a Throwable wrapper
  for an exception, rather than constructing RaiseException
  directly. The new mechanism allows exceptions to override what
  type of Java throwable they represent. This change is not
  compatible with JRuby versions prior to 9.2.
* Avoid the long-deprecated (pre-9k) NativeException by raising
  RuntimeError for some unexpected exceptions instead of using
  RaiseException.createNativeRaiseException. The NativeException
  class has been unsupported for many years in JRuby, and these
  are better represented as RuntimeError.
* Deprecate to_s19 override and flip delegation to to_s. All of
  the "19" methods were deprecated for JRuby 9k, and many have
  since been removed. The delegation should now prefer the non-19
  name and avoid the 19 version.
  • Loading branch information
headius committed Apr 26, 2020
1 parent a762738 commit de8c32d
Show file tree
Hide file tree
Showing 10 changed files with 17 additions and 18 deletions.
2 changes: 1 addition & 1 deletion ext/java/nokogiri/HtmlSaxParserContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ else if (encoding instanceof RubyFixnum) {
throw context.getRuntime().newEncodingCompatibilityError(rubyEncoding + "is not supported");
}
catch (IllegalCharsetNameException e) {
throw context.getRuntime().newInvalidEncoding(e.getMessage());
throw context.getRuntime().newEncodingError(e.getMessage());
}
}

Expand Down
2 changes: 1 addition & 1 deletion ext/java/nokogiri/HtmlSaxPushParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public IRubyObject native_write(ThreadContext context, IRubyObject chunk, IRubyO
final ByteArrayInputStream data = NokogiriHelpers.stringBytesToStream(chunk);
if (data == null) {
terminateTask(context.runtime);
throw new RaiseException(XmlSyntaxError.createHTMLSyntaxError(context.runtime)); // Nokogiri::HTML::SyntaxError
throw XmlSyntaxError.createHTMLSyntaxError(context.runtime).toThrowable(); // Nokogiri::HTML::SyntaxError
}

int errorCount0 = parserTask.getErrorCount();
Expand Down
2 changes: 1 addition & 1 deletion ext/java/nokogiri/XmlNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -1662,7 +1662,7 @@ public IRubyObject process_xincludes(ThreadContext context, IRubyObject options)
while(errors.getLength() > 0) {
XmlSyntaxError error = (XmlSyntaxError)errors.shift(context);
if (error.toString().contains("Include operation failed")) {
throw new RaiseException(error);
throw error.toThrowable();
}
}
return this;
Expand Down
4 changes: 2 additions & 2 deletions ext/java/nokogiri/XmlReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ private void readMoreData(ThreadContext context) {
continueParsing = config.parse(false);
}
catch (XNIException e) {
throw new RaiseException(XmlSyntaxError.createXMLSyntaxError(context.runtime, e)); // Nokogiri::XML::SyntaxError
throw XmlSyntaxError.createXMLSyntaxError(context.runtime, e).toThrowable(); // Nokogiri::XML::SyntaxError
}
catch (IOException e) {
throw context.runtime.newRuntimeError(e.toString());
Expand Down Expand Up @@ -364,7 +364,7 @@ private IRubyObject setAndRaiseErrorsIfAny(final Ruby runtime, final RaiseExcept
errors.append(error);
setInstanceVariable("@errors", errors);

throw ex != null ? ex : new RaiseException((XmlSyntaxError) error);
throw ex != null ? ex : ((XmlSyntaxError) error).toThrowable();
}
if ( ex != null ) throw ex;
return this;
Expand Down
9 changes: 6 additions & 3 deletions ext/java/nokogiri/XmlSaxParserContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ protected void initialize(Ruby runtime) {
parser = createParser();
}
catch (SAXException se) {
throw RaiseException.createNativeRaiseException(runtime, se);
// Unexpected failure in XML subsystem
throw runtime.newRuntimeError(se.getMessage());
}
}

Expand Down Expand Up @@ -208,7 +209,8 @@ protected void preParse(Ruby runtime, IRubyObject handlerRuby, NokogiriHandler h
parser.setFeature(FEATURE_CONTINUE_AFTER_FATAL_ERROR, true);
}
catch (Exception e) {
throw RaiseException.createNativeRaiseException(runtime, e);
// Unexpected failure in XML subsystem
throw runtime.newRuntimeError(e.getMessage());
}
}
}
Expand Down Expand Up @@ -261,7 +263,8 @@ public IRubyObject parse_with(ThreadContext context, IRubyObject handlerRuby) {
}
}
catch (SAXException ex) {
throw RaiseException.createNativeRaiseException(runtime, ex);
// Unexpected failure in XML subsystem
throw runtime.newRuntimeError(ex.getMessage());
}
catch (IOException ex) {
throw runtime.newIOErrorFromException(ex);
Expand Down
2 changes: 1 addition & 1 deletion ext/java/nokogiri/XmlSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public static IRubyObject from_document(ThreadContext context, IRubyObject klazz

RubyArray errors = (RubyArray) doc.getInstanceVariable("@errors");
if (!errors.isEmpty()) {
throw new RaiseException((XmlSyntaxError) errors.first());
throw ((XmlSyntaxError) errors.first()).toThrowable();
}

DOMSource source = new DOMSource(doc.getDocument());
Expand Down
8 changes: 2 additions & 6 deletions ext/java/nokogiri/XmlSyntaxError.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,9 @@ public void setException(Ruby runtime, SAXParseException exception, int level) {
setInstanceVariable("@file", stringOrNil(runtime, exception.getSystemId()));
}

// NOTE: special care - due JRuby 1.7.x

@Override
public IRubyObject to_s(ThreadContext context) { return to_s19(context); }

@JRubyMethod(name = "to_s")
public RubyString to_s19(ThreadContext context) {
@Override
public IRubyObject to_s(ThreadContext context) {
RubyString msg = msg(context.runtime);
return msg != null ? msg : super.to_s(context).asString();
}
Expand Down
2 changes: 1 addition & 1 deletion ext/java/nokogiri/XmlXpathContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ private IRubyObject node_set(ThreadContext context, String expr, IRubyObject han
return tryGetNodeSet(context, expr, fnResolver);
}
catch (TransformerException ex) {
throw new RaiseException(XmlSyntaxError.createXMLXPathSyntaxError(context.runtime, expr, ex)); // Nokogiri::XML::XPath::SyntaxError
throw XmlSyntaxError.createXMLXPathSyntaxError(context.runtime, expr, ex).toThrowable(); // Nokogiri::XML::XPath::SyntaxError
}
}

Expand Down
2 changes: 1 addition & 1 deletion ext/java/nokogiri/internals/NokogiriHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ void handleError(SAXParseException ex) {
try {
final String msg = ex.getMessage();
call("error", runtime.newString(msg == null ? "" : msg));
addError(new RaiseException(XmlSyntaxError.createError(runtime, ex), true));
addError(XmlSyntaxError.createError(runtime, ex).toThrowable());
} catch( RaiseException e) {
addError(e);
throw e;
Expand Down
2 changes: 1 addition & 1 deletion ext/java/nokogiri/internals/XmlDomParserContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public XmlDocument getDocumentWithErrorsOrRaiseException(ThreadContext context,
} else {
XmlSyntaxError xmlSyntaxError = XmlSyntaxError.createXMLSyntaxError(context.runtime);
xmlSyntaxError.setException(ex);
throw new RaiseException(xmlSyntaxError);
throw xmlSyntaxError.toThrowable();
}
}

Expand Down

0 comments on commit de8c32d

Please sign in to comment.