Search in sources :

Example 1 with Type_

use of suite.jdk.gen.Type_ in project suite by stupidsing.

the class Object_ method mapper.

public static Mapper mapper(Type type) {
    Mapper mapper;
    if (type instanceof Class) {
        Class<?> clazz = (Class<?>) type;
        if (Type_.isSimple(clazz))
            mapper = new Mapper(object -> object, object -> object);
        else if (clazz.isArray()) {
            Class<?> componentType = clazz.getComponentType();
            mapper = new Mapper(object -> {
                Map<Object, Object> map = new HashMap<>();
                int length = Array.getLength(object);
                for (int i = 0; i < length; i++) map.put(i, Array.get(object, i));
                return map;
            }, object -> {
                Map<?, ?> map = (Map<?, ?>) object;
                Object objects = Array.newInstance(componentType, map.size());
                int i = 0;
                while (map.containsKey(i)) {
                    Array.set(objects, i, map.get(i));
                    i++;
                }
                return objects;
            });
        } else if (// polymorphism
        clazz.isInterface() || Modifier.isAbstract(clazz.getModifiers()))
            mapper = new Mapper(object -> {
                Class<?> clazz1 = object.getClass();
                Object m = apply_(mapper(clazz1).map, object);
                if (m instanceof Map) {
                    @SuppressWarnings("unchecked") Map<String, String> map = (Map<String, String>) m;
                    map.put("@class", clazz1.getName());
                    return map;
                } else
                    // happens when an enum implements an interface
                    return m;
            }, object -> {
                if (object instanceof Map) {
                    Map<?, ?> map = (Map<?, ?>) object;
                    String className = map.get("@class").toString();
                    Class<?> clazz1 = Rethrow.ex(() -> Class.forName(className));
                    return apply_(mapper(clazz1).unmap, object);
                } else
                    // happens when an enum implements an interface
                    return object;
            });
        else {
            Inspect inspect = Singleton.me.inspect;
            List<Pair<String, Field>> sfs = // 
            Read.from(// 
            inspect.fields(clazz)).map(// 
            field -> Pair.of(field.getName(), field)).toList();
            mapper = new Mapper(object -> Rethrow.ex(() -> {
                Map<Object, Object> map = new HashMap<>();
                for (Pair<String, Field> sf : sfs) map.put(sf.t0, sf.t1.get(object));
                return map;
            }), object -> Rethrow.ex(() -> {
                Map<?, ?> map = (Map<?, ?>) object;
                Object object1 = new_(clazz);
                for (Pair<String, Field> sf : sfs) sf.t1.set(object1, map.get(sf.t0));
                return object1;
            }));
        }
    } else if (type instanceof ParameterizedType) {
        ParameterizedType pt = (ParameterizedType) type;
        Type rawType = pt.getRawType();
        Class<?> clazz = rawType instanceof Class ? (Class<?>) rawType : null;
        if (List.class.isAssignableFrom(clazz))
            mapper = new Mapper(object -> {
                Map<Object, Object> map = new HashMap<>();
                int i = 0;
                for (Object o : (Collection<?>) object) map.put(i++, o);
                return map;
            }, object -> {
                Map<?, ?> map = (Map<?, ?>) object;
                Collection<Object> object1 = new ArrayList<>();
                int i = 0;
                while (map.containsKey(i)) object1.add(map.get(i++));
                return object1;
            });
        else if (Map.class.isAssignableFrom(clazz))
            mapper = new Mapper(object -> object, object -> object);
        else
            mapper = mapper(rawType);
    } else
        mapper = Fail.t("unrecognized type " + type);
    return mapper;
}
Also used : Read(suite.streamlet.Read) Array(java.lang.reflect.Array) Singleton(suite.node.util.Singleton) Inspect(suite.inspect.Inspect) Collection(java.util.Collection) IOException(java.io.IOException) HashMap(java.util.HashMap) Field(java.lang.reflect.Field) Type_(suite.jdk.gen.Type_) Constructor(java.lang.reflect.Constructor) ArrayList(java.util.ArrayList) Iterate(suite.util.FunUtil.Iterate) Pair(suite.adt.pair.Pair) List(java.util.List) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Closeable(java.io.Closeable) Modifier(java.lang.reflect.Modifier) Map(java.util.Map) Comparator(java.util.Comparator) HashMap(java.util.HashMap) Inspect(suite.inspect.Inspect) ParameterizedType(java.lang.reflect.ParameterizedType) Field(java.lang.reflect.Field) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Pair(suite.adt.pair.Pair)

