use of kalang.core.Type in project kalang by kasonyang.
the class AstUtil method getUnimplementedMethod.
@Nonnull
public static List<MethodDescriptor> getUnimplementedMethod(ClassNode theClass, ObjectType theInterface) {
ClassType theType = Types.getClassType(theClass);
MethodDescriptor[] implementedMethods = theType.getMethodDescriptors(theClass, true, false);
List<MethodDescriptor> list = new LinkedList();
for (MethodDescriptor m : theInterface.getMethodDescriptors(theClass, false, true)) {
if (ModifierUtil.isDefault(m.getModifier()))
continue;
String name = m.getName();
Type[] types = m.getParameterTypes();
MethodDescriptor overridingMd = MethodUtil.getMethodDescriptor(implementedMethods, name, types);
if (overridingMd == null || // TODO move check to where method declare
!OverrideUtil.overridingCompatible(overridingMd.getModifier(), m.getModifier()) || !OverrideUtil.returnTypeCompatible(overridingMd.getReturnType(), m.getReturnType()) || !OverrideUtil.exceptionTypeCompatible(overridingMd.getExceptionTypes(), m.getExceptionTypes())) {
list.add(m);
}
}
return list;
}
use of kalang.core.Type in project kalang by kasonyang.
the class AstUtil method createEmptyConstructor.
public static boolean createEmptyConstructor(ClassNode clazzNode) {
ObjectType supType = clazzNode.getSuperType();
if (supType == null) {
throw new RuntimeException("super type is null:" + clazzNode.name);
}
ConstructorDescriptor[] constructors = supType.getConstructorDescriptors(clazzNode);
ConstructorDescriptor m = MethodUtil.getConstructorDescriptor(constructors, null);
if (m != null) {
MethodNode mm = clazzNode.createMethodNode(Types.VOID_TYPE, m.getName(), m.getModifier());
for (Type e : m.getExceptionTypes()) mm.addExceptionType(e);
ParameterDescriptor[] pds = m.getParameterDescriptors();
for (ParameterDescriptor pd : pds) {
ParameterNode p = mm.createParameter(pd.getType(), pd.getName());
// TODO update mm.createParameter
p.modifier = pd.getModifier();
}
BlockStmt body = mm.getBody();
ParameterNode[] parameters = mm.getParameters();
ExprNode[] params = new ExprNode[parameters.length];
for (int i = 0; i < params.length; i++) {
params[i] = new ParameterExpr(parameters[i]);
}
body.statements.add(new ExprStmt(new ObjectInvokeExpr(new SuperExpr(clazzNode), m, params)));
return true;
} else {
return false;
}
}
use of kalang.core.Type in project kalang by kasonyang.
the class TypeUtil method equalTypes.
public static boolean equalTypes(Type[] declaredTypes, Type[] argTypes, @Nullable Map<GenericType, Type> genericTypes) {
if (declaredTypes.length != argTypes.length)
return false;
if (declaredTypes.length == 0)
return true;
for (int i = 0; i < declaredTypes.length; i++) {
Type dt = declaredTypes[i];
if (dt instanceof GenericType) {
if (genericTypes != null) {
dt = genericTypes.get((GenericType) dt);
Objects.requireNonNull(dt);
}
}
if (!equalType(dt, argTypes[i], genericTypes))
return false;
}
return true;
}
use of kalang.core.Type in project kalang by kasonyang.
the class ClassTypeTest method test.
@Test
public void test() throws AstNotFoundException {
AstLoader astLoader = new AstLoader();
ClassNode listClass = astLoader.loadAst("java.util.List");
ClassNode arrayListClass = astLoader.loadAst("java.util.ArrayList");
Type[] paramTypes = new Type[] { Types.getRootType() };
ObjectType listType = Types.getClassType(listClass, paramTypes);
ArrayType listArrayType = Types.getArrayType(listType);
ObjectType arrayListType = Types.getClassType(arrayListClass, paramTypes);
ArrayType arrayListArrayType = Types.getArrayType(arrayListType);
assertTrue(arrayListType.isSubTypeOf(listType));
assertTrue(listArrayType.isAssignableFrom(arrayListArrayType));
String mdDescriptors = Arrays.toString(listType.getMethodDescriptors(listClass, false, true));
assertEquals("[public abstract boolean contains(java.lang.Object arg0), public abstract boolean addAll(int arg0,java.util.Collection<? extends java.lang.Object> arg1), public void sort(java.util.Comparator<? super java.lang.Object> arg0), public abstract java.util.ListIterator<java.lang.Object> listIterator(int arg0), public abstract java.util.ListIterator<java.lang.Object> listIterator(), public abstract boolean retainAll(java.util.Collection<? extends java.lang.Object> arg0), public abstract java.lang.Object[] toArray(), public abstract java.lang.Object remove(int arg0), public abstract boolean addAll(java.util.Collection<? extends java.lang.Object> arg0), public boolean removeIf(java.util.function.Predicate<? super java.lang.Object> arg0), public abstract int hashCode(), public abstract boolean removeAll(java.util.Collection<? extends java.lang.Object> arg0), public java.util.stream.Stream<java.lang.Object> parallelStream(), public abstract int lastIndexOf(java.lang.Object arg0), public void replaceAll(java.util.function.UnaryOperator<java.lang.Object> arg0), public abstract void clear(), public abstract boolean containsAll(java.util.Collection<? extends java.lang.Object> arg0), public abstract int indexOf(java.lang.Object arg0), public abstract boolean add(java.lang.Object arg0), public abstract boolean isEmpty(), public abstract java.util.List<java.lang.Object> subList(int arg0,int arg1), public abstract java.lang.Object get(int arg0), public abstract java.lang.Object set(int arg0,java.lang.Object arg1), public abstract void add(int arg0,java.lang.Object arg1), public abstract java.util.Iterator<java.lang.Object> iterator(), public java.util.Spliterator<java.lang.Object> spliterator(), public void forEach(java.util.function.Consumer<? super java.lang.Object> arg0), public abstract java.lang.Object[] toArray(java.lang.Object[] arg0), public abstract boolean remove(java.lang.Object arg0), public abstract boolean equals(java.lang.Object arg0), public java.util.stream.Stream<java.lang.Object> stream(), public abstract int size()]", mdDescriptors);
// assertTrue(listType.isSubTypeOf(Types.getRootType()));
}
use of kalang.core.Type in project kalang by kasonyang.
the class Ast2Class method getMethodDescriptor.
private String getMethodDescriptor(Type returnType, Type[] parameterTypes) {
String desc = "";
String retTyp = getTypeDescriptor(returnType);
if (parameterTypes != null) {
for (Type t : parameterTypes) {
desc += getTypeDescriptor(t);
}
}
return "(" + desc + ")" + retTyp;
}
Aggregations