Skip to content

Commit 6662ab8

Browse files
committed
Initial cleanup of JRuby extension
* Recommended changes from IntelliJ, including generic type cleanups, inner classes to lambdas, unused methods deprecated, redundant code eliminated, loops simplified, imports cleaned up, and more. * Added TODOs to calls to JRuby methods deprecated in 10. These have replacements in 10 that will be backported after release. These calls should not be updated to the new versions for at least a year after they are available in JRuby 9.4 releases. This commit should not contain any behavioral changes. All changes here should be simple cleanup and Java style updates.
1 parent 56b336b commit 6662ab8

31 files changed

Lines changed: 249 additions & 283 deletions

ext/java/nokogiri/Html4Document.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public class Html4Document extends XmlDocument
5252
super(ruby, klazz, doc);
5353
}
5454

55-
@JRubyMethod(name = "new", meta = true, rest = true, required = 0)
55+
@JRubyMethod(name = "new", meta = true, rest = true)
5656
public static IRubyObject
5757
rbNew(ThreadContext context, IRubyObject klazz, IRubyObject[] args)
5858
{

ext/java/nokogiri/Html4ElementDescription.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class Html4ElementDescription extends RubyObject
3333
static
3434
{
3535
Map<Short, List<String>> _subElements =
36-
new HashMap<Short, List<String>>();
36+
new HashMap<>();
3737
subElements = Collections.synchronizedMap(_subElements);
3838
}
3939

@@ -56,7 +56,7 @@ public class Html4ElementDescription extends RubyObject
5656
List<String> subs = subElements.get(elem.code);
5757

5858
if (subs == null) {
59-
subs = new ArrayList<String>();
59+
subs = new ArrayList<>();
6060

6161
/*
6262
* A bit of a hack. NekoHtml source code shows that
@@ -127,6 +127,7 @@ public class Html4ElementDescription extends RubyObject
127127
ary[i] = ruby.newString(subs.get(i));
128128
}
129129

130+
// TODO: switch to common undeprecated API when 9.4 adds 10 methods
130131
return ruby.newArray(ary);
131132
}
132133

ext/java/nokogiri/Html4EntityLookup.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class Html4EntityLookup extends RubyObject
2929

3030
/**
3131
* Looks up an HTML entity <code>key</code>.
32-
*
32+
* <p>
3333
* The description is a bit lacking.
3434
*/
3535
@JRubyMethod()
@@ -53,11 +53,10 @@ public class Html4EntityLookup extends RubyObject
5353

5454
IRubyObject edClass =
5555
ruby.getClassFromPath("Nokogiri::HTML4::EntityDescription");
56-
IRubyObject edObj = invoke(context, edClass, "new",
56+
57+
return invoke(context, edClass, "new",
5758
ruby.newFixnum(val), ruby.newString(name),
5859
ruby.newString(name + " entity"));
59-
60-
return edObj;
6160
}
6261

6362
}

ext/java/nokogiri/Html4SaxParserContext.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
11
package nokogiri;
22

3-
import java.io.ByteArrayInputStream;
43
import java.io.InputStream;
54

65
import org.apache.xerces.parsers.AbstractSAXParser;
76
import net.sourceforge.htmlunit.cyberneko.parsers.SAXParser;
87
import org.jruby.Ruby;
98
import org.jruby.RubyClass;
109
import org.jruby.RubyEncoding;
11-
import org.jruby.RubyFixnum;
12-
import org.jruby.RubyString;
1310
import org.jruby.anno.JRubyClass;
1411
import org.jruby.anno.JRubyMethod;
1512
import org.jruby.runtime.ThreadContext;
1613
import org.jruby.runtime.builtin.IRubyObject;
1714
import org.xml.sax.SAXException;
1815

1916
import nokogiri.internals.NokogiriHandler;
20-
import static nokogiri.internals.NokogiriHelpers.rubyStringToString;
2117

2218
import static org.jruby.runtime.Helpers.invoke;
2319

@@ -65,7 +61,7 @@ public class Html4SaxParserContext extends XmlSaxParserContext
6561
return parser;
6662
} catch (SAXException ex) {
6763
throw new SAXException(
68-
"Problem while creating HTML4 SAX Parser: " + ex.toString());
64+
"Problem while creating HTML4 SAX Parser: " + ex);
6965
}
7066
}
7167

