Search in sources :

Example 36 with AnalysisType

use of com.oracle.graal.pointsto.meta.AnalysisType in project graal by oracle.

the class BigBang method addSystemField.

@SuppressWarnings("try")
public AnalysisType addSystemField(Class<?> clazz, String fieldName) {
    AnalysisType type = addSystemClass(clazz, false, false);
    for (AnalysisField field : type.getInstanceFields(true)) {
        if (field.getName().equals(fieldName)) {
            try (Indent indent = debug.logAndIndent("add system field %s in class %s", fieldName, clazz.getName())) {
                field.registerAsAccessed();
                /*
                     * For system classes any instantiated (sub)type of the declared field type can
                     * be written to the field flow.
                     */
                TypeFlow<?> fieldDeclaredTypeFlow = field.getType().getTypeFlow(this, true);
                fieldDeclaredTypeFlow.addUse(this, type.getContextInsensitiveAnalysisObject().getInstanceFieldFlow(this, field, true));
            }
            return field.getType();
        }
    }
    throw shouldNotReachHere("field not found: " + fieldName);
}
Also used : AnalysisType(com.oracle.graal.pointsto.meta.AnalysisType) Indent(org.graalvm.compiler.debug.Indent) AnalysisField(com.oracle.graal.pointsto.meta.AnalysisField)

Example 37 with AnalysisType

use of com.oracle.graal.pointsto.meta.AnalysisType in project graal by oracle.

the class ObjectScanner method scanArray.

/**
 * Scans constant arrays, one element at the time.
 *
 * @param array the array to be scanned
 * @param reason what triggered the scanning
 */
protected final void scanArray(JavaConstant array, Object reason) {
    Object valueObj = bb.getSnippetReflectionProvider().asObject(Object.class, array);
    AnalysisType arrayType = bb.getMetaAccess().lookupJavaType(valueObj.getClass());
    assert valueObj instanceof Object[];
    try {
        Object[] arrayObject = (Object[]) valueObj;
        for (int idx = 0; idx < arrayObject.length; idx++) {
            Object e = arrayObject[idx];
            if (e == null) {
                forNullArrayElement(array, arrayType, idx);
            } else {
                Object element = bb.getUniverse().replaceObject(e);
                JavaConstant elementConstant = bb.getSnippetReflectionProvider().forObject(element);
                AnalysisType elementType = bb.getMetaAccess().lookupJavaType(element.getClass());
                propagateRoot(array, elementConstant);
                /* Scan the array element. */
                scanConstant(elementConstant, reason);
                /* Process the array element. */
                forNonNullArrayElement(array, arrayType, elementConstant, elementType, idx);
            }
        }
    } catch (UnsupportedFeatureException ex) {
        unsupportedFeature(arrayType.toJavaName(true), ex.getMessage(), reason);
    }
}
Also used : AnalysisType(com.oracle.graal.pointsto.meta.AnalysisType) UnsupportedFeatureException(com.oracle.graal.pointsto.constraints.UnsupportedFeatureException) JavaConstant(jdk.vm.ci.meta.JavaConstant)

Example 38 with AnalysisType

use of com.oracle.graal.pointsto.meta.AnalysisType in project graal by oracle.

the class ReflectionDataBuilder method duringAnalysis.

