<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>El blog de Deigote &#187; estado</title>
	<atom:link href="http://blog.deigote.com/tag/estado/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.deigote.com</link>
	<description>El mundo de Deigote. Un diario de cualquier cosa que me resulte interesante (si a alguien más se lo resulta, es otro cantar). Espero que os guste o disguste. Incluso que os deje indiferentes sería una opción tan buena como cualquier otra.</description>
	<lastBuildDate>Thu, 07 Apr 2011 15:29:52 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Java: cambiar el valor de un atributo privado de un objeto</title>
		<link>http://blog.deigote.com/2007/05/29/java-cambiar-el-valor-un-atributo-privado-de-un-objeto/</link>
		<comments>http://blog.deigote.com/2007/05/29/java-cambiar-el-valor-un-atributo-privado-de-un-objeto/#comments</comments>
		<pubDate>Tue, 29 May 2007 11:17:34 +0000</pubDate>
		<dc:creator>Deigote</dc:creator>
				<category><![CDATA[Informática, internet y tecnología]]></category>
		<category><![CDATA[atributos privados]]></category>
		<category><![CDATA[estado]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[programación]]></category>

		<guid isPermaLink="false">http://deigote.com/blog/2007/05/29/java-cambiar-el-valor-un-atributo-privado-de-un-objeto/</guid>
		<description><![CDATA[En Java a veces es necesario cambiar el valor de un atributo privado de un objeto. Por suerte, la reflexión nos permite hace esto de forma sencilla. El código, como vereis, no puede ser más simple:
public static void setField (Object o, String fieldName, Object newValue)
	throws IllegalArgumentException, IllegalAccessException {
	// Get all the object class fields
	final Field [...]]]></description>
			<content:encoded><![CDATA[<p>En Java a veces es necesario cambiar el valor de un atributo privado de un objeto. Por suerte, la reflexión nos permite hace esto de forma sencilla. El código, como vereis, no puede ser más simple:</p>
<pre><code>public static void setField (Object o, String fieldName, Object newValue)
	throws IllegalArgumentException, IllegalAccessException {
	// Get all the object class fields
	final Field fields[] = o.getClass().getDeclaredFields();
	// Search the requested field
	for (int i = 0; i &lt; fields.length; ++i)
		// If found, set its new value
		if (fieldName.equals(fields[i].getName())) {
			boolean accesible = fields[i].isAccessible();
			fields[i].setAccessible(true);
			fields[i].set(o, newValue);
			fields[i].setAccessible(accesible);
		}
}
</code></pre>
<p>Un sencillo código para hacer la prueba puede ser el siguiente, que juega con el atributo privado <em>hash</em> de la clase String:</p>
<pre><code>public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException {
	String s = new String("abc");
	System.out.println("hashcode: " + s.hashCode());
	setField(s, "hash", 30);
	System.out.println("hashcode: " + s.hashCode());
}
</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.deigote.com/2007/05/29/java-cambiar-el-valor-un-atributo-privado-de-un-objeto/feed/</wfw:commentRss>
		<slash:comments>27</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
