publicify a field - Android java.lang.reflect

Android examples for java.lang.reflect:Field

Description

publicify a field

Demo Code

/**//w  w w.  j  ava2 s  .co  m
 *
 * Copyright (c) 2015-present, Total Location Test Paragraph.
 * All rights reserved.
 *
 * This file is part of Where@. Where@ is free software:
 * you can redistribute it and/or modify it under the terms of
 * the GNU General Public License (GPL), either version 3
 * of the License, or (at your option) any later version.
 *
 * Where@ is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
 * see the full license at <http://www.gnu.org/licenses/gpl-3.0.en.html>
 *
 */
//package com.java2s;

import java.lang.reflect.Field;

public class Main {
    public static <T> void override(Class<T> klass, String fieldName,
            Object val) {
        try {
            publicify(klass, fieldName).set(klass.getClass(), val);
        } catch (Exception e) {
            return;
        }
    }

    public static <T extends Class> Field publicify(T klass,
            String fieldName) {
        Field field;
        try {
            field = klass.getClass().getDeclaredField(fieldName);
            field.setAccessible(true);
            return field;
        } catch (Exception e) {
            return null;
        }
    }
}

Related Tutorials