protected void duringAnalysis(DuringAnalysisAccess a) {
    DuringAnalysisAccessImpl access = (DuringAnalysisAccessImpl) a;
    if (!modified) {
        return;
    }
    modified = false;
    access.requireAnalysisIteration();
    Method reflectionDataMethod = findMethod(Class.class, "reflectionData");
    Class<?> originalReflectionDataClass = access.getImageClassLoader().findClassByName("java.lang.Class$ReflectionData");
    Field declaredFieldsField = findField(originalReflectionDataClass, "declaredFields");
    Field publicFieldsField = findField(originalReflectionDataClass, "publicFields");
    Field declaredMethodsField = findField(originalReflectionDataClass, "declaredMethods");
    Field publicMethodsField = findField(originalReflectionDataClass, "publicMethods");
    Field declaredConstructorsField = findField(originalReflectionDataClass, "declaredConstructors");
    Field publicConstructorsField = findField(originalReflectionDataClass, "publicConstructors");
    Field declaredPublicFieldsField = findField(originalReflectionDataClass, "declaredPublicFields");
    Field declaredPublicMethodsField = findField(originalReflectionDataClass, "declaredPublicMethods");
    Field[] emptyFields = new Field[0];
    Method[] emptyMethods = new Method[0];
    Constructor<?>[] emptyConstructors = new Constructor<?>[0];
    Set<Class<?>> allClasses = new HashSet<>(reflectionClasses);
    reflectionMethods.stream().map(method -> method.getDeclaringClass()).forEach(clazz -> allClasses.add(clazz));
    reflectionFields.stream().map(field -> field.getDeclaringClass()).forEach(clazz -> allClasses.add(clazz));
    /*
         * We need to find all classes that have an enclosingMethod or enclosingConstructor.
         * Unfortunately, there is no reverse lookup (ask a Method or Constructor about the classes
         * they contain), so we need to iterate through all types that have been loaded so far.
         * Accessing the original java.lang.Class for a ResolvedJavaType is not 100% reliable,
         * especially in the case of class and method substitutions. But it is the best we can do
         * here, and we assume that user code that requires reflection support is not using
         * substitutions.
         */
    for (AnalysisType aType : access.getUniverse().getTypes()) {
        Class<?> originalClass = aType.getJavaClass();
        if (originalClass != null && enclosingMethodOrConstructor(originalClass) != null) {
            /*
                 * We haven an enclosing method or constructor for this class, so we add the class
                 * to the set of processed classes so that the ReflectionData is initialized below.
                 */
            allClasses.add(originalClass);
        }
    }
    for (Class<?> clazz : allClasses) {
        DynamicHub hub = access.getHostVM().dynamicHub(access.getMetaAccess().lookupJavaType(clazz));
        if (reflectionClasses.contains(clazz)) {
            ClassForNameSupport.registerClass(clazz);
        }
        /*
             * Ensure all internal fields of the original Class.ReflectionData object are
             * initialized. Calling the public methods triggers lazy initialization of the fields.
             */
        clazz.getDeclaredFields();
        clazz.getFields();
        clazz.getDeclaredMethods();
        clazz.getMethods();
        clazz.getDeclaredConstructors();
        clazz.getConstructors();
        try {
            Object originalReflectionData = reflectionDataMethod.invoke(clazz);
            hub.setReflectionData(new DynamicHub.ReflectionData(filter(declaredFieldsField.get(originalReflectionData), reflectionFields, emptyFields), filter(publicFieldsField.get(originalReflectionData), reflectionFields, emptyFields), filter(declaredMethodsField.get(originalReflectionData), reflectionMethods, emptyMethods), filter(publicMethodsField.get(originalReflectionData), reflectionMethods, emptyMethods), filter(declaredConstructorsField.get(originalReflectionData), reflectionMethods, emptyConstructors), filter(publicConstructorsField.get(originalReflectionData), reflectionMethods, emptyConstructors), nullaryConstructor(declaredConstructorsField.get(originalReflectionData), reflectionMethods), filter(declaredPublicFieldsField.get(originalReflectionData), reflectionFields, emptyFields), filter(declaredPublicMethodsField.get(originalReflectionData), reflectionMethods, emptyMethods), enclosingMethodOrConstructor(clazz)));
        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
            throw VMError.shouldNotReachHere(ex);
        }
    }
}
Also used : Arrays(java.util.Arrays) AccessibleObject(java.lang.reflect.AccessibleObject) DuringAnalysisAccess(org.graalvm.nativeimage.Feature.DuringAnalysisAccess) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) DynamicHub(com.oracle.svm.core.hub.DynamicHub) DuringAnalysisAccessImpl(com.oracle.svm.hosted.FeatureImpl.DuringAnalysisAccessImpl) Field(java.lang.reflect.Field) Constructor(java.lang.reflect.Constructor) InvocationTargetException(java.lang.reflect.InvocationTargetException) ArrayList(java.util.ArrayList) AnalysisType(com.oracle.graal.pointsto.meta.AnalysisType) VMError(com.oracle.svm.core.util.VMError) HashSet(java.util.HashSet) List(java.util.List) UserError(com.oracle.svm.core.util.UserError) Executable(java.lang.reflect.Executable) ClassForNameSupport(com.oracle.svm.core.hub.ClassForNameSupport) Method(java.lang.reflect.Method) Collections(java.util.Collections) RuntimeReflectionSupport(com.oracle.svm.core.RuntimeReflection.RuntimeReflectionSupport) AnalysisType(com.oracle.graal.pointsto.meta.AnalysisType) Constructor(java.lang.reflect.Constructor) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) Field(java.lang.reflect.Field) DynamicHub(com.oracle.svm.core.hub.DynamicHub) DuringAnalysisAccessImpl(com.oracle.svm.hosted.FeatureImpl.DuringAnalysisAccessImpl) AccessibleObject(java.lang.reflect.AccessibleObject) HashSet(java.util.HashSet)

Aggregations

AnalysisType (com.oracle.graal.pointsto.meta.AnalysisType)38 AnalysisField (com.oracle.graal.pointsto.meta.AnalysisField)10 TypeState (com.oracle.graal.pointsto.typestate.TypeState)7 AnalysisMethod (com.oracle.graal.pointsto.meta.AnalysisMethod)6 AnalysisObject (com.oracle.graal.pointsto.flow.context.object.AnalysisObject)5 DynamicHub (com.oracle.svm.core.hub.DynamicHub)5 ArrayList (java.util.ArrayList)5 UnsupportedFeatureException (com.oracle.graal.pointsto.constraints.UnsupportedFeatureException)4 SVMHost (com.oracle.svm.hosted.SVMHost)4 JavaKind (jdk.vm.ci.meta.JavaKind)4 Indent (org.graalvm.compiler.debug.Indent)4 MethodTypeFlow (com.oracle.graal.pointsto.flow.MethodTypeFlow)3 BytecodeLocation (com.oracle.graal.pointsto.flow.context.BytecodeLocation)3 AnalysisMetaAccess (com.oracle.graal.pointsto.meta.AnalysisMetaAccess)3 AnalysisUniverse (com.oracle.graal.pointsto.meta.AnalysisUniverse)3 DuringAnalysisAccessImpl (com.oracle.svm.hosted.FeatureImpl.DuringAnalysisAccessImpl)3 HostedType (com.oracle.svm.hosted.meta.HostedType)3 CEntryPoint (org.graalvm.nativeimage.c.function.CEntryPoint)3 BigBang (com.oracle.graal.pointsto.BigBang)2 HostedProviders (com.oracle.graal.pointsto.meta.HostedProviders)2