From e59b2636ddbd4a394d7b544252ef7ed778f14f2c Mon Sep 17 00:00:00 2001 From: Robert Panzer Date: Sun, 18 Jun 2023 17:44:12 +0200 Subject: [PATCH] Fixes #1221. Create mutable copies of options passed to Processor.create...() --- .../extension/internal/JRubyProcessor.java | 33 +++++++++++-------- .../asciidoctor/jruby/internal/RubyUtils.java | 15 +++------ .../asciidoctor/extension/ManpageMacro.java | 7 ++-- .../TerminalCommandTreeprocessor.java | 8 ++--- 4 files changed, 30 insertions(+), 33 deletions(-) diff --git a/asciidoctorj-core/src/main/java/org/asciidoctor/jruby/extension/internal/JRubyProcessor.java b/asciidoctorj-core/src/main/java/org/asciidoctor/jruby/extension/internal/JRubyProcessor.java index 3417a70d1..cd568a867 100644 --- a/asciidoctorj-core/src/main/java/org/asciidoctor/jruby/extension/internal/JRubyProcessor.java +++ b/asciidoctorj-core/src/main/java/org/asciidoctor/jruby/extension/internal/JRubyProcessor.java @@ -159,19 +159,21 @@ public Cell createTableCell(Column column, String text, Map attr public Block createBlock(StructuralNode parent, String context, String content, Map attributes, Map options) { - options.put(Options.SOURCE, content); - options.put(Options.ATTRIBUTES, attributes); + Map optionsCopy = new HashMap<>(options); + optionsCopy.put(Options.SOURCE, content); + optionsCopy.put(Options.ATTRIBUTES, attributes); - return createBlock(parent, context, options); + return createBlock(parent, context, optionsCopy); } @Override public Block createBlock(StructuralNode parent, String context, List content, Map attributes, Map options) { - options.put(Options.SOURCE, content); - options.put(Options.ATTRIBUTES, new HashMap<>(attributes)); - return createBlock(parent, context, options); + Map optionsCopy = new HashMap<>(options); + optionsCopy.put(Options.SOURCE, content); + optionsCopy.put(Options.ATTRIBUTES, new HashMap<>(attributes)); + return createBlock(parent, context, optionsCopy); } @Override @@ -179,10 +181,11 @@ public PhraseNode createPhraseNode(ContentNode parent, String context, List optionsCopy = new HashMap<>(options); + optionsCopy.put(Options.ATTRIBUTES, attributes); RubyHash convertMapToRubyHashWithSymbols = RubyHashUtil.convertMapToRubyHashWithSymbolsIfNecessary(rubyRuntime, - options); + optionsCopy); RubyArray rubyText = rubyRuntime.newArray(); rubyText.addAll(text); @@ -200,9 +203,10 @@ public PhraseNode createPhraseNode(ContentNode parent, String context, String te Ruby rubyRuntime = JRubyRuntimeContext.get(parent); - options.put(Options.ATTRIBUTES, RubyHashUtil.convertMapToRubyHashWithStrings(rubyRuntime, attributes)); + Map optionsCopy = new HashMap<>(options); + optionsCopy.put(Options.ATTRIBUTES, RubyHashUtil.convertMapToRubyHashWithStrings(rubyRuntime, attributes)); - RubyHash convertedOptions = RubyHashUtil.convertMapToRubyHashWithSymbols(rubyRuntime, options); + RubyHash convertedOptions = RubyHashUtil.convertMapToRubyHashWithSymbols(rubyRuntime, optionsCopy); IRubyObject[] parameters = { ((ContentNodeImpl) parent).getRubyObject(), @@ -306,8 +310,9 @@ public org.asciidoctor.ast.List createList(StructuralNode parent, String context Map attributes, Map options) { - options.put(Options.ATTRIBUTES, new HashMap<>(attributes)); - return createList(parent, context, options); + HashMap optionsCopy = new HashMap<>(options); + optionsCopy.put(Options.ATTRIBUTES, new HashMap<>(attributes)); + return createList(parent, context, optionsCopy); } @Override @@ -330,14 +335,14 @@ public org.asciidoctor.ast.List createList(StructuralNode parent, String context public ListItem createListItem(final org.asciidoctor.ast.List parent, final String text) { Ruby rubyRuntime = JRubyRuntimeContext.get(parent); - return (ListItem) NodeConverter.createASTNode(rubyRuntime, NodeConverter.NodeType.LIST_ITEM_CLASS, ListImpl.class.cast(parent).getRubyObject(), rubyRuntime.newString(text)); + return (ListItem) NodeConverter.createASTNode(rubyRuntime, NodeConverter.NodeType.LIST_ITEM_CLASS, ((ListImpl) parent).getRubyObject(), rubyRuntime.newString(text)); } @Override public ListItem createListItem(final org.asciidoctor.ast.DescriptionList parent, final String text) { Ruby rubyRuntime = JRubyRuntimeContext.get(parent); - return (ListItem) NodeConverter.createASTNode(rubyRuntime, NodeConverter.NodeType.LIST_ITEM_CLASS, DescriptionListImpl.class.cast(parent).getRubyObject(), rubyRuntime.newString(text)); + return (ListItem) NodeConverter.createASTNode(rubyRuntime, NodeConverter.NodeType.LIST_ITEM_CLASS, ((DescriptionListImpl) parent).getRubyObject(), rubyRuntime.newString(text)); } /** diff --git a/asciidoctorj-core/src/main/java/org/asciidoctor/jruby/internal/RubyUtils.java b/asciidoctorj-core/src/main/java/org/asciidoctor/jruby/internal/RubyUtils.java index ee8971047..27067c3af 100644 --- a/asciidoctorj-core/src/main/java/org/asciidoctor/jruby/internal/RubyUtils.java +++ b/asciidoctorj-core/src/main/java/org/asciidoctor/jruby/internal/RubyUtils.java @@ -1,13 +1,11 @@ package org.asciidoctor.jruby.internal; -import java.io.InputStream; - import org.jruby.Ruby; -import org.jruby.RubyClass; import org.jruby.RubySymbol; -import org.jruby.javasupport.JavaClass; import org.jruby.runtime.builtin.IRubyObject; +import java.io.InputStream; + public class RubyUtils { public static T rubyToJava(Ruby runtime, IRubyObject rubyObject, Class returnType) { @@ -15,12 +13,7 @@ public static T rubyToJava(Ruby runtime, IRubyObject rubyObject, Class re } public static RubySymbol toSymbol(Ruby rubyRuntime, String key) { - RubySymbol newSymbol = RubySymbol.newSymbol(rubyRuntime, key); - return newSymbol; - } - - public static RubyClass toRubyClass(Ruby rubyRuntime, Class rubyClass) { - return JavaClass.get(rubyRuntime, rubyClass).getProxyClass(); + return RubySymbol.newSymbol(rubyRuntime, key); } public static void requireLibrary(Ruby rubyRuntime, String require) { @@ -32,7 +25,7 @@ public static void loadRubyClass(Ruby rubyRuntime, InputStream rubyClassDefiniti rubyRuntime.evalScriptlet(script); } - public static final void setGlobalVariable(Ruby rubyRuntime, String variableName, Object variableValue) { + public static void setGlobalVariable(Ruby rubyRuntime, String variableName, Object variableValue) { String script = String.format("$%s = %s", variableName, variableValue); rubyRuntime.evalScriptlet(script); } diff --git a/asciidoctorj-core/src/test/java/org/asciidoctor/extension/ManpageMacro.java b/asciidoctorj-core/src/test/java/org/asciidoctor/extension/ManpageMacro.java index 5749be660..7457bf53c 100644 --- a/asciidoctorj-core/src/test/java/org/asciidoctor/extension/ManpageMacro.java +++ b/asciidoctorj-core/src/test/java/org/asciidoctor/extension/ManpageMacro.java @@ -3,7 +3,6 @@ import org.asciidoctor.ast.PhraseNode; import org.asciidoctor.ast.StructuralNode; -import java.util.HashMap; import java.util.Map; public class ManpageMacro extends InlineMacroProcessor { @@ -19,9 +18,9 @@ public ManpageMacro(String macroName, Map config) { @Override public PhraseNode process(StructuralNode parent, String target, Map attributes) { - Map options = new HashMap(); - options.put("type", ":link"); - options.put("target", target + ".html"); + Map options = Map.of( + "type", ":link", + "target", target + ".html"); return createPhraseNode(parent, "anchor", target, attributes, options); } diff --git a/asciidoctorj-core/src/test/java/org/asciidoctor/extension/TerminalCommandTreeprocessor.java b/asciidoctorj-core/src/test/java/org/asciidoctor/extension/TerminalCommandTreeprocessor.java index 6881c2fc2..7b419ec91 100644 --- a/asciidoctorj-core/src/test/java/org/asciidoctor/extension/TerminalCommandTreeprocessor.java +++ b/asciidoctorj-core/src/test/java/org/asciidoctor/extension/TerminalCommandTreeprocessor.java @@ -4,7 +4,7 @@ import org.asciidoctor.ast.Document; import org.asciidoctor.ast.StructuralNode; -import java.util.HashMap; +import java.util.Collections; import java.util.List; import java.util.Map; @@ -32,7 +32,7 @@ private void processBlock(StructuralNode block) { for (int i = 0; i < blocks.size(); i++) { final StructuralNode currentBlock = blocks.get(i); - if (currentBlock instanceof StructuralNode) { + if (currentBlock != null) { if ("paragraph".equals(currentBlock.getContext())) { List lines = ((Block) currentBlock).getLines(); if (lines.size() > 0 && lines.get(0).startsWith("$")) { @@ -57,7 +57,7 @@ public Block convertToTerminalListing(Block block) { for (String line : lines) { if (line.startsWith("$")) { resultLines.append("") - .append(line.substring(2, line.length())) + .append(line.substring(2)) .append(""); } else { resultLines.append(line); @@ -65,7 +65,7 @@ public Block convertToTerminalListing(Block block) { } return createBlock(this.document, "listing", - List.of(resultLines.toString()), attributes, new HashMap<>()); + List.of(resultLines.toString()), attributes, Collections.emptyMap()); } }