use of kalang.core.ObjectType in project kalang by kasonyang.
the class AstBuilder method getImplicitInvokeExpr.
@Nullable
private ExprNode getImplicitInvokeExpr(String methodName, ExprNode[] args, ParserRuleContext ctx) {
ExprNode expr = null;
try {
ObjectType clazzType = getThisType();
InvocationExpr.MethodSelection ms = InvocationExpr.applyMethod(clazzType, methodName, args, clazzType.getMethodDescriptors(thisClazz, true, true));
if (Modifier.isStatic(ms.selectedMethod.getModifier())) {
expr = new StaticInvokeExpr(new ClassReference(thisClazz), ms.selectedMethod, ms.appliedArguments);
} else {
expr = new ObjectInvokeExpr(new ThisExpr(getThisType()), ms.selectedMethod, ms.appliedArguments);
}
} catch (MethodNotFoundException ex) {
if (args.length == 1 && (methodName.equals("print") || methodName.equals("println"))) {
try {
StaticFieldExpr fieldExpr = StaticFieldExpr.create(new ClassReference(Types.requireClassType("java.lang.System").getClassNode()), "out", null);
expr = getObjectInvokeExpr(fieldExpr, methodName, args, ctx);
} catch (FieldNotFoundException fnfEx) {
throw Exceptions.unexceptedException(fnfEx);
}
}
if (expr == null) {
this.methodNotFound(ctx.getStart(), className, methodName, args);
expr = new UnknownInvocationExpr(null, methodName, args);
}
} catch (AmbiguousMethodException ex) {
methodIsAmbiguous(ctx.start, ex);
return null;
}
mapAst(expr, ctx);
return expr;
}
use of kalang.core.ObjectType in project kalang by kasonyang.
the class AstBuilder method getObjectFieldExpr.
@Nullable
protected ExprNode getObjectFieldExpr(ExprNode expr, String fieldName, @Nullable ParserRuleContext rule) {
ExprNode ret;
Type type = expr.getType();
if (!(type instanceof ObjectType)) {
// AstBuilder.this.handleSyntaxError("unsupported type", rule==null ? ParserRuleContext.EMPTY : rule);
return null;
}
ObjectType exprType = (ObjectType) type;
if ((exprType instanceof ArrayType)) {
return null;
} else {
try {
ret = ObjectFieldExpr.create(expr, fieldName, exprType.getClassNode());
} catch (FieldNotFoundException ex) {
return null;
}
}
if (rule != null)
mapAst(ret, rule);
return ret;
}
use of kalang.core.ObjectType 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.core.ObjectType in project kalang by kasonyang.
the class JvmClassNode method transType.
// TODO why transType could be null?
@Nullable
private Type transType(java.lang.reflect.Type t, Map<TypeVariable, GenericType> genericTypes) {
if (t instanceof TypeVariable) {
GenericType vt = genericTypes.get((TypeVariable) t);
// FIXME it may be null if TypeVariable comes from method
if (vt != null) {
return vt;
}
return null;
} else if (t instanceof java.lang.reflect.ParameterizedType) {
java.lang.reflect.ParameterizedType pt = (java.lang.reflect.ParameterizedType) t;
Type rawType = transType(pt.getRawType(), genericTypes);
if (!(rawType instanceof ObjectType)) {
return null;
}
java.lang.reflect.Type[] typeArgs = pt.getActualTypeArguments();
Type[] gTypes = transType(typeArgs, genericTypes);
if (gTypes == null) {
return null;
}
return Types.getClassType(((ObjectType) rawType).getClassNode(), gTypes);
} else if (t instanceof java.lang.reflect.WildcardType) {
java.lang.reflect.WildcardType wt = (java.lang.reflect.WildcardType) t;
Type[] upperBounds = transType(wt.getUpperBounds(), genericTypes);
if (upperBounds == null) {
return null;
}
Type[] lowerBounds = transType(wt.getLowerBounds(), genericTypes);
if (lowerBounds == null) {
return null;
}
return new WildcardType(upperBounds, lowerBounds);
} else if (t instanceof GenericArrayType) {
GenericArrayType gt = (GenericArrayType) t;
Type ct = transType(gt.getGenericComponentType(), genericTypes);
if (ct == null) {
return null;
}
return Types.getArrayType(ct, NullableKind.NONNULL);
} else if (t instanceof Class) {
Class type = (Class) t;
if (type.isPrimitive()) {
return Types.getPrimitiveType(type.getTypeName());
} else if (type.isArray()) {
Type ct = transType(type.getComponentType(), genericTypes);
if (ct == null) {
return null;
}
return Types.getArrayType(ct);
} else {
try {
return Types.getClassType(this.astLoader.findAst(type));
} catch (AstNotFoundException ex) {
throw new RuntimeException(ex);
}
}
} else {
return null;
}
}
use of kalang.core.ObjectType 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;
}
Aggregations