Example 2 with Type_

use of suite.jdk.gen.Type_ in project suite by stupidsing.

the class FunRewrite method rewrite_.

private FunExpr rewrite_(FunExpr e0) {
    return // 
    e0.<// 
    FunExpr>switch_().applyIf(ApplyFunExpr.class, e1 -> {
        FunExpr object = rewrite(e1.object);
        FunExpr[] parameters = Read.from(e1.parameters).map(this::rewrite).toArray(FunExpr.class);
        Method method = fti.methodOf(object);
        return object.invoke(method.getName(), parameters);
    }).applyIf(CastFunExpr.class, e1 -> {
        FunExpr e2 = e1.expr;
        if (e2 instanceof DeclareParameterFunExpr) {
            Class<?> interfaceClass = Type_.classOf(e1.type);
            Map<String, Type> fieldTypes = new HashMap<>();
            Map<String, FunExpr> fieldValues = new HashMap<>();
            FunExpr e3 = rewrite(e -> {
                FunExpr fieldValue;
                if (e instanceof FieldStaticFunExpr) {
                    FieldStaticFunExpr e_ = (FieldStaticFunExpr) e;
                    String fieldName = e_.fieldName;
                    Type fieldType = fieldTypes.get(fieldName);
                    fieldTypes.put(fieldName, fieldType);
                    fieldValues.put(fieldName, e_);
                    return e;
                } else if (e instanceof PlaceholderFunExpr && (fieldValue = placeholders.get(e)) != null) {
                    String fieldName = "e" + Util.temp();
                    Type fieldType = fti.typeOf(fieldValue);
                    fieldTypes.put(fieldName, fieldType);
                    fieldValues.put(fieldName, fieldValue);
                    return this_().field(fieldName, fieldType);
                } else
                    return null;
            }, e2);
            FunCreator<?>.CreateClass cc = FunCreator.of(LambdaInterface.of(interfaceClass), fieldTypes).create_(e3);
            Streamlet2<String, FunExpr> fieldValues0 = Read.from2(cc.fieldTypeValues).mapValue(tv -> objectField(tv.t1, tv.t0));
            Streamlet2<String, FunExpr> fieldValues1 = Read.from2(fieldValues);
            NewFunExpr e4 = new NewFunExpr();
            e4.className = cc.className;
            e4.fieldValues = Streamlet2.concat(fieldValues0, fieldValues1).toMap();
            e4.implementationClass = cc.clazz;
            e4.interfaceClass = interfaceClass;
            return e4;
        } else
            return null;
    }).applyIf(DeclareLocalFunExpr.class, e1 -> {
        FunExpr value = rewrite(e1.value);
        FunExpr lfe = local(localTypes.size());
        localTypes.add(fti.typeOf(value));
        AssignLocalFunExpr alfe = new AssignLocalFunExpr();
        alfe.var = lfe;
        alfe.value = value;
        placeholders.put(e1.var, lfe);
        return seq(alfe, rewrite(e1.do_));
    }).applyIf(FieldFunExpr_.class, e1 -> {
        FunExpr set = e1 instanceof FieldSetFunExpr ? ((FieldSetFunExpr) e1).value : null;
        FunExpr object0 = rewrite(e1.object);
        String fieldName = e1.fieldName;
        Class<?> clazz = fti.classOf(object0);
        Field field = Rethrow.ex(() -> clazz.getField(fieldName));
        FunExpr object1 = object0.cast_(field.getDeclaringClass());
        Type fieldType = Type.getType(field.getType());
        return set == null ? object1.field(fieldName, fieldType) : object1.fieldSet(fieldName, fieldType, set);
    }).applyIf(FieldInjectFunExpr.class, e1 -> {
        Type type = fieldTypes.get(e1.fieldName);
        if (type != null)
            return rewrite(this_().field(e1.fieldName, type));
        else
            return Fail.t(e1.fieldName);
    }).applyIf(InvokeLambdaFunExpr.class, e1 -> {
        LambdaInstance<?> l_inst = e1.lambda;
        LambdaImplementation<?> l_impl = l_inst.lambdaImplementation;
        LambdaInterface<?> l_iface = l_impl.lambdaInterface;
        FunExpr object = object_(l_impl.newFun(l_inst.fieldValues), l_iface.interfaceClass);
        return rewrite(object.invoke(l_iface.interfaceClass, l_iface.methodName, e1.parameters));
    }).applyIf(ObjectFunExpr.class, e1 -> {
        return objectField(e1.object, e1.type);
    }).applyIf(PlaceholderFunExpr.class, e1 -> {
        FunExpr e2 = placeholders.get(e1);
        if (e2 != null)
            return e2;
        else
            return Fail.t("cannot resolve placeholder");
    }).applyIf(ProfileFunExpr.class, e1 -> {
        fieldTypeValues.put(e1.counterFieldName, Pair.of(Type.INT, 0));
        return null;
    }).result();
}
Also used : FieldStaticFunExpr(suite.jdk.gen.FunExprM.FieldStaticFunExpr) Read(suite.streamlet.Read) PlaceholderFunExpr(suite.jdk.gen.FunExprK.PlaceholderFunExpr) InvokeLambdaFunExpr(suite.jdk.gen.FunExprL.InvokeLambdaFunExpr) NewFunExpr(suite.jdk.gen.FunExprM.NewFunExpr) AssignLocalFunExpr(suite.jdk.gen.FunExprM.AssignLocalFunExpr) Util(suite.util.Util) HashMap(java.util.HashMap) Type_(suite.jdk.gen.Type_) Declare2ParameterFunExpr(suite.jdk.gen.FunExprK.Declare2ParameterFunExpr) ApplyFunExpr(suite.jdk.gen.FunExprL.ApplyFunExpr) ProfileFunExpr(suite.jdk.gen.FunExprM.ProfileFunExpr) ArrayList(java.util.ArrayList) LambdaImplementation(suite.jdk.lambda.LambdaImplementation) FieldInjectFunExpr(suite.jdk.gen.FunExprL.FieldInjectFunExpr) Declare0ParameterFunExpr(suite.jdk.gen.FunExprK.Declare0ParameterFunExpr) FieldSetFunExpr(suite.jdk.gen.FunExprL.FieldSetFunExpr) Rethrow(suite.util.Rethrow) LambdaInstance(suite.jdk.lambda.LambdaInstance) Map(java.util.Map) Type(org.apache.bcel.generic.Type) FunCreator(suite.jdk.gen.FunCreator) CastFunExpr(suite.jdk.gen.FunExprM.CastFunExpr) FunExpr(suite.jdk.gen.FunExpression.FunExpr) FunFactory(suite.jdk.gen.FunFactory) Method(java.lang.reflect.Method) LambdaInterface(suite.jdk.lambda.LambdaInterface) ObjectFunExpr(suite.jdk.gen.FunExprL.ObjectFunExpr) DeclareLocalFunExpr(suite.jdk.gen.FunExprL.DeclareLocalFunExpr) Streamlet2(suite.streamlet.Streamlet2) DeclareParameterFunExpr(suite.jdk.gen.FunExprK.DeclareParameterFunExpr) Field(java.lang.reflect.Field) FieldFunExpr_(suite.jdk.gen.FunExprL.FieldFunExpr_) Pair(suite.adt.pair.Pair) List(java.util.List) Declare1ParameterFunExpr(suite.jdk.gen.FunExprK.Declare1ParameterFunExpr) Fail(suite.util.Fail) ApplyFunExpr(suite.jdk.gen.FunExprL.ApplyFunExpr) HashMap(java.util.HashMap) ObjectFunExpr(suite.jdk.gen.FunExprL.ObjectFunExpr) Field(java.lang.reflect.Field) PlaceholderFunExpr(suite.jdk.gen.FunExprK.PlaceholderFunExpr) FieldSetFunExpr(suite.jdk.gen.FunExprL.FieldSetFunExpr) NewFunExpr(suite.jdk.gen.FunExprM.NewFunExpr) FieldStaticFunExpr(suite.jdk.gen.FunExprM.FieldStaticFunExpr) ProfileFunExpr(suite.jdk.gen.FunExprM.ProfileFunExpr) FunCreator(suite.jdk.gen.FunCreator) Method(java.lang.reflect.Method) DeclareParameterFunExpr(suite.jdk.gen.FunExprK.DeclareParameterFunExpr) Type(org.apache.bcel.generic.Type) AssignLocalFunExpr(suite.jdk.gen.FunExprM.AssignLocalFunExpr) DeclareLocalFunExpr(suite.jdk.gen.FunExprL.DeclareLocalFunExpr) FieldStaticFunExpr(suite.jdk.gen.FunExprM.FieldStaticFunExpr) PlaceholderFunExpr(suite.jdk.gen.FunExprK.PlaceholderFunExpr) InvokeLambdaFunExpr(suite.jdk.gen.FunExprL.InvokeLambdaFunExpr) NewFunExpr(suite.jdk.gen.FunExprM.NewFunExpr) AssignLocalFunExpr(suite.jdk.gen.FunExprM.AssignLocalFunExpr) Declare2ParameterFunExpr(suite.jdk.gen.FunExprK.Declare2ParameterFunExpr) ApplyFunExpr(suite.jdk.gen.FunExprL.ApplyFunExpr) ProfileFunExpr(suite.jdk.gen.FunExprM.ProfileFunExpr) FieldInjectFunExpr(suite.jdk.gen.FunExprL.FieldInjectFunExpr) Declare0ParameterFunExpr(suite.jdk.gen.FunExprK.Declare0ParameterFunExpr) FieldSetFunExpr(suite.jdk.gen.FunExprL.FieldSetFunExpr) CastFunExpr(suite.jdk.gen.FunExprM.CastFunExpr) FunExpr(suite.jdk.gen.FunExpression.FunExpr) ObjectFunExpr(suite.jdk.gen.FunExprL.ObjectFunExpr) DeclareLocalFunExpr(suite.jdk.gen.FunExprL.DeclareLocalFunExpr) DeclareParameterFunExpr(suite.jdk.gen.FunExprK.DeclareParameterFunExpr) Declare1ParameterFunExpr(suite.jdk.gen.FunExprK.Declare1ParameterFunExpr) FieldInjectFunExpr(suite.jdk.gen.FunExprL.FieldInjectFunExpr)

Aggregations

Field (java.lang.reflect.Field)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Pair (suite.adt.pair.Pair)2 Type_ (suite.jdk.gen.Type_)2 Read (suite.streamlet.Read)2 Closeable (java.io.Closeable)1 IOException (java.io.IOException)1 Array (java.lang.reflect.Array)1 Constructor (java.lang.reflect.Constructor)1 Method (java.lang.reflect.Method)1 Modifier (java.lang.reflect.Modifier)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 Collection (java.util.Collection)1 Comparator (java.util.Comparator)1 Type (org.apache.bcel.generic.Type)1 Inspect (suite.inspect.Inspect)1