use of kalang.core.ObjectType in project kalang by kasonyang.
the class ObjectFieldExpr method create.
@Nonnull
public static FieldExpr create(@Nonnull ExprNode target, String fieldName, @Nullable ClassNode caller) throws FieldNotFoundException {
Type type = target.getType();
if (!(type instanceof ObjectType)) {
throw new UnsupportedOperationException("unsupported type:" + type);
}
ObjectType classType = (ObjectType) type;
FieldDescriptor field = getField(classType, fieldName, caller);
if (AstUtil.isStatic(field.getModifier())) {
throw new FieldNotFoundException(fieldName + " is static");
}
return new ObjectFieldExpr(target, field);
}
use of kalang.core.ObjectType in project kalang by kasonyang.
the class StaticInvokeExpr method create.
public static StaticInvokeExpr create(ClassReference clazz, String methodName, ExprNode[] args, @Nullable ClassNode caller) throws MethodNotFoundException, AmbiguousMethodException {
ObjectType clazzType = Types.getClassType(clazz.getReferencedClassNode());
// TODO static only
// TODO what about generic static method?
MethodDescriptor[] candidates = clazzType.getMethodDescriptors(caller, true, true);
MethodSelection ms = applyMethod(Types.getClassType(clazz.getReferencedClassNode()), methodName, args, candidates);
ExecutableDescriptor md = ms.selectedMethod;
if (!AstUtil.isStatic(md.getModifier())) {
throw new MethodNotFoundException(methodName + " is not static");
}
return new StaticInvokeExpr(clazz, md, ms.appliedArguments);
}
use of kalang.core.ObjectType in project kalang by kasonyang.
the class ObjectInvokeExpr method create.
public static ObjectInvokeExpr create(ExprNode target, String methodName, ExprNode[] args, @Nullable ClassNode caller) throws MethodNotFoundException, AmbiguousMethodException {
ObjectType targetType = (ObjectType) target.getType();
ClassNode clazz = targetType.getClassNode();
boolean recursive = !"<init>".equals(methodName);
// MethodNode[] candidates = AstUtil.listAccessibleMethods(clazz, caller , recursive);
List<ExecutableDescriptor> candidates = new LinkedList();
candidates.addAll(Arrays.asList(targetType.getMethodDescriptors(caller, recursive, true)));
candidates.addAll(Arrays.asList(targetType.getConstructorDescriptors(caller)));
MethodSelection ms = applyMethod(targetType, methodName, args, candidates.toArray(new ExecutableDescriptor[candidates.size()]));
ExecutableDescriptor md = ms.selectedMethod;
if (AstUtil.isStatic(md.getModifier())) {
throw new MethodNotFoundException(methodName + " is static");
}
return new ObjectInvokeExpr(target, ms.selectedMethod, ms.appliedArguments);
}
use of kalang.core.ObjectType in project kalang by kasonyang.
the class AstUtil method createClassNodeWithInterfaces.
public static ClassNode createClassNodeWithInterfaces(String name, @Nullable ObjectType superType, @Nullable ObjectType... interfaces) {
ClassNode cn = new ClassNode();
cn.name = name;
cn.setSuperType(superType == null ? Types.getRootType() : superType);
if (interfaces != null) {
for (ObjectType itf : interfaces) cn.addInterface(itf);
}
return cn;
}
use of kalang.core.ObjectType in project kalang by kasonyang.
the class BoxUtil method getCastMethod.
private static int getCastMethod(Type fromType, Type toType) {
if (toType.isAssignableFrom(fromType)) {
return CAST_NOTHING;
}
if (fromType instanceof PrimitiveType && toType instanceof PrimitiveType) {
// if ((toType) instanceof PrimitiveType) {
if (MathType.castable(MathType.getType(fromType.getName()), MathType.getType(toType.getName()))) {
return CAST_PRIMITIVE;
}
} else if (fromType instanceof PrimitiveType && toType instanceof ObjectType) {
if (toType.equals(Types.getRootType())) {
return CAST_PRIMITIVE_TO_OBJECT;
}
// if (fromType.equals(Types.NULL_TYPE)) {
// return CAST_NOTHING;
// }
// // if (toType.equals(Types.STRING_CLASS_TYPE)) {
// // return CAST_PRIMITIVE_TO_STRING;
// // }
PrimitiveType toPriType = Types.getPrimitiveType((ObjectType) toType);
if (toPriType == null) {
return CAST_UNSUPPORTED;
}
if (toPriType.equals(fromType)) {
return CAST_PRIMITIVE_TO_OBJECT;
}
} else if (fromType instanceof ObjectType && toType instanceof PrimitiveType) {
// if() {
ObjectType fromClassType = (ObjectType) fromType;
PrimitiveType fromPrimitive = Types.getPrimitiveType(fromClassType);
if (fromPrimitive == null) {
return CAST_UNSUPPORTED;
}
if (fromPrimitive.equals(toType)) {
return CAST_OBJECT_TO_PRIMITIVE;
}
}
// }
return CAST_UNSUPPORTED;
}
Aggregations