use of kalang.ast.ClassNode in project kalang by kasonyang.
the class ClassNodeMetaBuilder method build.
public void build(ClassNode cn, boolean inScriptMode) {
this.thisClazz = cn;
astBuilder.thisClazz = cn;
ParserRuleContext ctx = classNodeBuilder.getClassNodeDefContext(cn);
if (ctx != null)
visit(ctx);
for (ClassNode c : cn.classes) {
build(c, false);
}
}
use of kalang.ast.ClassNode in project kalang by kasonyang.
the class ClassNodeMetaBuilder method visitClassDef.
@Override
public Object visitClassDef(KalangParser.ClassDefContext ctx) {
thisClazz.annotations.addAll(astBuilder.getAnnotations(ctx.annotation()));
thisClazz.modifier = astBuilder.parseModifier(ctx.varModifier());
List<Token> gnrTypes = ctx.genericTypes;
if (gnrTypes != null && !gnrTypes.isEmpty()) {
for (Token g : gnrTypes) {
// TODO suport generic type bounds in syntax
GenericType gt = new GenericType(g.getText(), Types.getRootType(), null, NullableKind.NONNULL);
thisClazz.declareGenericType(gt);
}
}
ObjectType superType = null;
if (ctx.parentClass != null) {
ObjectType parentClass = astBuilder.parseClassType(ctx.parentClass);
if (parentClass != null) {
superType = parentClass;
}
} else {
superType = Types.getRootType();
}
if (Modifier.isInterface(thisClazz.modifier)) {
// TODO update syntax to support:interface extends T1,T2...
thisClazz.addInterface(superType);
} else {
thisClazz.setSuperType(superType);
}
if (ctx.interfaces != null && ctx.interfaces.size() > 0) {
for (KalangParser.ClassTypeContext itf : ctx.interfaces) {
ObjectType itfClz = astBuilder.parseClassType(itf);
if (itfClz != null) {
thisClazz.addInterface(itfClz);
}
}
}
if (this.isDeclaringNonStaticInnerClass()) {
ClassNode parentClass = thisClazz.enclosingClass;
if (parentClass == null) {
throw Exceptions.unexceptedValue(parentClass);
}
thisClazz.createField(Types.getClassType(parentClass), "this$0", Modifier.PRIVATE | ModifierConstant.SYNTHETIC);
}
visit(ctx.classBody());
if (!ModifierUtil.isInterface(thisClazz.modifier) && !AstUtil.containsConstructor(thisClazz) && !AstUtil.createEmptyConstructor(thisClazz)) {
this.diagnosisReporter.report(Diagnosis.Kind.ERROR, "failed to create constructor with no parameters", ctx);
}
MethodNode[] methods = thisClazz.getDeclaredMethodNodes();
for (int i = 0; i < methods.length; i++) {
MethodNode node = methods[i];
BlockStmt body = node.getBody();
if (body != null) {
if (AstUtil.isConstructor(node)) {
// constructor
if (this.isDeclaringNonStaticInnerClass()) {
ClassNode enclosingClass = thisClazz.enclosingClass;
if (enclosingClass == null) {
throw Exceptions.unexceptedValue(enclosingClass);
}
ParameterNode outerInstanceParam = node.createParameter(0, Types.getClassType(enclosingClass), "this$0");
ExprNode parentFieldExpr = astBuilder.getObjectFieldExpr(new ThisExpr(Types.getClassType(thisClazz)), "this$0", ParserRuleContext.EMPTY);
if (parentFieldExpr == null) {
throw Exceptions.unexceptedValue(parentFieldExpr);
}
body.statements.add(1, new ExprStmt(new AssignExpr((AssignableExpr) parentFieldExpr, new ParameterExpr(outerInstanceParam))));
}
}
}
}
for (FieldNode fieldNode : thisClazz.getFields()) {
int mdf = fieldNode.modifier;
if (!AstUtil.hasGetter(thisClazz, fieldNode)) {
AstUtil.createGetter(thisClazz, fieldNode, mdf);
}
if (!AstUtil.hasSetter(thisClazz, fieldNode)) {
AstUtil.createSetter(thisClazz, fieldNode, mdf);
}
fieldNode.modifier = ModifierUtil.setPrivate(mdf);
}
return null;
}
use of kalang.ast.ClassNode in project kalang by kasonyang.
the class AstUtil method createArrayAst.
public static ClassNode createArrayAst(String component) {
ClassNode clazz = new ClassNode();
clazz.name = component + "[]";
clazz.setSuperType(Types.getRootType());
return clazz;
}
use of kalang.ast.ClassNode 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.ast.ClassNode 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