Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.ide.vscode.commons.java.IType;
import org.springframework.ide.vscode.commons.java.Signature;
import org.springframework.ide.vscode.commons.util.ArrayUtils;
import java.util.stream.Collectors;

import org.springframework.ide.vscode.commons.java.IArrayType;
import org.springframework.ide.vscode.commons.java.IClassType;
import org.springframework.ide.vscode.commons.java.IJavaType;
import org.springframework.ide.vscode.commons.java.IParameterizedType;
import org.springframework.ide.vscode.commons.java.IPrimitiveType;
import org.springframework.ide.vscode.commons.java.IVoidType;
import org.springframework.ide.vscode.commons.util.Assert;
import org.springframework.ide.vscode.commons.util.Log;
import org.springframework.ide.vscode.commons.util.StringUtil;
import org.springframework.ide.vscode.commons.yaml.schema.YType;

/**
Expand Down Expand Up @@ -76,51 +79,74 @@ private void toString(StringBuilder buf) {
* Not all valid typeSig have a representation as a Type object. This may
* return null if no corresponding representation can be constructed.
*/
public static Type fromSignature(String typeSig, IType context) {
//TODO: does this work correctly with nested types (i.e like Map$Entry)
Type type = TYPE_FROM_SIG.get(typeSig);
if (type!=null) {
return type;
}
int kind = Signature.getTypeSignatureKind(typeSig);
//Essentially, Type object only able to represent class types with with generic parameters
// as long as these generic parameters are fully concrete (i.e. do not contain unbound type
// variables. For now only support the simplest case (no generics) and bail out returning null if we
// see something we don't understand.
if (kind==Signature.CLASS_TYPE_SIGNATURE) {
boolean shouldResolve = typeSig.charAt(0)==Signature.C_UNRESOLVED;
String erasure = Signature.getTypeErasure(typeSig);
String pkg = Signature.getSignatureQualifier(erasure);
String nam = Signature.getSignatureSimpleName(erasure);
String[] params = Signature.getTypeParameters(typeSig);
String[] args = Signature.getTypeArguments(typeSig);
if (shouldResolve) {
erasure = tryToResolve(qualifiedName(pkg, nam), context);
} else {
erasure = qualifiedName(pkg, nam);
}
if (ArrayUtils.hasElements(params)) {
//TODO: handle this case
return null;
} else if (ArrayUtils.hasElements(args)) {
Type[] argTypes = new Type[args.length];
for (int i = 0; i < argTypes.length; i++) {
argTypes[i] = fromSignature(args[i], context);
}
return new Type(erasure, argTypes);
} else {
return new Type(erasure, null);
// public static Type fromSignature(String typeSig, IType context) {
// //TODO: does this work correctly with nested types (i.e like Map$Entry)
// Type type = TYPE_FROM_SIG.get(typeSig);
// if (type!=null) {
// return type;
// }
// int kind = Signature.getTypeSignatureKind(typeSig);
// //Essentially, Type object only able to represent class types with with generic parameters
// // as long as these generic parameters are fully concrete (i.e. do not contain unbound type
// // variables. For now only support the simplest case (no generics) and bail out returning null if we
// // see something we don't understand.
// if (kind==Signature.CLASS_TYPE_SIGNATURE) {
// boolean shouldResolve = typeSig.charAt(0)==Signature.C_UNRESOLVED;
// String erasure = Signature.getTypeErasure(typeSig);
// String pkg = Signature.getSignatureQualifier(erasure);
// String nam = Signature.getSignatureSimpleName(erasure);
// String[] params = Signature.getTypeParameters(typeSig);
// String[] args = Signature.getTypeArguments(typeSig);
// if (shouldResolve) {
// erasure = tryToResolve(qualifiedName(pkg, nam), context);
// } else {
// erasure = qualifiedName(pkg, nam);
// }
// if (ArrayUtils.hasElements(params)) {
// //TODO: handle this case
// return null;
// } else if (ArrayUtils.hasElements(args)) {
// Type[] argTypes = new Type[args.length];
// for (int i = 0; i < argTypes.length; i++) {
// argTypes[i] = fromSignature(args[i], context);
// }
// return new Type(erasure, argTypes);
// } else {
// return new Type(erasure, null);
// }
// } else if (kind==Signature.ARRAY_TYPE_SIGNATURE) {
// Type elementType = fromSignature(Signature.getElementType(typeSig), context);
// if (elementType!=null) {
// int arrayCount = Signature.getArrayCount(typeSig);
// return elementType.asArray(arrayCount);
// }
// }
// return null;
// }

public static Type fromJavaType(IJavaType javaType) {
if (javaType instanceof IPrimitiveType || javaType instanceof IVoidType) {
Type type = TYPE_FROM_SIG.get(javaType.name());
if (type != null) {
return type;
}
} else if (kind==Signature.ARRAY_TYPE_SIGNATURE) {
Type elementType = fromSignature(Signature.getElementType(typeSig), context);
} else if (javaType instanceof IClassType) {
return new Type(javaType.name(), null);
} else if (javaType instanceof IParameterizedType) {
IParameterizedType parameterizedType = (IParameterizedType) javaType;
List<Type> arguments = parameterizedType.arguments().map(Type::fromJavaType).collect(Collectors.toList());
return new Type(parameterizedType.name(), arguments.toArray(new Type[arguments.size()]));
} else if (javaType instanceof IArrayType) {
IArrayType arrayType = (IArrayType) javaType;
Type elementType = fromJavaType(arrayType.component());
if (elementType!=null) {
int arrayCount = Signature.getArrayCount(typeSig);
return elementType.asArray(arrayCount);
return elementType.asArray(arrayType.dimensions());
}
}

return null;
}

public Type asArray(int arrayCount) {
Assert.isLegal(arrayCount>0);
StringBuilder arrayErasure = new StringBuilder(erasure);
Expand All @@ -130,32 +156,32 @@ public Type asArray(int arrayCount) {
return new Type(arrayErasure.toString(), params);
}

private static String qualifiedName(String pkg, String nam) {
if (StringUtil.hasText(pkg)) {
return pkg + "." + nam;
} else {
return nam;
}
}

private static String tryToResolve(String typeName, IType context) {
try {
String[][] resolved = context.resolveType(typeName);
if (ArrayUtils.hasElements(resolved)) {
String pkg = resolved[0][0];
String nam = resolved[0][1];
if (StringUtil.hasText(pkg)) {
return pkg+"."+nam;
} else {
//No . in front of default package
return nam;
}
}
} catch (Exception e) {
Log.log(e);
}
return typeName;
}
// private static String qualifiedName(String pkg, String nam) {
// if (StringUtil.hasText(pkg)) {
// return pkg + "." + nam;
// } else {
// return nam;
// }
// }
//
// private static String tryToResolve(String typeName, IType context) {
// try {
// String[][] resolved = context.resolveType(typeName);
// if (ArrayUtils.hasElements(resolved)) {
// String pkg = resolved[0][0];
// String nam = resolved[0][1];
// if (StringUtil.hasText(pkg)) {
// return pkg+"."+nam;
// } else {
// //No . in front of default package
// return nam;
// }
// }
// } catch (Exception e) {
// Log.log(e);
// }
// return typeName;
// }

//////////////////////////////////////////////////

Expand All @@ -179,7 +205,7 @@ private static String tryToResolve(String typeName, IType context) {
private static void sig2type(String sig, Class<?> cls) {
TYPE_FROM_SIG.put(sig, TypeParser.parse(cls.getName()));
}

@Override
public int hashCode() {
final int prime = 31;
Expand Down
Loading