From 7fa1b8f171bca1b509e8aa4e49124226b31f5e9c Mon Sep 17 00:00:00 2001 From: emmanue1 Date: Fri, 1 Nov 2019 15:19:27 +0100 Subject: [PATCH] Quick fixes --- build.gradle | 2 +- .../expression/ArrayExpression.java | 2 - .../v1/model/javasyntax/type/ObjectType.java | 2 +- .../model/javasyntax/type/PrimitiveType.java | 2 +- .../localvariable/ObjectLocalVariable.java | 12 ++++-- .../util/ByteCodeParser.java | 7 ++-- .../classfiletojavasyntax/util/TypeMaker.java | 6 +-- .../TypeParametersToTypeArgumentsBinder.java | 31 ++++++++------ .../visitor/AddCastExpressionVisitor.java | 19 ++++++--- ...dTypeParametersToTypeArgumentsVisitor.java | 3 +- ...pulateBindingsWithTypeArgumentVisitor.java | 41 +++++++++++-------- 11 files changed, 76 insertions(+), 51 deletions(-) diff --git a/build.gradle b/build.gradle index 6c46835a..5f441c7a 100644 --- a/build.gradle +++ b/build.gradle @@ -9,7 +9,7 @@ dependencies { testCompile 'org.apache.commons:commons-collections4:4.1' } -version='1.1.0' +version='1.1.1' tasks.withType(JavaCompile) { sourceCompatibility = targetCompatibility = '1.8' diff --git a/src/main/java/org/jd/core/v1/model/javasyntax/expression/ArrayExpression.java b/src/main/java/org/jd/core/v1/model/javasyntax/expression/ArrayExpression.java index aaa0ac7f..1ac38159 100644 --- a/src/main/java/org/jd/core/v1/model/javasyntax/expression/ArrayExpression.java +++ b/src/main/java/org/jd/core/v1/model/javasyntax/expression/ArrayExpression.java @@ -52,8 +52,6 @@ protected static Type createItemType(Expression expression) { Type type = expression.getType(); int dimension = type.getDimension(); - assert (type == TYPE_UNDEFINED_OBJECT) || (dimension > 0) : "ArrayExpression.createItemType(exp) : zero or negative dimension"; - return type.createType((dimension > 0) ? dimension-1 : 0); } diff --git a/src/main/java/org/jd/core/v1/model/javasyntax/type/ObjectType.java b/src/main/java/org/jd/core/v1/model/javasyntax/type/ObjectType.java index 258d420e..b9ec7d46 100644 --- a/src/main/java/org/jd/core/v1/model/javasyntax/type/ObjectType.java +++ b/src/main/java/org/jd/core/v1/model/javasyntax/type/ObjectType.java @@ -137,7 +137,7 @@ public int getDimension() { @Override public Type createType(int dimension) { - assert dimension >= 0 : "ObjectType.createType(dim) : create type with zero or negative dimension"; + assert dimension >= 0 : "ObjectType.createType(dim) : create type with negative dimension"; if (this.dimension == dimension) { return this; diff --git a/src/main/java/org/jd/core/v1/model/javasyntax/type/PrimitiveType.java b/src/main/java/org/jd/core/v1/model/javasyntax/type/PrimitiveType.java index f9e236ca..4eedc364 100644 --- a/src/main/java/org/jd/core/v1/model/javasyntax/type/PrimitiveType.java +++ b/src/main/java/org/jd/core/v1/model/javasyntax/type/PrimitiveType.java @@ -123,7 +123,7 @@ public int getRightFlags() { @Override public Type createType(int dimension) { - assert dimension >= 0 : "PrimitiveType.createType(dim) : create type with zero or negative dimension"; + assert dimension >= 0 : "PrimitiveType.createType(dim) : create type with negative dimension"; if (dimension == 0) { return this; } else { diff --git a/src/main/java/org/jd/core/v1/service/converter/classfiletojavasyntax/model/localvariable/ObjectLocalVariable.java b/src/main/java/org/jd/core/v1/service/converter/classfiletojavasyntax/model/localvariable/ObjectLocalVariable.java index ad370b50..2dabbff0 100644 --- a/src/main/java/org/jd/core/v1/service/converter/classfiletojavasyntax/model/localvariable/ObjectLocalVariable.java +++ b/src/main/java/org/jd/core/v1/service/converter/classfiletojavasyntax/model/localvariable/ObjectLocalVariable.java @@ -79,11 +79,15 @@ public String toString() { @Override public boolean isAssignableFrom(Type type) { - if (type.isObject()) { - return typeMaker.isAssignable((ObjectType) this.type, (ObjectType) type); - } else if (type.isGeneric()) { + if (this.type.isObject()) { if (this.type.equals(TYPE_OBJECT)) { - return true; + if ((type.getDimension() > 0) || !type.isPrimitive()) { + return true; + } + } + + if (type.isObject()) { + return typeMaker.isAssignable((ObjectType) this.type, (ObjectType) type); } } diff --git a/src/main/java/org/jd/core/v1/service/converter/classfiletojavasyntax/util/ByteCodeParser.java b/src/main/java/org/jd/core/v1/service/converter/classfiletojavasyntax/util/ByteCodeParser.java index 17912868..35d03973 100644 --- a/src/main/java/org/jd/core/v1/service/converter/classfiletojavasyntax/util/ByteCodeParser.java +++ b/src/main/java/org/jd/core/v1/service/converter/classfiletojavasyntax/util/ByteCodeParser.java @@ -37,6 +37,7 @@ import static org.jd.core.v1.model.javasyntax.declaration.Declaration.FLAG_STATIC; import static org.jd.core.v1.model.javasyntax.declaration.Declaration.FLAG_SYNTHETIC; import static org.jd.core.v1.model.javasyntax.statement.ReturnStatement.RETURN; +import static org.jd.core.v1.model.javasyntax.type.ObjectType.TYPE_OBJECT; import static org.jd.core.v1.model.javasyntax.type.ObjectType.TYPE_UNDEFINED_OBJECT; import static org.jd.core.v1.model.javasyntax.type.PrimitiveType.*; @@ -261,7 +262,7 @@ public void parse(BasicBlock basicBlock, Statements statements, DefaultStack bindings = new HashMap<>(); - if (rightTypeTypes.typeParameters.isList()) { + if (rightTypeTypes.typeParameters.isList() && objectType.getTypeArguments().isTypeArgumentList()) { Iterator iteratorTypeParameter = rightTypeTypes.typeParameters.iterator(); Iterator iteratorTypeArgument = objectType.getTypeArguments().getTypeArgumentList().iterator(); @@ -1213,7 +1213,7 @@ private Type loadFieldType(ObjectType objectType, String fieldName, String descr BindTypeParametersToTypeArgumentsVisitor bindTypeParametersToTypeArgumentsVisitor = new BindTypeParametersToTypeArgumentsVisitor(); HashMap bindings = new HashMap<>(); - if (typeTypes.typeParameters.isList()) { + if (typeTypes.typeParameters.isList() && typeArguments.isTypeArgumentList()) { Iterator iteratorTypeParameter = typeTypes.typeParameters.iterator(); Iterator iteratorTypeArgument = typeArguments.getTypeArgumentList().iterator(); @@ -1303,7 +1303,7 @@ private MethodTypes loadMethodTypes(ObjectType objectType, String methodName, St HashMap bindings = new HashMap<>(); MethodTypes newMethodTypes = new MethodTypes(); - if (typeTypes.typeParameters.isList()) { + if (typeTypes.typeParameters.isList() && typeArguments.isTypeArgumentList()) { Iterator iteratorTypeParameter = typeTypes.typeParameters.iterator(); Iterator iteratorTypeArgument = typeArguments.getTypeArgumentList().iterator(); diff --git a/src/main/java/org/jd/core/v1/service/converter/classfiletojavasyntax/util/TypeParametersToTypeArgumentsBinder.java b/src/main/java/org/jd/core/v1/service/converter/classfiletojavasyntax/util/TypeParametersToTypeArgumentsBinder.java index 7febc2d3..94910e37 100644 --- a/src/main/java/org/jd/core/v1/service/converter/classfiletojavasyntax/util/TypeParametersToTypeArgumentsBinder.java +++ b/src/main/java/org/jd/core/v1/service/converter/classfiletojavasyntax/util/TypeParametersToTypeArgumentsBinder.java @@ -18,6 +18,7 @@ import java.util.*; import static org.jd.core.v1.model.javasyntax.declaration.Declaration.FLAG_STATIC; +import static org.jd.core.v1.model.javasyntax.type.ObjectType.TYPE_OBJECT; public class TypeParametersToTypeArgumentsBinder { protected PopulateBindingsWithTypeParameterVisitor populateBindingsWithTypeParameterVisitor = new PopulateBindingsWithTypeParameterVisitor(); @@ -75,7 +76,7 @@ public ClassFileConstructorInvocationExpression newConstructorInvocationExpressi TypeMaker.MethodTypes methodTypes, BaseExpression parameters) { BaseType parameterTypes = clone(methodTypes.parameterTypes); - Map bindings = createBindings(null, null, null, methodTypes.typeParameters, PrimitiveType.TYPE_VOID, null, parameterTypes, parameters); + Map bindings = createBindings(null, null, null, methodTypes.typeParameters, TYPE_OBJECT, null, parameterTypes, parameters); parameterTypes = bindParameterTypesWithArgumentTypes(bindings, parameterTypes); bindParameterTypesWithArgumentTypes(parameterTypes, parameters); @@ -99,7 +100,7 @@ public ClassFileSuperConstructorInvocationExpression newSuperConstructorInvocati BaseTypeArgument typeArguments = typeTypes.superType.getTypeArguments(); BaseTypeParameter methodTypeParameters = methodTypes.typeParameters; - bindings = createBindings(null, typeParameters, typeArguments, methodTypeParameters, PrimitiveType.TYPE_VOID, null, parameterTypes, parameters); + bindings = createBindings(null, typeParameters, typeArguments, methodTypeParameters, TYPE_OBJECT, null, parameterTypes, parameters); } } @@ -139,7 +140,7 @@ public FieldReferenceExpression newFieldReferenceExpression( BaseTypeParameter typeParameters = typeTypes.typeParameters; BaseTypeArgument typeArguments = expressionObjectType.getTypeArguments(); - bindings = createBindings(expression, typeParameters, typeArguments, null, PrimitiveType.TYPE_VOID, null, null, null); + bindings = createBindings(expression, typeParameters, typeArguments, null, TYPE_OBJECT, null, null, null); } type = (Type)bindParameterTypesWithArgumentTypes(bindings, type); @@ -303,7 +304,7 @@ protected void bindParameterTypesWithArgumentTypes(Type type, ClassFileNewExpres protected void bindParameterTypesWithArgumentTypes(BaseType types, BaseExpression expressions) { if (types != null) { - if (types.isList()) { + if (types.isList() && expressions.isList()) { Iterator parameterTypesIterator = types.iterator(); Iterator parametersIterator = expressions.iterator(); @@ -344,7 +345,7 @@ protected Map createBindings( typeParameters.accept(populateBindingsWithTypeParameterVisitor); if (typeArguments != null) { - if (typeParameters.isList()) { + if (typeParameters.isList() && typeArguments.isTypeArgumentList()) { Iterator iteratorTypeParameter = typeParameters.iterator(); Iterator iteratorTypeArgument = typeArguments.getTypeArgumentList().iterator(); @@ -363,13 +364,13 @@ protected Map createBindings( methodTypeParameters.accept(populateBindingsWithTypeParameterVisitor); } - if ((returnType != PrimitiveType.TYPE_VOID) && (returnExpressionType != null)) { + if (!TYPE_OBJECT.equals(returnType) && (returnExpressionType != null)) { populateBindingsWithTypeArgumentVisitor.init(contextualTypeBounds, bindings, typeBounds, returnType); returnExpressionType.accept(populateBindingsWithTypeArgumentVisitor); } if (parameterTypes != null) { - if (parameterTypes.isList()) { + if (parameterTypes.isList() && parameters.isList()) { Iterator parameterTypesIterator = parameterTypes.iterator(); Iterator parametersIterator = parameters.iterator(); @@ -548,7 +549,7 @@ public void visit(NewExpression expression) { @Override public void visit(CastExpression expression) { - assert (type.getDimension() == expression.getType().getDimension()) : "TypeParametersToTypeArgumentsBinder.visit(CastExpression ce) : invalid array type"; + assert TYPE_OBJECT.equals(type) || (type.getDimension() == expression.getType().getDimension()) : "TypeParametersToTypeArgumentsBinder.visit(CastExpression ce) : invalid array type"; if (type.isObject()) { ObjectType objectType = (ObjectType)type; @@ -582,15 +583,19 @@ public void visit(CastExpression expression) { @Override public void visit(TernaryOperatorExpression expression) { - expression.setType(type); - bindParameterTypesWithArgumentTypes(type, expression.getExpressionTrue()); - bindParameterTypesWithArgumentTypes(type, expression.getExpressionFalse()); + Type t = type; + + expression.setType(t); + bindParameterTypesWithArgumentTypes(t, expression.getExpressionTrue()); + bindParameterTypesWithArgumentTypes(t, expression.getExpressionFalse()); } @Override public void visit(BinaryOperatorExpression expression) { - bindParameterTypesWithArgumentTypes(type, expression.getLeftExpression()); - bindParameterTypesWithArgumentTypes(type, expression.getRightExpression()); + Type t = type; + + bindParameterTypesWithArgumentTypes(t, expression.getLeftExpression()); + bindParameterTypesWithArgumentTypes(t, expression.getRightExpression()); } } } diff --git a/src/main/java/org/jd/core/v1/service/converter/classfiletojavasyntax/visitor/AddCastExpressionVisitor.java b/src/main/java/org/jd/core/v1/service/converter/classfiletojavasyntax/visitor/AddCastExpressionVisitor.java index f4778cdf..fcee997d 100644 --- a/src/main/java/org/jd/core/v1/service/converter/classfiletojavasyntax/visitor/AddCastExpressionVisitor.java +++ b/src/main/java/org/jd/core/v1/service/converter/classfiletojavasyntax/visitor/AddCastExpressionVisitor.java @@ -122,11 +122,15 @@ public void visit(LocalVariableDeclarator declarator) { @Override public void visit(ArrayVariableInitializer declaration) { - Type t = type; + if (type.getDimension() == 0) { + acceptListDeclaration(declaration); + } else { + Type t = type; - type = type.createType(type.getDimension() - 1); - acceptListDeclaration(declaration); - type = t; + type = type.createType(type.getDimension() - 1); + acceptListDeclaration(declaration); + type = t; + } } @Override @@ -134,7 +138,12 @@ public void visit(ExpressionVariableInitializer declaration) { Expression expression = declaration.getExpression(); if (expression.getClass() == NewInitializedArray.class) { - ((NewInitializedArray)expression).getArrayInitializer().accept(this); + NewInitializedArray nia = (NewInitializedArray)expression; + Type t = type; + + type = nia.getType(); + nia.getArrayInitializer().accept(this); + type = t; } else { declaration.setExpression(updateExpression(type, expression)); } diff --git a/src/main/java/org/jd/core/v1/service/converter/classfiletojavasyntax/visitor/BindTypeParametersToTypeArgumentsVisitor.java b/src/main/java/org/jd/core/v1/service/converter/classfiletojavasyntax/visitor/BindTypeParametersToTypeArgumentsVisitor.java index a63c2149..45697820 100644 --- a/src/main/java/org/jd/core/v1/service/converter/classfiletojavasyntax/visitor/BindTypeParametersToTypeArgumentsVisitor.java +++ b/src/main/java/org/jd/core/v1/service/converter/classfiletojavasyntax/visitor/BindTypeParametersToTypeArgumentsVisitor.java @@ -104,7 +104,8 @@ public void visit(GenericType type) { } else { typeArgumentToTypeVisitor.init(); ta.accept(typeArgumentToTypeVisitor); - result = typeArgumentToTypeVisitor.getType().createType(type.getDimension()); + Type t = typeArgumentToTypeVisitor.getType(); + result = t.createType(t.getDimension() + type.getDimension()); } } diff --git a/src/main/java/org/jd/core/v1/service/converter/classfiletojavasyntax/visitor/PopulateBindingsWithTypeArgumentVisitor.java b/src/main/java/org/jd/core/v1/service/converter/classfiletojavasyntax/visitor/PopulateBindingsWithTypeArgumentVisitor.java index 91087b09..9e97e35b 100644 --- a/src/main/java/org/jd/core/v1/service/converter/classfiletojavasyntax/visitor/PopulateBindingsWithTypeArgumentVisitor.java +++ b/src/main/java/org/jd/core/v1/service/converter/classfiletojavasyntax/visitor/PopulateBindingsWithTypeArgumentVisitor.java @@ -10,9 +10,7 @@ import org.jd.core.v1.model.javasyntax.type.*; import org.jd.core.v1.service.converter.classfiletojavasyntax.util.TypeMaker; -import java.util.ArrayList; import java.util.Iterator; -import java.util.List; import java.util.Map; import static org.jd.core.v1.model.javasyntax.type.ObjectType.TYPE_CLASS; @@ -63,7 +61,7 @@ public void visit(TypeArguments arguments) { } if (typeArgument == null) { - bindings.put(typeName, convertCurrentTypeArgument()); + bindings.put(typeName, checkTypeClassCheckDimensionAndReturnCurrentAsTypeArgument(type)); } else if (!current.equals(typeArgument)) { typeArgumentToTypeVisitor.init(); typeArgument.accept(typeArgumentToTypeVisitor); @@ -76,14 +74,14 @@ public void visit(TypeArguments arguments) { if (!t1.createType(0).equals(t2.createType(0))) { if (t1.isObject() && t2.isObject()) { ObjectType ot1 = (ObjectType)t1; - ObjectType ot2 = (ObjectType)t2; - - if (typeMaker.isAssignable(ot1, ot2)) { - bindings.put(typeName, typeArgument); - } else if (typeMaker.isAssignable(ot2, ot1)) { - bindings.put(typeName, convertCurrentTypeArgument()); - } else { - bindings.put(typeName, WildcardTypeArgument.WILDCARD_TYPE_ARGUMENT); + ObjectType ot2 = (ObjectType)t2.createType(t2.getDimension() - type.getDimension()); + + if (!typeMaker.isAssignable(ot1, ot2)) { + if (typeMaker.isAssignable(ot2, ot1)) { + bindings.put(typeName, checkTypeClassCheckDimensionAndReturnCurrentAsTypeArgument(type)); + } else { + bindings.put(typeName, WildcardTypeArgument.WILDCARD_TYPE_ARGUMENT); + } } } } @@ -100,16 +98,25 @@ protected boolean equals(BaseType bt1, BaseType bt2) { } } - protected TypeArgument convertCurrentTypeArgument() { - if ((current != null) && (current.getClass() == ObjectType.class)) { - ObjectType ot = (ObjectType)current; + protected TypeArgument checkTypeClassCheckDimensionAndReturnCurrentAsTypeArgument(GenericType type) { + if (current != null) { + Class currentClass = current.getClass(); + + if (currentClass == ObjectType.class) { + ObjectType ot = (ObjectType) current; + + if ((ot.getTypeArguments() == null) && ot.getInternalName().equals(TYPE_CLASS.getInternalName())) { + return TYPE_CLASS_WILDCARD.createType(ot.getDimension()); + } - if ((ot.getTypeArguments() == null) && ot.getInternalName().equals(TYPE_CLASS.getInternalName())) { - return TYPE_CLASS_WILDCARD; + return ot.createType(ot.getDimension() - type.getDimension()); + } else if ((currentClass == InnerObjectType.class) || (currentClass == GenericType.class) || (currentClass == PrimitiveType.class)) { + Type t = (Type)current; + return t.createType(t.getDimension() - type.getDimension()); } } - return (TypeArgument)current; + return current.getTypeArgumentFirst(); } @Override