Skip to content

Commit

Permalink
various fixes related to casts
Browse files Browse the repository at this point in the history
  • Loading branch information
nbauma109 committed Jul 3, 2023
1 parent fe7a5dc commit c633b16
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class CastExpression extends AbstractLineNumberTypeExpression {
private Expression expression;
private boolean explicit;
private boolean byteCodeCheckCast;
private Type intersectType;

public CastExpression(Type type, Expression expression) {
this(UNKNOWN_LINE_NUMBER, type, expression);
Expand All @@ -32,7 +33,16 @@ public CastExpression(int lineNumber, Type type, Expression expression, boolean
this.explicit = explicit;
this.byteCodeCheckCast = byteCodeCheckCast;
}


public Type getIntersectType() {
return intersectType;
}

public void setIntersectType(Type type) {
this.intersectType = getType();
setType(type);
}

@Override
public Expression getExpression() {
return expression;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,12 @@ public boolean isAssignable(Map<String, TypeArgument> typeBindings, Map<String,
return isAssignable(typeBindings, typeBounds, lt, rt);
}
}
if (leftUnbound instanceof GenericType gt) {
TypeArgument typeArgument = typeBindings.get(gt.getName());
if (typeArgument instanceof WildcardSuperTypeArgument wsta && wsta.type().equals(left)) {
return true;
}
}
return false;
}

Expand Down Expand Up @@ -1651,7 +1657,9 @@ private void loadFieldsAndMethods(String internalTypeName, byte[] data) throws I
methodTypes = parseMethodSignature(internalTypeName, name, descriptor, signature, exceptionTypeNames);
}
methodTypes.setAccessFlags(accessFlags);

if ((accessFlags & Const.ACC_SYNTHETIC) != 0) {
continue;
}
internalTypeNameMethodNameDescriptorToMethodTypes.put(key, methodTypes);

parameterCount = methodTypes.getParameterTypes() == null ? 0 : methodTypes.getParameterTypes().size();
Expand Down Expand Up @@ -1940,8 +1948,8 @@ private boolean match(Map<String, TypeArgument> typeBindings, Map<String, BaseTy
}
}

private boolean match(Map<String, TypeArgument> typeBindings, Map<String, BaseType> typeBounds, Type leftType, Type rightType) {
if (leftType.equals(rightType)) {
boolean match(Map<String, TypeArgument> typeBindings, Map<String, BaseType> typeBounds, Type leftType, Type rightType) {
if (ObjectType.TYPE_OBJECT.equals(leftType) || leftType.equals(rightType)) {
return true;
}

Expand All @@ -1950,10 +1958,17 @@ private boolean match(Map<String, TypeArgument> typeBindings, Map<String, BaseTy
return flags != 0;
}

if (leftType.isObjectType() && rightType.isObjectType()) {
ObjectType ot1 = (ObjectType)leftType;
ObjectType ot2 = (ObjectType)rightType;
return isAssignable(typeBindings, typeBounds, ot1, ot2);
if (rightType instanceof ObjectType otRight) {
if (leftType instanceof ObjectType otLeft) {
return isAssignable(typeBindings, typeBounds, otLeft, otRight);
}

if (leftType instanceof GenericType gt) {
BaseType boundType = typeBounds.get(gt.getName());
if (boundType instanceof ObjectType ot && isAssignable(ot, otRight)) {
return true;
}
}
}

return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -655,10 +655,18 @@ public void testParseTypeParameter() {
assertEquals("java.lang.Serializable", ((ObjectType) ((TypeParameterWithTypeBounds) result).getTypeBounds().getLast()).getQualifiedName());
}

@Test
public void testMatch() throws Exception {
Map<String, TypeArgument> typeBindings = Collections.emptyMap();
Map<String, BaseType> typeBounds = Collections.emptyMap();
assertTrue(typeMaker.match(typeBindings, typeBounds, ObjectType.TYPE_OBJECT, new GenericType("T", 1)));
}

@Test
public void testMatchCount() throws Exception {
assertEquals(2, typeMaker.matchCount(StringConstants.JAVA_LANG_MATH, "round", 1, false));
assertEquals(8, typeMaker.matchCount(StringConstants.JAVA_LANG_STRING, "valueOf", 1, false));
assertEquals(1, typeMaker.matchCount("java/util/concurrent/DelayQueue", "add", 1, false));
}

@Test
Expand Down Expand Up @@ -958,6 +966,19 @@ public void testIsAssignable5() {
// Verify the result
assertFalse(typeMaker.isAssignable(typeBindings, typeBounds, left, right));
}

@Test
public void testIsAssignable6() {
// Prepare some test data
ObjectType map = typeMaker.makeFromInternalTypeName("java/util/Map");
map = map.createType(new TypeArguments(Arrays.asList(WildcardTypeArgument.WILDCARD_TYPE_ARGUMENT, WildcardTypeArgument.WILDCARD_TYPE_ARGUMENT)));
Map<String, BaseType> typeBounds = Collections.emptyMap();
Map<String, TypeArgument> typeBindings = Collections.singletonMap("T", new WildcardSuperTypeArgument(map));

// Verify the result
assertTrue(typeMaker.isAssignable(typeBindings, typeBounds, map, new GenericType("T"), ObjectType.TYPE_OBJECT));
}

@Test
public void testIsTypeArgumentAssignableFrom() {
// Prepare some test data
Expand Down

0 comments on commit c633b16

Please sign in to comment.