@@ -76,9 +72,10 @@ public class Html4SaxParserContext extends XmlSaxParserContext
7672
String java_encoding = null;
7773
if (encoding != context.runtime.getNil()) {
7874
if (!(encoding instanceof RubyEncoding)) {
75+
// TODO: switch to common undeprecated API when 9.4 adds 10 methods
7976
throw context.runtime.newTypeError("encoding must be kind_of Encoding");
8077
}
81-
java_encoding = ((RubyEncoding)encoding).toString();
78+
java_encoding = encoding.toString();
8279
}
8380

8481
Html4SaxParserContext ctx = Html4SaxParserContext.newInstance(context.runtime, (RubyClass) klazz);
@@ -98,9 +95,10 @@ public class Html4SaxParserContext extends XmlSaxParserContext
9895
String java_encoding = null;
9996
if (encoding != context.runtime.getNil()) {
10097
if (!(encoding instanceof RubyEncoding)) {
98+
// TODO: switch to common undeprecated API when 9.4 adds 10 methods
10199
throw context.runtime.newTypeError("encoding must be kind_of Encoding");
102100
}
103-
java_encoding = ((RubyEncoding)encoding).toString();
101+
java_encoding = encoding.toString();
104102
}
105103

106104
Html4SaxParserContext ctx = Html4SaxParserContext.newInstance(context.runtime, (RubyClass) klass);
@@ -118,15 +116,17 @@ public class Html4SaxParserContext extends XmlSaxParserContext
118116
parse_io(ThreadContext context, IRubyObject klazz, IRubyObject data, IRubyObject encoding)
119117
{
120118
if (!invoke(context, data, "respond_to?", context.runtime.newSymbol("read")).isTrue()) {
119+
// TODO: switch to common undeprecated API when 9.4 adds 10 methods
121120
throw context.runtime.newTypeError("argument expected to respond to :read");
122121
}
123122

124123
String java_encoding = null;
125124
if (encoding != context.runtime.getNil()) {
126125
if (!(encoding instanceof RubyEncoding)) {
126+
// TODO: switch to common undeprecated API when 9.4 adds 10 methods
127127
throw context.runtime.newTypeError("encoding must be kind_of Encoding");
128128
}
129-
java_encoding = ((RubyEncoding)encoding).toString();
129+
java_encoding = encoding.toString();
130130
}
131131

132132
Html4SaxParserContext ctx = Html4SaxParserContext.newInstance(context.runtime, (RubyClass) klazz);

ext/java/nokogiri/Html4SaxPushParser.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
/**
2525
* Class for Nokogiri::HTML4::SAX::PushParser
2626
*
27-
* @author
2827
* @author Piotr Szmielew <p.szmielew@ava.waw.pl> - based on Nokogiri::XML::SAX::PushParser
2928
*/
3029
@JRubyClass(name = "Nokogiri::HTML4::SAX::PushParser")
@@ -93,6 +92,7 @@ public class Html4SaxPushParser extends RubyObject
9392
setOptions(ThreadContext context, IRubyObject opts)
9493
{
9594
invoke(context, parse_options(context), "options=", opts);
95+
// TODO: switch to common undeprecated API when 9.4 adds 10 methods
9696
options = new ParserContext.Options(opts.convertToInteger().getLongValue());
9797
return getOptions(context);
9898
}
@@ -148,14 +148,11 @@ public class Html4SaxPushParser extends RubyObject
148148
assert saxParser != null : "saxParser null";
149149
parserTask = new ParserTask(context, saxParser, stream);
150150
futureTask = new FutureTask<Html4SaxParserContext>((Callable) parserTask);
151-
executor = Executors.newSingleThreadExecutor(new ThreadFactory() {
152-
@Override
153-
public Thread newThread(Runnable r) {
154-
Thread t = new Thread(r);
155-
t.setName("Html4SaxPushParser");
156-
t.setDaemon(true);
157-
return t;
158-
}
151+
executor = Executors.newSingleThreadExecutor(r -> {
152+
Thread t = new Thread(r);
153+
t.setName("Html4SaxPushParser");
154+
t.setDaemon(true);
155+
return t;
159156
});
160157
executor.submit(futureTask);
161158
}
@@ -168,8 +165,6 @@ public Thread newThread(Runnable r) {
168165

169166
try {
170167
terminateImpl();
171-
} catch (InterruptedException e) {
172-
throw runtime.newRuntimeError(e.toString());
173168
} catch (Exception e) {
174169
throw runtime.newRuntimeError(e.toString());
175170
}

ext/java/nokogiri/NokogiriService.java

Lines changed: 19 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import java.util.Map;
66

77
import org.jruby.Ruby;
8-
import org.jruby.RubyArray;
98
import org.jruby.RubyClass;
109
import org.jruby.RubyFixnum;
1110
import org.jruby.RubyModule;
@@ -33,13 +32,14 @@ public class NokogiriService implements BasicLibraryService
3332
public static Map<String, RubyClass>
3433
getNokogiriClassCache(Ruby ruby)
3534
{
35+
// TODO: switch to common undeprecated API when 9.4 adds 10 methods
3636
return (Map<String, RubyClass>) ruby.getModule("Nokogiri").getInternalVariable("cache");
3737
}
3838

3939
private static Map<String, RubyClass>
4040
populateNokogiriClassCache(Ruby ruby)
4141
{
42-
Map<String, RubyClass> nokogiriClassCache = new HashMap<String, RubyClass>();
42+
Map<String, RubyClass> nokogiriClassCache = new HashMap<>();
4343
nokogiriClassCache.put("Nokogiri::HTML4::Document", (RubyClass)ruby.getClassFromPath("Nokogiri::HTML4::Document"));
4444
nokogiriClassCache.put("Nokogiri::HTML4::ElementDescription",
4545
(RubyClass)ruby.getClassFromPath("Nokogiri::HTML4::ElementDescription"));
@@ -78,6 +78,7 @@ public class NokogiriService implements BasicLibraryService
7878
private void
7979
init(Ruby ruby)
8080
{
81+
// TODO: switch to common undeprecated API when 9.4 adds 10 methods
8182
RubyModule nokogiri = ruby.defineModule("Nokogiri");
8283
RubyModule xmlModule = nokogiri.defineModuleUnder("XML");
8384
RubyModule xmlSaxModule = xmlModule.defineModuleUnder("SAX");
@@ -97,6 +98,7 @@ public class NokogiriService implements BasicLibraryService
9798
private void
9899
createSyntaxErrors(Ruby ruby, RubyModule nokogiri, RubyModule xmlModule)
99100
{
101+
// TODO: switch to common undeprecated API when 9.4 adds 10 methods
100102
RubyClass syntaxError = nokogiri.defineClassUnder("SyntaxError", ruby.getStandardError(),
101103
ruby.getStandardError().getAllocator());
102104
RubyClass xmlSyntaxError = xmlModule.defineClassUnder("SyntaxError", syntaxError, XML_SYNTAXERROR_ALLOCATOR);
@@ -106,6 +108,7 @@ public class NokogiriService implements BasicLibraryService
106108
private RubyClass
107109
createXmlModule(Ruby ruby, RubyModule xmlModule)
108110
{
111+
// TODO: switch to common undeprecated API when 9.4 adds 10 methods
109112
RubyClass node = xmlModule.defineClassUnder("Node", ruby.getObject(), XML_NODE_ALLOCATOR);
110113
node.defineAnnotatedMethods(XmlNode.class);
111114

@@ -183,6 +186,7 @@ public class NokogiriService implements BasicLibraryService
183186
private void
184187
createHtmlModule(Ruby ruby, RubyModule htmlModule)
185188
{
189+
// TODO: switch to common undeprecated API when 9.4 adds 10 methods
186190
RubyClass htmlElemDesc = htmlModule.defineClassUnder("ElementDescription", ruby.getObject(),
187191
HTML_ELEMENT_DESCRIPTION_ALLOCATOR);
188192
htmlElemDesc.defineAnnotatedMethods(Html4ElementDescription.class);
@@ -195,6 +199,7 @@ public class NokogiriService implements BasicLibraryService
195199
private void
196200
createDocuments(Ruby ruby, RubyModule xmlModule, RubyModule htmlModule, RubyClass node)
197201
{
202+
// TODO: switch to common undeprecated API when 9.4 adds 10 methods
198203
RubyClass xmlDocument = xmlModule.defineClassUnder("Document", node, XML_DOCUMENT_ALLOCATOR);
199204
xmlDocument.defineAnnotatedMethods(XmlDocument.class);
200205

@@ -206,6 +211,7 @@ public class NokogiriService implements BasicLibraryService
206211
private void
207212
createSaxModule(Ruby ruby, RubyModule xmlSaxModule, RubyModule htmlSaxModule)
208213
{
214+
// TODO: switch to common undeprecated API when 9.4 adds 10 methods
209215
RubyClass xmlSaxParserContext = xmlSaxModule.defineClassUnder("ParserContext", ruby.getObject(),
210216
XML_SAXPARSER_CONTEXT_ALLOCATOR);
211217
xmlSaxParserContext.defineAnnotatedMethods(XmlSaxParserContext.class);
@@ -225,6 +231,7 @@ public class NokogiriService implements BasicLibraryService
225231
private void
226232
createXsltModule(Ruby ruby, RubyModule xsltModule)
227233
{
234+
// TODO: switch to common undeprecated API when 9.4 adds 10 methods
228235
RubyClass stylesheet = xsltModule.defineClassUnder("Stylesheet", ruby.getObject(), XSLT_STYLESHEET_ALLOCATOR);
229236
stylesheet.defineAnnotatedMethods(XsltStylesheet.class);
230237
}
@@ -259,21 +266,9 @@ public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
259266
}
260267
};
261268

262-
private static ObjectAllocator HTML_ELEMENT_DESCRIPTION_ALLOCATOR =
263-
new ObjectAllocator()
264-
{
265-
public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
266-
return new Html4ElementDescription(runtime, klazz);
267-
}
268-
};
269+
private static final ObjectAllocator HTML_ELEMENT_DESCRIPTION_ALLOCATOR = Html4ElementDescription::new;
269270

270-
private static ObjectAllocator HTML_ENTITY_LOOKUP_ALLOCATOR =
271-
new ObjectAllocator()
272-
{
273-
public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
274-
return new Html4EntityLookup(runtime, klazz);
275-
}
276-
};
271+
private static final ObjectAllocator HTML_ENTITY_LOOKUP_ALLOCATOR = Html4EntityLookup::new;
277272

278273
public static final ObjectAllocator XML_ATTR_ALLOCATOR = new ObjectAllocator()
279274
{
@@ -486,25 +481,12 @@ public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
486481
}
487482
};
488483

489-
private static ObjectAllocator XML_ATTRIBUTE_DECL_ALLOCATOR = new ObjectAllocator()
490-
{
491-
public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
492-
return new XmlAttributeDecl(runtime, klazz);
493-
}
494-
};
484+
private static final ObjectAllocator XML_ATTRIBUTE_DECL_ALLOCATOR = XmlAttributeDecl::new;
495485

496-
private static ObjectAllocator XML_ENTITY_DECL_ALLOCATOR = new ObjectAllocator()
497-
{
498-
public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
499-
return new XmlEntityDecl(runtime, klazz);
500-
}
501-
};
486+
private static final ObjectAllocator XML_ENTITY_DECL_ALLOCATOR = XmlEntityDecl::new;
502487

503-
private static ObjectAllocator XML_ELEMENT_CONTENT_ALLOCATOR = new ObjectAllocator()
504-
{
505-
public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
506-
throw runtime.newNotImplementedError("not implemented");
507-
}
488+
private static final ObjectAllocator XML_ELEMENT_CONTENT_ALLOCATOR = (runtime, klazz) -> {
489+
throw runtime.newNotImplementedError("not implemented");
508490
};
509491

510492
public static final ObjectAllocator XML_RELAXNG_ALLOCATOR = new ObjectAllocator()
@@ -537,19 +519,9 @@ public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
537519
}
538520
};
539521

540-
private static final ObjectAllocator XML_SAXPUSHPARSER_ALLOCATOR = new ObjectAllocator()
541-
{
542-
public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
543-
return new XmlSaxPushParser(runtime, klazz);
544-
}
545-
};
522+
private static final ObjectAllocator XML_SAXPUSHPARSER_ALLOCATOR = XmlSaxPushParser::new;
546523

