// Copyright (c) 2015 Intel Corporation. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.xwalk.core.internal;

import java.lang.reflect.Field;
import java.util.concurrent.RejectedExecutionException;

class ReflectField {
    private Object mInstance;
    private Class<?> mClass;
    private String mName;
    private Field mField;

    public ReflectField() {
    }

    public ReflectField(Object instance, String name) {
        init(instance, null, name);
    }

    public ReflectField(Class<?> clazz, String name) {
        init(null, clazz, name);
    }

    public boolean init(Object instance, Class<?> clazz, String name) {
        mInstance = instance;
        mClass = clazz != null ? clazz : (instance != null ? instance.getClass() : null);
        mName = name;
        mField = null;

        if (mClass == null) return false;

        try {
            mField = mClass.getField(mName);
        } catch (NoSuchFieldException e) {
            try {
                mField = mClass.getDeclaredField(mName);
                mField.setAccessible(true);
            } catch (NoSuchFieldException e2) {
            }
        }
        return mField != null;
    }

    public Object get() {
        if (mField == null) {
            throw new UnsupportedOperationException(toString());
        }

        try {
            return mField.get(mInstance);
        } catch (IllegalAccessException | NullPointerException e) {
            throw new RejectedExecutionException(e);
        } catch (IllegalArgumentException e) {
            throw e;
        } catch (ExceptionInInitializerError e) {
            throw new RuntimeException(e);
        }
    }

    public boolean isNull() {
        return mField == null;
    }

    public String toString() {
        if (mField != null) return mField.toString();

        String ret = "";
        if (mClass != null) ret += mClass.toString() + ".";
        if (mName != null) ret += mName;
        return ret;
    }

    public String getName() {
        return mName;
    }

    public Object getInstance() {
        return mInstance;
    }
}

@@ -1020,32 +1020,130 @@
 dException e) %7B%0A
+            for (Class%3C?%3E parent = mClass; parent != null; parent = parent.getSuperclass()) %7B%0A    
             try 
@@ -1136,32 +1136,36 @@
           try %7B%0A
+    
                 
@@ -1169,30 +1169,30 @@
    mField = 
-mClass
+parent
 .getDeclared
@@ -1197,32 +1197,36 @@
 edField(mName);%0A
+    
                 
@@ -1249,24 +1249,55 @@
 ible(true);%0A
+                    break;%0A    
             
@@ -1328,24 +1328,42 @@
 ption e2) %7B%0A
+                %7D%0A
             

Fix crash when setting surface type through layout