Skip to content

Commit

Permalink
Merge pull request #4183 from lcbarcellos/master
Browse files Browse the repository at this point in the history
Fix: issue #2751 new HashSet()" != "new HashSet<>()
  • Loading branch information
jlerbsc committed Oct 29, 2023
2 parents 16be06e + a0e4072 commit 4249ae0
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,66 @@ void testCustomXML() throws SAXException, IOException, XMLStreamException {
stringWriter.toString()
);
}

@Test
void testAbsentTypeParameterList() throws SAXException, IOException, XMLStreamException {
Expression expression = parseExpression("new HashSet()");
XmlPrinter xmlOutput = new XmlPrinter(false);
String output = xmlOutput.output(expression);
assertXMLEquals(""
// Expected
+ "<root>"
+ "<type>"
+ "<name identifier='HashSet'/>"
+ "</type>"
+ "</root>",
// Actual
output
);
}

@Test
void testEmptyTypeParameterList() throws SAXException, IOException, XMLStreamException {
Expression expression = parseExpression("new HashSet<>()");
XmlPrinter xmlOutput = new XmlPrinter(false);
String output = xmlOutput.output(expression);
assertXMLEquals(""
// Expected
+ "<root>"
+ "<type>"
+ "<name identifier='HashSet'/>"
+ "<typeArguments/>"
+ "</type>"
+ "</root>",
// Actual
output
);
}

@Test
void testNonEmptyTypeParameterList() throws SAXException, IOException, XMLStreamException {
Expression expression = parseExpression("new HashSet<Integer,File>()");
XmlPrinter xmlOutput = new XmlPrinter(false);
String output = xmlOutput.output(expression);
assertXMLEquals(""
// Expected
+ "<root>"
+ "<type>"
+ "<name identifier='HashSet'/>"
+ "<typeArguments>"
+ "<typeArgument>"
+ "<name identifier='Integer'/>"
+ "</typeArgument>"
+ "<typeArgument>"
+ "<name identifier='File'/>"
+ "</typeArgument>"
+ "</typeArguments>"
+ "</type>"
+ "</root>",
// Actual
output
);
}
}

interface Cleanup {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.type.Type;
import com.github.javaparser.metamodel.NodeMetaModel;
import com.github.javaparser.metamodel.PropertyMetaModel;

Expand All @@ -42,6 +43,7 @@
public class XmlPrinter {

private final boolean outputNodeType;
private static final Class<?> TYPE_CLASS = Type.class;

public XmlPrinter(boolean outputNodeType) {
this.outputNodeType = outputNodeType;
Expand Down Expand Up @@ -178,6 +180,8 @@ public void outputNode(Node node, String name, XMLStreamWriter xmlWriter) throws
Predicate<PropertyMetaModel> nonNullNode = propertyMetaModel -> propertyMetaModel.getValue(node) != null;
Predicate<PropertyMetaModel> nonEmptyList = propertyMetaModel ->
((NodeList) propertyMetaModel.getValue(node)).isNonEmpty();
Predicate<PropertyMetaModel> typeList = propertyMetaModel ->
TYPE_CLASS == propertyMetaModel.getType();

xmlWriter.writeStartElement(name);

Expand Down Expand Up @@ -220,7 +224,7 @@ public void outputNode(Node node, String name, XMLStreamWriter xmlWriter) throws
allPropertyMetaModels.stream()
.filter(PropertyMetaModel::isNodeList)
.filter(nonNullNode)
.filter(nonEmptyList)
.filter(nonEmptyList.or(typeList))
.forEach(listMetaModel -> {
try {
String listName = listMetaModel.getName();
Expand Down

0 comments on commit 4249ae0

Please sign in to comment.