Skip to content

Commit

Permalink
fix issues in TypeMaker
Browse files Browse the repository at this point in the history
  • Loading branch information
nbauma109 committed May 3, 2023
1 parent 87e2a9e commit e559f7b
Showing 1 changed file with 38 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;

Expand Down Expand Up @@ -236,18 +237,17 @@ public TypeTypes parseClassFileSignature(ClassFile classFile) {

public MethodTypes parseMethodSignature(ClassFile classFile, Method method) {
String key = classFile.getInternalTypeName() + ':' + method.getName() + method.getSignature();
return parseMethodSignature(method, key);
return parseMethodSignature(classFile.getInternalTypeName(), method, key);
}

private MethodTypes parseMethodSignature(Method method, String key) {
private MethodTypes parseMethodSignature(String internalTypeName, Method method, String key) {
Signature attributeSignature = getSignature(method);
String[] exceptionTypeNames = getExceptionTypeNames(method);
MethodTypes methodTypes;

if (attributeSignature == null) {
methodTypes = parseMethodSignature(method.getSignature(), exceptionTypeNames);
methodTypes = parseMethodSignature(internalTypeName, method.getName(), method.getSignature(), exceptionTypeNames);
} else {
methodTypes = parseMethodSignature(method.getSignature(), attributeSignature.getSignature(), exceptionTypeNames);
methodTypes = parseMethodSignature(internalTypeName, method.getName(), method.getSignature(), attributeSignature.getSignature(), exceptionTypeNames);
}

internalTypeNameMethodNameDescriptorToMethodTypes.put(key, methodTypes);
Expand Down Expand Up @@ -301,13 +301,13 @@ public static int countDimension(String descriptor) {
return count;
}

private MethodTypes parseMethodSignature(String descriptor, String signature, String[] exceptionTypeNames) {
private MethodTypes parseMethodSignature(String internalTypeName, String methodName, String descriptor, String signature, String[] exceptionTypeNames) {
if (signature == null) {
return parseMethodSignature(descriptor, exceptionTypeNames);
return parseMethodSignature(internalTypeName, methodName, descriptor, exceptionTypeNames);
}
// Signature does not contain synthetic parameters like outer type name, for example.
MethodTypes mtDescriptor = parseMethodSignature(descriptor, exceptionTypeNames);
MethodTypes mtSignature = parseMethodSignature(signature, exceptionTypeNames);
MethodTypes mtDescriptor = parseMethodSignature(internalTypeName, methodName, descriptor, exceptionTypeNames);
MethodTypes mtSignature = parseMethodSignature(internalTypeName, methodName, signature, exceptionTypeNames);

if (mtDescriptor.getParameterTypes() != null) {
if (mtSignature.getParameterTypes() == null) {
Expand Down Expand Up @@ -344,12 +344,12 @@ private MethodTypes parseMethodSignature(String descriptor, String signature, St
* ReturnType: TypeSignature | VoidDescriptor
* ThrowsSignature: '^' ClassTypeSignature | '^' TypeVariableSignature
*/
private MethodTypes parseMethodSignature(String signature, String[] exceptionTypeNames) {
String cacheKey = signature;
private MethodTypes parseMethodSignature(String internalTypeName, String methodName, String signature, String[] exceptionTypeNames) {
String cacheKey = internalTypeName + '.' + methodName + signature;
boolean containsThrowsSignature = signature.indexOf('^') != -1;

if (!containsThrowsSignature && exceptionTypeNames != null) {
StringBuilder sb = new StringBuilder(signature);
StringBuilder sb = new StringBuilder(cacheKey);

for (String exceptionTypeName : exceptionTypeNames) {
sb.append("^L").append(exceptionTypeName).append(';');
Expand Down Expand Up @@ -847,6 +847,16 @@ public ObjectType makeFromDescriptorOrInternalTypeName(String descriptorOrIntern
return descriptorOrInternalTypeName.charAt(0) == '[' ? makeFromDescriptor(descriptorOrInternalTypeName) : makeFromInternalTypeName(descriptorOrInternalTypeName);
}

public Type makeFromSignatureOrInternalTypeName(String signatureOrInternalTypeName) {
if (signatureOrInternalTypeName == null) {
throw new IllegalArgumentException("ObjectTypeMaker.makeFromSignatureOrInternalTypeName(signatureOrInternalTypeName) : invalid signatureOrInternalTypeName");
}
if (signatureOrInternalTypeName.charAt(0) == '[' || signatureOrInternalTypeName.endsWith(";")) {
return makeFromSignature(signatureOrInternalTypeName);
}
return makeFromInternalTypeName(signatureOrInternalTypeName);
}

public ObjectType makeFromDescriptor(String descriptor) {
ObjectType ot = descriptorToObjectType.get(descriptor);

Expand Down Expand Up @@ -946,7 +956,9 @@ public boolean isAssignable(Map<String, TypeArgument> typeBindings, Map<String,
if (left == TYPE_UNDEFINED_OBJECT || right == TYPE_UNDEFINED_OBJECT || left.equals(TYPE_OBJECT) || left.equals(right)) {
return true;
}
if (left.getDimension() <= 0 && right.getDimension() <= 0) {
int leftDim = left.getDimension();
int rightDim = right.getDimension();
if (leftDim <= 0 && rightDim <= 0) {
String leftInternalTypeName = left.getInternalName();
long leftHashCode = leftInternalTypeName.hashCode() * 31L;
ObjectType ot = searchSuperParameterizedType(leftHashCode, leftInternalTypeName, right);
Expand All @@ -957,6 +969,13 @@ public boolean isAssignable(Map<String, TypeArgument> typeBindings, Map<String,
|| (leftUnbound instanceof ObjectType && isAssignable(typeBindings, typeBounds, (ObjectType) leftUnbound, right));
}
}
if (leftDim == rightDim && leftDim > 0 && rightDim > 0) {
Type leftType = left.createType(leftDim - 1);
Type rightType = right.createType(rightDim - 1);
if (leftType instanceof ObjectType && rightType instanceof ObjectType) {
return isAssignable(typeBindings, typeBounds, (ObjectType) leftType, (ObjectType) rightType);
}
}
return false;
}

Expand Down Expand Up @@ -1301,16 +1320,16 @@ public void setMethodReturnedType(String internalTypeName, String methodName, St
makeMethodTypes(internalTypeName, methodName, descriptor).setReturnedType(type);
}

public MethodTypes makeMethodTypes(String descriptor) {
return parseMethodSignature(descriptor, null);
public MethodTypes makeMethodTypes(String internalTypeName, String descriptor) {
return parseMethodSignature(internalTypeName, null, descriptor, null);
}

public MethodTypes makeMethodTypes(String internalTypeName, String methodName, String descriptor) {
MethodTypes methodTypes = loadMethodTypes(internalTypeName, methodName, descriptor);

if (methodTypes == null) {
String key = internalTypeName + ':' + methodName + descriptor;
methodTypes = parseMethodSignature(descriptor, null);
methodTypes = parseMethodSignature(internalTypeName, methodName, descriptor, null);
internalTypeNameMethodNameDescriptorToMethodTypes.put(key, methodTypes);
}

Expand Down Expand Up @@ -1420,7 +1439,7 @@ private ObjectType loadType(String internalTypeName) {
} else if (classPathLoader.canLoad(internalTypeName)) {
ot = loadType(internalTypeName, classPathLoader.load(internalTypeName));
}
internalTypeNameToObjectType.put(internalTypeName, ot);
internalTypeNameToObjectType.put(internalTypeName, Optional.ofNullable(ot).orElse(TYPE_UNDEFINED_OBJECT));
} catch (Exception e) {
assert ExceptionUtil.printStackTrace(e);
}
Expand Down Expand Up @@ -1638,9 +1657,9 @@ private void loadFieldsAndMethods(String internalTypeName, byte[] data) throws I
descriptor = (String)constants[descriptorIndex];
key = internalTypeName + ':' + name + descriptor;
if (signature == null) {
methodTypes = parseMethodSignature(descriptor, exceptionTypeNames);
methodTypes = parseMethodSignature(internalTypeName, name, descriptor, exceptionTypeNames);
} else {
methodTypes = parseMethodSignature(descriptor, signature, exceptionTypeNames);
methodTypes = parseMethodSignature(internalTypeName, name, descriptor, signature, exceptionTypeNames);
}
methodTypes.setAccessFlags(accessFlags);

Expand Down

0 comments on commit e559f7b

Please sign in to comment.