use of kalang.core.ClassType in project kalang by kasonyang.
the class AstBuilder method getVarObjectType.
private Type getVarObjectType(VarObject p) {
Type type = this.overrideTypes.get(p);
if (type == null)
type = p.getType();
// TODO handle other object type
if (type instanceof ClassType) {
Integer ns = nullState.get(p);
NullableKind nullable;
if (ns == null) {
nullable = ((ObjectType) type).getNullable();
} else if (ns == NULLSTATE_MUST_NONNULL) {
nullable = NullableKind.NONNULL;
} else if (ns == NULLSTATE_UNKNOWN) {
nullable = NullableKind.UNKNOWN;
} else if (ns == NULLSTATE_MUST_NULL || ns == NULLSTATE_NULLABLE) {
nullable = NullableKind.NULLABLE;
} else {
throw Exceptions.unexceptedValue(ns);
}
return Types.getClassType((ClassType) type, nullable);
} else {
return type;
}
}
use of kalang.core.ClassType in project kalang by kasonyang.
the class AstBuilder method parseWildcardType.
private Type parseWildcardType(KalangParser.WildcardTypeContext ctx) {
ObjectType classType = parseClassType(ctx.classType());
if (classType == null)
return null;
Type[] bounds = new Type[] { classType };
String boundKind = ctx.boundKind.getText();
if (boundKind.equals("super")) {
return new WildcardType(new Type[] { Types.getRootType() }, bounds);
} else {
return new WildcardType(bounds, null);
}
}
use of kalang.core.ClassType in project kalang by kasonyang.
the class AstUtil method hasGetter.
public static boolean hasGetter(ClassNode clazz, FieldNode field) {
ClassType type = Types.getClassType(clazz);
MethodDescriptor md = MethodUtil.getMethodDescriptor(type.getMethodDescriptors(clazz, true, true), "get" + NameUtil.firstCharToUpperCase(field.getName()), null);
return md != null;
}
use of kalang.core.ClassType in project kalang by kasonyang.
the class AstUtil method createScriptMainMethodIfNotExists.
public static void createScriptMainMethodIfNotExists(ClassNode clazz) {
ClassType clazzType = Types.getClassType(clazz);
MethodDescriptor[] methods = clazzType.getMethodDescriptors(null, false, false);
Type[] argTypes = new Type[] { Types.getArrayType(Types.getStringClassType()) };
MethodDescriptor mainMethod = MethodUtil.getMethodDescriptor(methods, "main", argTypes);
if (mainMethod == null) {
MethodNode m = clazz.createMethodNode(Types.VOID_TYPE, "main", Modifier.PUBLIC + Modifier.STATIC);
ParameterNode p = m.createParameter(argTypes[0], "arg");
BlockStmt body = m.getBody();
try {
NewObjectExpr newScriptExpr = new NewObjectExpr(clazzType);
ObjectInvokeExpr invokeExpr = ObjectInvokeExpr.create(newScriptExpr, "run", new ExprNode[] { new ParameterExpr(p) });
body.statements.add(new ExprStmt(invokeExpr));
} catch (MethodNotFoundException | AmbiguousMethodException ex) {
throw Exceptions.unexceptedException(ex);
}
}
}
use of kalang.core.ClassType in project kalang by kasonyang.
the class ObjectTypeTest method test.
@Test
public void test() throws AstNotFoundException {
ClassType type = Types.getClassType(ObjectTypeTest.class.getName());
ClassNode classNode = type.getClassNode();
ObjectType rootType = Types.getRootType();
ClassNode rootClassNode = rootType.getClassNode();
assertEquals(null, type.getFieldDescriptor(rootClassNode, "privateField"));
assertEquals(null, type.getFieldDescriptor(rootClassNode, "protectedField"));
assertNotEquals(null, type.getFieldDescriptor(rootClassNode, "publicField"));
assertNotEquals(null, type.getFieldDescriptor(classNode, "privateField"));
assertNotEquals(null, type.getFieldDescriptor(classNode, "protectedField"));
assertNotEquals(null, type.getFieldDescriptor(classNode, "publicField"));
}
Aggregations