use of kalang.core.ObjectType 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.ObjectType in project kalang by kasonyang.
the class BoxUtil method castPrimitive2Object.
private static ExprNode castPrimitive2Object(ExprNode expr, PrimitiveType fromType) {
ObjectType classType = Types.getClassType(fromType);
if (classType == null) {
throw new UnknownError("unknown primitive type:" + fromType);
}
InvocationExpr inv;
try {
inv = StaticInvokeExpr.create(new ClassReference(classType.getClassNode()), "valueOf", new ExprNode[] { expr });
} catch (MethodNotFoundException | AmbiguousMethodException ex) {
throw new RuntimeException(ex);
}
return inv;
}
use of kalang.core.ObjectType in project kalang by kasonyang.
the class ParameterizedTypeTest method test.
@Test
public void test() throws AstNotFoundException {
// TODO optimize
ClassType pt = Types.getClassType(Types.getClassType("java.util.LinkedList").getClassNode(), new Type[] { Types.getIntClassType() });
ObjectType supType = pt.getSuperType();
assertTrue(supType instanceof ClassType);
assertEquals("java.util.AbstractSequentialList<java.lang.Integer>", supType.toString());
// MethodDescriptor[] mds = supType.getMethodDescriptors(null, true,true);
// System.out.print(Arrays.toString(mds));
}
use of kalang.core.ObjectType 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.ObjectType in project kalang by kasonyang.
the class InheritanceUtil method isSubclassOf.
public static boolean isSubclassOf(ClassNode childClass, ClassNode parentClass) {
ObjectType[] interfaces = childClass.getInterfaces();
ObjectType superType = childClass.getSuperType();
List<ObjectType> list = new ArrayList(interfaces.length + 1);
list.addAll(Arrays.asList(interfaces));
if (superType != null) {
list.add(superType);
}
for (ObjectType it : list) {
ClassNode clz = it.getClassNode();
if (clz.equals(parentClass)) {
return true;
}
if (isSubclassOf(clz, parentClass)) {
return true;
}
}
return false;
}
Aggregations