Here you can find the source of setFieldVisible( Class extends Object> classToModify, String fieldName)
Parameter | Description |
---|---|
classToModify | The class to modify. |
fieldName | The name of the field to modify |
Parameter | Description |
---|---|
SecurityException | In case of problem |
NoSuchFieldException | In case of problem |
public static Field setFieldVisible( Class<? extends Object> classToModify, String fieldName) throws SecurityException, NoSuchFieldException
//package com.java2s; /******************************************************************************* * Copyright (c) 2011, 2012 THALES GLOBAL SERVICES. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors:/*from w ww . ja va2 s . com*/ * Obeo - initial API and implementation *******************************************************************************/ import java.lang.reflect.Field; public class Main { /** * Modify the visibility of a field. * * @param classToModify * The class to modify. * @param fieldName * The name of the field to modify * @return The modified field * @throws SecurityException * In case of problem * @throws NoSuchFieldException * In case of problem */ public static Field setFieldVisible( Class<? extends Object> classToModify, String fieldName) throws SecurityException, NoSuchFieldException { Field field = null; try { field = classToModify.getDeclaredField(fieldName); field.setAccessible(true); } catch (SecurityException e) { // DO nothing } catch (NoSuchFieldException e) { if (classToModify.getSuperclass() != null) { field = setFieldVisible(classToModify.getSuperclass(), fieldName); } } return field; } }