diff --git a/src/main/java/org/jd/core/v1/service/converter/classfiletojavasyntax/visitor/BindTypesToTypesVisitor.java b/src/main/java/org/jd/core/v1/service/converter/classfiletojavasyntax/visitor/BindTypesToTypesVisitor.java index b9a8e1d..a13a1d8 100644 --- a/src/main/java/org/jd/core/v1/service/converter/classfiletojavasyntax/visitor/BindTypesToTypesVisitor.java +++ b/src/main/java/org/jd/core/v1/service/converter/classfiletojavasyntax/visitor/BindTypesToTypesVisitor.java @@ -111,7 +111,7 @@ public void visit(InnerObjectType type) { public void visit(GenericType type) { TypeArgument ta = bindings.get(type.getName()); - if ((ta == null) || (ta == WildcardTypeArgument.WILDCARD_TYPE_ARGUMENT)) { + if (ta == null || ta == WildcardTypeArgument.WILDCARD_TYPE_ARGUMENT) { result = TYPE_OBJECT.createType(type.getDimension()); } else { typeArgumentToTypeVisitor.init(); diff --git a/src/test/java/org/jd/core/v1/service/converter/classfiletojavasyntax/util/TypeMakerTest.java b/src/test/java/org/jd/core/v1/service/converter/classfiletojavasyntax/util/TypeMakerTest.java index a6134c1..3549757 100644 --- a/src/test/java/org/jd/core/v1/service/converter/classfiletojavasyntax/util/TypeMakerTest.java +++ b/src/test/java/org/jd/core/v1/service/converter/classfiletojavasyntax/util/TypeMakerTest.java @@ -58,7 +58,7 @@ @SuppressWarnings("all") public class TypeMakerTest extends TestCase { - protected TypeMaker typeMaker = new TypeMaker(new ClassPathLoader()); + protected TypeMaker typeMaker = new TypeMaker(); protected ObjectType otAbstractUntypedIteratorDecorator = makeObjectType(AbstractUntypedIteratorDecorator.class); protected ObjectType otArrayList = makeObjectType(ArrayList.class); @@ -720,12 +720,14 @@ public void testSearchSuperParameterizedType() throws Exception { ObjectType treeMap = typeMaker.makeFromDescriptorOrInternalTypeName("java/util/TreeMap"); ObjectType abstMap = typeMaker.makeFromDescriptorOrInternalTypeName("java/util/AbstractMap"); ObjectType set = typeMaker.makeFromDescriptorOrInternalTypeName("java/util/Set"); + ObjectType genericSet = set.createType(new GenericType("V")); ObjectType unmodifiableEntrySet = typeMaker.makeFromInternalTypeName("org/apache/commons/collections4/map/UnmodifiableEntrySet"); ObjectType mapEntry = typeMaker.makeFromDescriptorOrInternalTypeName("java/util/Map$Entry"); + ObjectType valueView = typeMaker.makeFromInternalTypeName("org/apache/commons/collections4/bidimap/TreeBidiMap$ValueView"); TypeArguments typeArguments = new TypeArguments(Arrays.asList(new GenericType("K"), new GenericType("V"))); mapEntry = mapEntry.createType(typeArguments); unmodifiableEntrySet = unmodifiableEntrySet.createType(typeArguments); - set = set.createType(mapEntry); + ObjectType mapEntrySet = set.createType(mapEntry); assertEquals(abstMap, typeMaker.searchSuperParameterizedType(abstMap, hashMap)); assertEquals(abstMap, typeMaker.searchSuperParameterizedType(abstMap, treeMap)); assertEquals(treeMap, typeMaker.searchSuperParameterizedType(ObjectType.TYPE_UNDEFINED_OBJECT, treeMap)); @@ -733,7 +735,8 @@ public void testSearchSuperParameterizedType() throws Exception { assertEquals(ObjectType.TYPE_CLASS, typeMaker.searchSuperParameterizedType(ObjectType.TYPE_CLASS, ObjectType.TYPE_CLASS)); assertNull(typeMaker.searchSuperParameterizedType((ObjectType) ObjectType.TYPE_CLASS.createType(1), ObjectType.TYPE_CLASS)); assertNull(typeMaker.searchSuperParameterizedType(ObjectType.TYPE_CLASS, (ObjectType) ObjectType.TYPE_CLASS.createType(1))); - assertEquals(set, typeMaker.searchSuperParameterizedType(set, unmodifiableEntrySet)); + assertEquals(mapEntrySet, typeMaker.searchSuperParameterizedType(set, unmodifiableEntrySet)); + assertEquals(set, typeMaker.searchSuperParameterizedType(genericSet, valueView)); } @Test diff --git a/src/test/java/org/jd/core/v1/service/converter/classfiletojavasyntax/visitor/BindTypesToTypesVisitorTest.java b/src/test/java/org/jd/core/v1/service/converter/classfiletojavasyntax/visitor/BindTypesToTypesVisitorTest.java new file mode 100644 index 0000000..d56a3e0 --- /dev/null +++ b/src/test/java/org/jd/core/v1/service/converter/classfiletojavasyntax/visitor/BindTypesToTypesVisitorTest.java @@ -0,0 +1,100 @@ +package org.jd.core.v1.service.converter.classfiletojavasyntax.visitor; + +import org.jd.core.v1.model.javasyntax.type.BaseType; +import org.jd.core.v1.model.javasyntax.type.GenericType; +import org.jd.core.v1.model.javasyntax.type.InnerObjectType; +import org.jd.core.v1.model.javasyntax.type.ObjectType; +import org.jd.core.v1.model.javasyntax.type.TypeArgument; +import org.jd.core.v1.model.javasyntax.type.WildcardTypeArgument; +import org.jd.core.v1.service.converter.classfiletojavasyntax.util.TypeMaker; +import org.junit.Before; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +import static org.junit.Assert.assertEquals; + +public class BindTypesToTypesVisitorTest { + private BindTypesToTypesVisitor visitor; + private Map bindings; + private TypeMaker typeMaker; + + @Before + public void setup() { + visitor = new BindTypesToTypesVisitor(); + bindings = new HashMap<>(); + typeMaker = new TypeMaker(); + visitor.setBindings(bindings); + } + + @Test + public void visitGenericTypeWhenTypeArgumentIsNull() { + // Given + GenericType genericType = new GenericType("Test", 0); + bindings.put("Test", null); + + // When + visitor.init(); + genericType.accept(visitor); + + // Then + BaseType result = visitor.getType(); + assertEquals(ObjectType.TYPE_OBJECT.createType(0), result); + } + + @Test + public void visitGenericTypeWhenTypeArgumentIsWildcard() { + // Given + GenericType genericType = new GenericType("Test", 0); + bindings.put("Test", WildcardTypeArgument.WILDCARD_TYPE_ARGUMENT); + + // When + visitor.init(); + genericType.accept(visitor); + + // Then + BaseType result = visitor.getType(); + assertEquals(ObjectType.TYPE_OBJECT.createType(0), result); + } + + @Test + public void visitGenericTypeWhenTypeArgumentIsNotWildcardNorNull() { + // Given + GenericType genericType = new GenericType("Test", 0); + ObjectType typeArgument = ObjectType.TYPE_OBJECT; + bindings.put("Test", typeArgument); + + // When + visitor.init(); + genericType.accept(visitor); + + // Then + BaseType result = visitor.getType(); + + // Assuming that result is equivalent to what + // typeArgumentToTypeVisitor.getType() returns, with increased dimension + assertEquals(typeArgument.createType(0 + genericType.getDimension()), result); + } + + @Test + public void visitInnerObjectTypeWhenResultIsNotEqualOuterTypeAndTypeArgumentsIsNotWildcardNorNull() { + // Given + ObjectType outerType = typeMaker.makeFromInternalTypeName("OuterType"); + InnerObjectType innerObjectType = (InnerObjectType) typeMaker.makeFromInternalTypeName("OuterType$InnerType"); + + outerType.accept(visitor); + + // When + visitor.init(); + innerObjectType.accept(visitor); + + // Then + BaseType result = visitor.getType(); + + // Assuming that when typeArguments is not WildcardTypeArgument or null and + // result is not equal to outerType, + // the result is a new InnerObjectType with the same typeArguments + assertEquals(innerObjectType, result); + } +}