Skip to content

Commit

Permalink
Merge pull request #1222 from robertpanzer/fix-1221
Browse files Browse the repository at this point in the history
Fixes #1221. 'UnsupportedOperationException' when passing immutable Map as options to 'createPhraseNode'
  • Loading branch information
robertpanzer authored Jun 22, 2023
2 parents 256bf5e + cfa68db commit 58c0226
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 40 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ For a detailed view of what has changed, refer to the {url-repo}/commits/main[co

== Unreleased

Bug Fixes::

* 'UnsupportedOperationException' when passing immutable Map as options to 'createPhraseNode' (#1221) (@abelsromero)

Improvement::

* BREAKING: Fix Macro APIs to take StructuralNodes and return Phrase- or StructuralNodes. (#1084)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,10 @@ public Block createBlock(StructuralNode parent, String context, String content,

@Override
public Block createBlock(StructuralNode parent, String context, String content, Map<String, Object> attributes, Map<Object, Object> options) {
options.put(Options.SOURCE, content);
options.put(Options.ATTRIBUTES, attributes);
return createBlock(parent, context, options);
Map<Object, Object> optionsCopy = new HashMap<>(options);
optionsCopy.put(Options.SOURCE, content);
optionsCopy.put(Options.ATTRIBUTES, attributes);
return createBlock(parent, context, optionsCopy);
}

@Override
Expand All @@ -237,11 +238,11 @@ public Block createBlock(StructuralNode parent, String context, List<String> con
@Override
public Block createBlock(StructuralNode parent, String context, List<String> content, Map<String, Object> attributes,
Map<Object, Object> options) {
Map<Object, Object> optionsCopy = new HashMap<>(options);
optionsCopy.put(Options.SOURCE, content);
optionsCopy.put(Options.ATTRIBUTES, new HashMap<>(attributes));

options.put(Options.SOURCE, content);
options.put(Options.ATTRIBUTES, new HashMap<>(attributes));

return createBlock(parent, context, options);
return createBlock(parent, context, optionsCopy);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,30 +159,33 @@ public Cell createTableCell(Column column, String text, Map<String, Object> attr
public Block createBlock(StructuralNode parent, String context, String content, Map<String, Object> attributes,
Map<Object, Object> options) {

options.put(Options.SOURCE, content);
options.put(Options.ATTRIBUTES, attributes);
Map<Object, Object> 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<String> content, Map<String, Object> attributes,
Map<Object, Object> options) {

options.put(Options.SOURCE, content);
options.put(Options.ATTRIBUTES, new HashMap<>(attributes));
return createBlock(parent, context, options);
Map<Object, Object> optionsCopy = new HashMap<>(options);
optionsCopy.put(Options.SOURCE, content);
optionsCopy.put(Options.ATTRIBUTES, new HashMap<>(attributes));
return createBlock(parent, context, optionsCopy);
}

@Override
public PhraseNode createPhraseNode(ContentNode parent, String context, List<String> text, Map<String, Object> attributes, Map<Object, Object> options) {

Ruby rubyRuntime = JRubyRuntimeContext.get(parent);

options.put(Options.ATTRIBUTES, attributes);
Map<Object, Object> optionsCopy = new HashMap<>(options);
optionsCopy.put(Options.ATTRIBUTES, attributes);

RubyHash convertMapToRubyHashWithSymbols = RubyHashUtil.convertMapToRubyHashWithSymbolsIfNecessary(rubyRuntime,
options);
optionsCopy);

RubyArray rubyText = rubyRuntime.newArray();
rubyText.addAll(text);
Expand All @@ -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<String, Object> 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(),
Expand Down Expand Up @@ -306,8 +310,9 @@ public org.asciidoctor.ast.List createList(StructuralNode parent, String context
Map<String, Object> attributes,
Map<Object, Object> options) {

options.put(Options.ATTRIBUTES, new HashMap<>(attributes));
return createList(parent, context, options);
HashMap<Object, Object> optionsCopy = new HashMap<>(options);
optionsCopy.put(Options.ATTRIBUTES, new HashMap<>(attributes));
return createList(parent, context, optionsCopy);
}

@Override
Expand All @@ -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));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
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> T rubyToJava(Ruby runtime, IRubyObject rubyObject, Class<T> returnType) {
return (T) org.jruby.javasupport.JavaEmbedUtils.rubyToJava(runtime, rubyObject, returnType);
}

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) {
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -19,9 +18,9 @@ public ManpageMacro(String macroName, Map<String, Object> config) {
@Override
public PhraseNode process(StructuralNode parent, String target,
Map<String, Object> attributes) {
Map<String, Object> options = new HashMap<String, Object>();
options.put("type", ":link");
options.put("target", target + ".html");
Map<String, Object> options = Map.of(
"type", ":link",
"target", target + ".html");
return createPhraseNode(parent, "anchor", target, attributes, options);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<String> lines = ((Block) currentBlock).getLines();
if (lines.size() > 0 && lines.get(0).startsWith("$")) {
Expand All @@ -57,15 +57,15 @@ public Block convertToTerminalListing(Block block) {
for (String line : lines) {
if (line.startsWith("$")) {
resultLines.append("<span class=\"command\">")
.append(line.substring(2, line.length()))
.append(line.substring(2))
.append("</span>");
} else {
resultLines.append(line);
}
}

return createBlock(this.document, "listing",
List.of(resultLines.toString()), attributes, new HashMap<>());
List.of(resultLines.toString()), attributes, Collections.emptyMap());
}

}

0 comments on commit 58c0226

Please sign in to comment.