use of kalang.core.Type in project kalang by kasonyang.
the class AstBuilder method varDecl.
protected VarInfo varDecl(VarDeclContext ctx, Type inferedType) {
VarInfo vds = new VarInfo();
String name = ctx.name.getText();
TypeContext type = null;
if (ctx.varType != null) {
type = ctx.varType;
} else if (ctx.type() != null) {
type = ctx.type();
}
Type declType = type != null ? parseType(type) : inferedType;
if (isDefindedId(name)) {
diagnosisReporter.report(Diagnosis.Kind.ERROR, "the name is definded:" + name, ctx);
}
vds.name = name;
vds.type = declType;
vds.modifier = ctx.valToken != null ? Modifier.FINAL : 0;
// }
if (vds.type == null) {
vds.type = Types.getRootType();
}
return vds;
}
use of kalang.core.Type 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.Type in project kalang by kasonyang.
the class AstBuilder method visitNewArrayExpr.
@Override
public ExprNode visitNewArrayExpr(KalangParser.NewArrayExprContext ctx) {
Type type = parseType(ctx.type());
ExprNode ret;
if (ctx.size != null) {
ExprNode size = visitExpression(ctx.size);
ret = new NewArrayExpr(type, size);
} else {
ExprNode[] initExprs = new ExprNode[ctx.initExpr.size()];
for (int i = 0; i < initExprs.length; i++) {
initExprs[i] = visitExpression(ctx.initExpr.get(i));
}
ret = createInitializedArray(type, initExprs);
}
mapAst(ret, ctx);
return ret;
}
use of kalang.core.Type in project kalang by kasonyang.
the class JvmClassNode method getDeclaredMethodNodes.
@Override
public MethodNode[] getDeclaredMethodNodes() {
if (!this.methodsInitialized) {
this.methodsInitialized = true;
List<Executable> methods = new LinkedList();
methods.addAll(Arrays.asList(clazz.getDeclaredMethods()));
methods.addAll(Arrays.asList(clazz.getDeclaredConstructors()));
for (Executable m : methods) {
NullableKind nullable = getNullable(m.getAnnotations());
Type mType;
String mName;
int mModifier;
if (m instanceof Method) {
mType = getType(((Method) m).getGenericReturnType(), getGenericTypeMap(), ((Method) m).getReturnType(), nullable);
mName = m.getName();
mModifier = m.getModifiers();
} else if (m instanceof Constructor) {
mName = "<init>";
// getType(clz);
mType = Types.VOID_TYPE;
// | Modifier.STATIC;
mModifier = m.getModifiers();
} else {
throw Exceptions.unexceptedValue(m);
}
MethodNode methodNode = createMethodNode(mType, mName, mModifier);
for (Parameter p : m.getParameters()) {
NullableKind pnullable = getNullable(p.getAnnotations());
methodNode.createParameter(getType(p.getParameterizedType(), getGenericTypeMap(), p.getType(), pnullable), p.getName());
}
for (Class e : m.getExceptionTypes()) {
methodNode.addExceptionType(getType(e, getGenericTypeMap(), e, NullableKind.NONNULL));
}
}
}
return super.getDeclaredMethodNodes();
}
use of kalang.core.Type 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;
}
}
Aggregations