547-
private static final ObjectAllocator HTML_SAXPUSHPARSER_ALLOCATOR = new ObjectAllocator()
548-
{
549-
public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
550-
return new Html4SaxPushParser(runtime, klazz);
551-
}
552-
};
524+
private static final ObjectAllocator HTML_SAXPUSHPARSER_ALLOCATOR = Html4SaxPushParser::new;
553525

554526
public static final ObjectAllocator XML_SCHEMA_ALLOCATOR = new ObjectAllocator()
555527
{
@@ -566,12 +538,7 @@ public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
566538
}
567539
};
568540

569-
public static final ObjectAllocator XML_SYNTAXERROR_ALLOCATOR = new ObjectAllocator()
570-
{
571-
public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
572-
return new XmlSyntaxError(runtime, klazz);
573-
}
574-
};
541+
public static final ObjectAllocator XML_SYNTAXERROR_ALLOCATOR = XmlSyntaxError::new;
575542

576543
public static final ObjectAllocator XML_TEXT_ALLOCATOR = new ObjectAllocator()
577544
{
@@ -588,12 +555,7 @@ public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
588555
}
589556
};
590557

591-
public static final ObjectAllocator XML_XPATHCONTEXT_ALLOCATOR = new ObjectAllocator()
592-
{
593-
public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
594-
return new XmlXpathContext(runtime, klazz);
595-
}
596-
};
558+
public static final ObjectAllocator XML_XPATHCONTEXT_ALLOCATOR = XmlXpathContext::new;
597559

598560
public static ObjectAllocator XSLT_STYLESHEET_ALLOCATOR = new ObjectAllocator()
599561
{

ext/java/nokogiri/XmlAttr.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ public class XmlAttr extends XmlNode
2727
{
2828
private static final long serialVersionUID = 1L;
2929

30+
// unused
31+
@Deprecated
3032
public static final String[] HTML_BOOLEAN_ATTRS = {
3133
"checked", "compact", "declare", "defer", "disabled", "ismap",
3234
"multiple", "nohref", "noresize", "noshade", "nowrap", "readonly",
@@ -45,6 +47,8 @@ public class XmlAttr extends XmlNode
4547
super(ruby, rubyClass);
4648
}
4749

50+
// unused
51+
@Deprecated
4852
public
4953
XmlAttr(Ruby ruby, RubyClass rubyClass, Node attr)
5054
{
@@ -56,6 +60,7 @@ public class XmlAttr extends XmlNode
5660
init(ThreadContext context, IRubyObject[] args)
5761
{
5862
if (args.length < 2) {
63+
// TODO: switch to common undeprecated API when 9.4 adds 10 methods
5964
throw context.runtime.newArgumentError(args.length, 2);
6065
}
6166

0 commit comments

Comments
 (0)