Java tutorial
//package com.java2s; /******************************************************************************* * Copyright (c) 2010-present Sonatype, Inc. * 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: * Stuart McCulloch (Sonatype, Inc.) - initial API and implementation *******************************************************************************/ import java.lang.reflect.Field; import java.security.AccessController; import java.security.PrivilegedAction; public class Main { private static void setField(final Object bean, final Field field, final Object value) throws Exception { if (!field.isAccessible()) { AccessController.doPrivileged(new PrivilegedAction<Void>() { public Void run() { field.setAccessible(true); return null; } }); } field.set(bean, value); } }