Search in sources :

Example 1 with MethodDescriptor.ofConstructor

use of io.quarkus.gizmo.MethodDescriptor.ofConstructor in project quarkus by quarkusio.

the class BytecodeRecorderImpl method loadObjectInstanceImpl.

/**
 * Returns a representation of a serialized parameter.
 */
private DeferredParameter loadObjectInstanceImpl(Object param, Map<Object, DeferredParameter> existing, Class<?> expectedType, boolean relaxedValidation) {
    // null is easy
    if (param == null) {
        return new DeferredParameter() {

            @Override
            ResultHandle doLoad(MethodContext creator, MethodCreator method, ResultHandle array) {
                return method.loadNull();
            }
        };
    }
    // check the loaded object support (i.e. config) to see if this is a config item
    DeferredParameter loadedObject = findLoaded(param);
    if (loadedObject != null) {
        return loadedObject;
    }
    // Handle empty collections as returned by the Collections object
    loadedObject = handleCollectionsObjects(param, existing, relaxedValidation);
    if (loadedObject != null) {
        return loadedObject;
    }
    // but some are quite complex when dealing with objects and collections
    if (substitutions.containsKey(param.getClass()) || substitutions.containsKey(expectedType)) {
        // check for substitution types, if present we invoke recursively on the substitution
        SubstitutionHolder holder = substitutions.get(param.getClass());
        if (holder == null) {
            holder = substitutions.get(expectedType);
        }
        try {
            ObjectSubstitution substitution = holder.sub.getDeclaredConstructor().newInstance();
            Object res = substitution.serialize(param);
            DeferredParameter serialized = loadObjectInstance(res, existing, holder.to, relaxedValidation);
            SubstitutionHolder finalHolder = holder;
            return new DeferredArrayStoreParameter(param, expectedType) {

                @Override
                void doPrepare(MethodContext context) {
                    serialized.prepare(context);
                    super.doPrepare(context);
                }

                @Override
                ResultHandle createValue(MethodContext creator, MethodCreator method, ResultHandle array) {
                    ResultHandle subInstance = method.newInstance(MethodDescriptor.ofConstructor(finalHolder.sub));
                    return method.invokeInterfaceMethod(ofMethod(ObjectSubstitution.class, "deserialize", Object.class, Object.class), subInstance, creator.loadDeferred(serialized));
                }
            };
        } catch (Exception e) {
            throw new RuntimeException("Failed to substitute " + param, e);
        }
    } else if (param instanceof Optional) {
        Optional val = (Optional) param;
        if (val.isPresent()) {
            DeferredParameter res = loadObjectInstance(val.get(), existing, Object.class, relaxedValidation);
            return new DeferredArrayStoreParameter(param, expectedType) {

                @Override
                void doPrepare(MethodContext context) {
                    res.prepare(context);
                    super.doPrepare(context);
                }

                @Override
                ResultHandle createValue(MethodContext context, MethodCreator method, ResultHandle array) {
                    // so we need to use 'ofNullable' and not 'of' here.
                    return method.invokeStaticMethod(ofMethod(Optional.class, "ofNullable", Optional.class, Object.class), context.loadDeferred(res));
                }
            };
        } else {
            return new DeferredArrayStoreParameter(param, expectedType) {

                @Override
                ResultHandle createValue(MethodContext context, MethodCreator method, ResultHandle array) {
                    return method.invokeStaticMethod(ofMethod(Optional.class, "empty", Optional.class));
                }
            };
        }
    } else if (param instanceof String) {
        if (((String) param).length() > 65535) {
            throw new RuntimeException("String too large to record: " + param);
        }
        return new DeferredParameter() {

            @Override
            ResultHandle doLoad(MethodContext context, MethodCreator method, ResultHandle array) {
                return method.load((String) param);
            }
        };
    } else if (param instanceof URL) {
        String url = ((URL) param).toExternalForm();
        return new DeferredParameter() {

            @Override
            ResultHandle doLoad(MethodContext context, MethodCreator method, ResultHandle array) {
                AssignableResultHandle value = method.createVariable(URL.class);
                try (TryBlock et = method.tryBlock()) {
                    et.assign(value, et.newInstance(MethodDescriptor.ofConstructor(URL.class, String.class), et.load(url)));
                    try (CatchBlockCreator malformed = et.addCatch(MalformedURLException.class)) {
                        malformed.throwException(RuntimeException.class, "Malformed URL", malformed.getCaughtException());
                    }
                }
                return value;
            }
        };
    } else if (param instanceof Enum) {
        Enum e = (Enum) param;
        return new DeferredParameter() {

            @Override
            ResultHandle doLoad(MethodContext context, MethodCreator method, ResultHandle array) {
                ResultHandle nm = method.load(e.name());
                return method.invokeStaticMethod(ofMethod(e.getDeclaringClass(), "valueOf", e.getDeclaringClass(), String.class), nm);
            }
        };
    } else if (param instanceof ReturnedProxy) {
        // if this is a proxy we just grab the value from the StartupContext
        ReturnedProxy rp = (ReturnedProxy) param;
        if (!rp.__static$$init() && staticInit) {
            throw new RuntimeException("Invalid proxy passed to recorder. " + rp + " was created in a runtime recorder method, while this recorder is for a static init method. The object will not have been created at the time this method is run.");
        }
        String proxyId = rp.__returned$proxy$key();
        // we just load it from the startup context
        return new DeferredParameter() {

            @Override
            ResultHandle doLoad(MethodContext context, MethodCreator method, ResultHandle array) {
                return method.invokeVirtualMethod(ofMethod(StartupContext.class, "getValue", Object.class, String.class), method.getMethodParam(0), method.load(proxyId));
            }
        };
    } else if (param instanceof Duration) {
        return new DeferredParameter() {

            @Override
            ResultHandle doLoad(MethodContext context, MethodCreator method, ResultHandle array) {
                return method.invokeStaticMethod(ofMethod(Duration.class, "parse", Duration.class, CharSequence.class), method.load(param.toString()));
            }
        };
    } else if (param instanceof Class<?>) {
        if (!((Class) param).isPrimitive()) {
            // Only try to load the class by name if it is not a primitive class
            String name = classProxies.get(param);
            if (name == null) {
                name = ((Class) param).getName();
            }
            String finalName = name;
            return new DeferredParameter() {

                @Override
                ResultHandle doLoad(MethodContext context, MethodCreator method, ResultHandle array) {
                    ResultHandle currentThread = method.invokeStaticMethod(ofMethod(Thread.class, "currentThread", Thread.class));
                    ResultHandle tccl = method.invokeVirtualMethod(ofMethod(Thread.class, "getContextClassLoader", ClassLoader.class), currentThread);
                    return method.invokeStaticMethod(ofMethod(Class.class, "forName", Class.class, String.class, boolean.class, ClassLoader.class), method.load(finalName), method.load(true), tccl);
                }
            };
        } else {
            // Else load the primitive type by reference; double.class => Class var9 = Double.TYPE;
            return new DeferredParameter() {

                @Override
                ResultHandle doLoad(MethodContext context, MethodCreator method, ResultHandle array) {
                    return method.loadClassFromTCCL((Class) param);
                }
            };
        }
    } else if (expectedType == boolean.class || expectedType == Boolean.class || param instanceof Boolean) {
        return new DeferredParameter() {

            @Override
            ResultHandle doLoad(MethodContext context, MethodCreator method, ResultHandle array) {
                return method.load((boolean) param);
            }
        };
    } else if (expectedType == int.class || expectedType == Integer.class || param instanceof Integer) {
        return new DeferredParameter() {

            @Override
            ResultHandle doLoad(MethodContext context, MethodCreator method, ResultHandle array) {
                return method.load((int) param);
            }
        };
    } else if (expectedType == short.class || expectedType == Short.class || param instanceof Short) {
        return new DeferredParameter() {

            @Override
            ResultHandle doLoad(MethodContext context, MethodCreator method, ResultHandle array) {
                return method.load((short) param);
            }
        };
    } else if (expectedType == byte.class || expectedType == Byte.class || param instanceof Byte) {
        return new DeferredParameter() {

            @Override
            ResultHandle doLoad(MethodContext context, MethodCreator method, ResultHandle array) {
                return method.load((byte) param);
            }
        };
    } else if (expectedType == char.class || expectedType == Character.class || param instanceof Character) {
        return new DeferredParameter() {

            @Override
            ResultHandle doLoad(MethodContext context, MethodCreator method, ResultHandle array) {
                return method.load((char) param);
            }
        };
    } else if (expectedType == long.class || expectedType == Long.class || param instanceof Long) {
        return new DeferredParameter() {

            @Override
            ResultHandle doLoad(MethodContext context, MethodCreator method, ResultHandle array) {
                return method.load((long) param);
            }
        };
    } else if (expectedType == float.class || expectedType == Float.class || param instanceof Float) {
        return new DeferredParameter() {

            @Override
            ResultHandle doLoad(MethodContext context, MethodCreator method, ResultHandle array) {
                return method.load((float) param);
            }
        };
    } else if (expectedType == double.class || expectedType == Double.class || param instanceof Double) {
        return new DeferredParameter() {

            @Override
            ResultHandle doLoad(MethodContext context, MethodCreator method, ResultHandle array) {
                return method.load((double) param);
            }
        };
    } else if (expectedType.isArray()) {
        int length = Array.getLength(param);
        DeferredParameter[] components = new DeferredParameter[length];
        for (int i = 0; i < length; ++i) {
            DeferredParameter component = loadObjectInstance(Array.get(param, i), existing, expectedType.getComponentType(), relaxedValidation);
            components[i] = component;
        }
        return new DeferredArrayStoreParameter(param, expectedType) {

            @Override
            void doPrepare(MethodContext context) {
                for (int i = 0; i < length; ++i) {
                    components[i].prepare(context);
                }
                super.doPrepare(context);
            }

            @Override
            ResultHandle createValue(MethodContext context, MethodCreator method, ResultHandle array) {
                // TODO large arrays can still generate a fair bit of bytecode, and there appears to be a gizmo issue that prevents casting to an array
                // fix this later
                ResultHandle out = method.newArray(expectedType.getComponentType(), length);
                for (int i = 0; i < length; ++i) {
                    method.writeArrayValue(out, i, context.loadDeferred(components[i]));
                }
                return out;
            }
        };
    } else if (param instanceof AnnotationProxy) {
        // new com.foo.MyAnnotation_Proxy_AnnotationLiteral("foo")
        AnnotationProxy annotationProxy = (AnnotationProxy) param;
        List<MethodInfo> constructorParams = annotationProxy.getAnnotationClass().methods().stream().filter(m -> !m.name().equals("<clinit>") && !m.name().equals("<init>")).collect(Collectors.toList());
        Map<String, AnnotationValue> annotationValues = annotationProxy.getAnnotationInstance().values().stream().collect(Collectors.toMap(AnnotationValue::name, Function.identity()));
        DeferredParameter[] constructorParamsHandles = new DeferredParameter[constructorParams.size()];
        for (ListIterator<MethodInfo> iterator = constructorParams.listIterator(); iterator.hasNext(); ) {
            MethodInfo valueMethod = iterator.next();
            Object explicitValue = annotationProxy.getValues().get(valueMethod.name());
            if (explicitValue != null) {
                constructorParamsHandles[iterator.previousIndex()] = loadObjectInstance(explicitValue, existing, explicitValue.getClass(), relaxedValidation);
            } else {
                AnnotationValue value = annotationValues.get(valueMethod.name());
                if (value == null) {
                    // method.invokeInterfaceMethod(MAP_PUT, valuesHandle, method.load(entry.getKey()), loadObjectInstance(method, entry.getValue(),
                    // returnValueResults, entry.getValue().getClass()));
                    Object defaultValue = annotationProxy.getDefaultValues().get(valueMethod.name());
                    if (defaultValue != null) {
                        constructorParamsHandles[iterator.previousIndex()] = loadObjectInstance(defaultValue, existing, defaultValue.getClass(), relaxedValidation);
                        continue;
                    }
                    if (value == null) {
                        value = valueMethod.defaultValue();
                    }
                }
                if (value == null) {
                    throw new NullPointerException("Value not set for " + param);
                }
                DeferredParameter retValue = loadValue(value, annotationProxy.getAnnotationClass(), valueMethod);
                constructorParamsHandles[iterator.previousIndex()] = retValue;
            }
        }
        return new DeferredArrayStoreParameter(annotationProxy.getAnnotationLiteralType()) {

            @Override
            ResultHandle createValue(MethodContext context, MethodCreator method, ResultHandle array) {
                MethodDescriptor constructor = MethodDescriptor.ofConstructor(annotationProxy.getAnnotationLiteralType(), constructorParams.stream().map(m -> m.returnType().name().toString()).toArray());
                ResultHandle[] args = new ResultHandle[constructorParamsHandles.length];
                for (int i = 0; i < constructorParamsHandles.length; i++) {
                    DeferredParameter deferredParameter = constructorParamsHandles[i];
                    if (deferredParameter instanceof DeferredArrayStoreParameter) {
                        DeferredArrayStoreParameter arrayParam = (DeferredArrayStoreParameter) deferredParameter;
                        arrayParam.doPrepare(context);
                    }
                    args[i] = context.loadDeferred(deferredParameter);
                }
                return method.newInstance(constructor, args);
            }
        };
    } else {
        return loadComplexObject(param, existing, expectedType, relaxedValidation);
    }
}
Also used : Arrays(java.util.Arrays) Array(java.lang.reflect.Array) SortedSet(java.util.SortedSet) ListIterator(java.util.ListIterator) URL(java.net.URL) ClassOutput(io.quarkus.gizmo.ClassOutput) ClassInfo(org.jboss.jandex.ClassInfo) MethodInfo(org.jboss.jandex.MethodInfo) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RecordableConstructor(io.quarkus.runtime.annotations.RecordableConstructor) Duration(java.time.Duration) Map(java.util.Map) BytecodeCreator(io.quarkus.gizmo.BytecodeCreator) StartupContext(io.quarkus.runtime.StartupContext) Method(java.lang.reflect.Method) AnnotationValue(org.jboss.jandex.AnnotationValue) Assert(org.wildfly.common.Assert) IdentityHashMap(java.util.IdentityHashMap) ProxyConfiguration(io.quarkus.deployment.proxy.ProxyConfiguration) Collection(java.util.Collection) Property(io.quarkus.deployment.recording.PropertyUtils.Property) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) NavigableSet(java.util.NavigableSet) QuarkusClassLoader(io.quarkus.bootstrap.classloading.QuarkusClassLoader) Collectors(java.util.stream.Collectors) ProxyFactory(io.quarkus.deployment.proxy.ProxyFactory) List(java.util.List) RelaxedValidation(io.quarkus.runtime.annotations.RelaxedValidation) Modifier(java.lang.reflect.Modifier) Annotation(java.lang.annotation.Annotation) Optional(java.util.Optional) IgnoreProperty(io.quarkus.runtime.annotations.IgnoreProperty) ArrayType(org.jboss.jandex.ArrayType) ResultHandle(io.quarkus.gizmo.ResultHandle) SortedMap(java.util.SortedMap) TryBlock(io.quarkus.gizmo.TryBlock) MethodDescriptor.ofConstructor(io.quarkus.gizmo.MethodDescriptor.ofConstructor) CatchBlockCreator(io.quarkus.gizmo.CatchBlockCreator) Proxy(java.lang.reflect.Proxy) MethodCreator(io.quarkus.gizmo.MethodCreator) Type(org.jboss.jandex.Type) HashMap(java.util.HashMap) MethodDescriptor.ofMethod(io.quarkus.gizmo.MethodDescriptor.ofMethod) ClassCreator(io.quarkus.gizmo.ClassCreator) Constructor(java.lang.reflect.Constructor) Function(java.util.function.Function) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) Inject(javax.inject.Inject) Parameter(java.lang.reflect.Parameter) RuntimeValue(io.quarkus.runtime.RuntimeValue) AssignableResultHandle(io.quarkus.gizmo.AssignableResultHandle) StartupTask(io.quarkus.runtime.StartupTask) LinkedHashSet(java.util.LinkedHashSet) MethodDescriptor(io.quarkus.gizmo.MethodDescriptor) MalformedURLException(java.net.MalformedURLException) Field(java.lang.reflect.Field) FieldDescriptor(io.quarkus.gizmo.FieldDescriptor) AbstractMap(java.util.AbstractMap) ParameterizedType(java.lang.reflect.ParameterizedType) TreeMap(java.util.TreeMap) Closeable(java.io.Closeable) InvocationHandler(java.lang.reflect.InvocationHandler) AnnotationProxy(io.quarkus.deployment.recording.AnnotationProxyProvider.AnnotationProxy) ObjectSubstitution(io.quarkus.runtime.ObjectSubstitution) Collections(java.util.Collections) MalformedURLException(java.net.MalformedURLException) TryBlock(io.quarkus.gizmo.TryBlock) URL(java.net.URL) AnnotationProxy(io.quarkus.deployment.recording.AnnotationProxyProvider.AnnotationProxy) ObjectSubstitution(io.quarkus.runtime.ObjectSubstitution) ResultHandle(io.quarkus.gizmo.ResultHandle) AssignableResultHandle(io.quarkus.gizmo.AssignableResultHandle) List(java.util.List) ArrayList(java.util.ArrayList) Optional(java.util.Optional) Duration(java.time.Duration) ListIterator(java.util.ListIterator) MethodDescriptor(io.quarkus.gizmo.MethodDescriptor) MalformedURLException(java.net.MalformedURLException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MethodCreator(io.quarkus.gizmo.MethodCreator) AnnotationValue(org.jboss.jandex.AnnotationValue) AssignableResultHandle(io.quarkus.gizmo.AssignableResultHandle) MethodInfo(org.jboss.jandex.MethodInfo) CatchBlockCreator(io.quarkus.gizmo.CatchBlockCreator) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SortedMap(java.util.SortedMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) AbstractMap(java.util.AbstractMap) TreeMap(java.util.TreeMap)

Aggregations

QuarkusClassLoader (io.quarkus.bootstrap.classloading.QuarkusClassLoader)1 ProxyConfiguration (io.quarkus.deployment.proxy.ProxyConfiguration)1 ProxyFactory (io.quarkus.deployment.proxy.ProxyFactory)1 AnnotationProxy (io.quarkus.deployment.recording.AnnotationProxyProvider.AnnotationProxy)1 Property (io.quarkus.deployment.recording.PropertyUtils.Property)1 AssignableResultHandle (io.quarkus.gizmo.AssignableResultHandle)1 BytecodeCreator (io.quarkus.gizmo.BytecodeCreator)1 CatchBlockCreator (io.quarkus.gizmo.CatchBlockCreator)1 ClassCreator (io.quarkus.gizmo.ClassCreator)1 ClassOutput (io.quarkus.gizmo.ClassOutput)1 FieldDescriptor (io.quarkus.gizmo.FieldDescriptor)1 MethodCreator (io.quarkus.gizmo.MethodCreator)1 MethodDescriptor (io.quarkus.gizmo.MethodDescriptor)1 MethodDescriptor.ofConstructor (io.quarkus.gizmo.MethodDescriptor.ofConstructor)1 MethodDescriptor.ofMethod (io.quarkus.gizmo.MethodDescriptor.ofMethod)1 ResultHandle (io.quarkus.gizmo.ResultHandle)1 TryBlock (io.quarkus.gizmo.TryBlock)1 ObjectSubstitution (io.quarkus.runtime.ObjectSubstitution)1 RuntimeValue (io.quarkus.runtime.RuntimeValue)1 StartupContext (io.quarkus.runtime.StartupContext)1