<?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>CADJEE Consulting &#187; reflect</title>
	<atom:link href="https://cadjee.fr/tag/reflect/feed/" rel="self" type="application/rss+xml" />
	<link>https://cadjee.fr</link>
	<description>Expertise Java &#38; Système d&#039;information</description>
	<lastBuildDate>Thu, 17 Dec 2015 21:37:19 +0000</lastBuildDate>
	<language>fr-FR</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.2.39</generator>
	<item>
		<title>Réflexion &amp; Généricité:  instanciation d&#8217;un objet à visibilité privé</title>
		<link>https://cadjee.fr/reflexion-genericite/</link>
		<comments>https://cadjee.fr/reflexion-genericite/#comments</comments>
		<pubDate>Wed, 18 Feb 2015 14:39:05 +0000</pubDate>
		<dc:creator><![CDATA[Ismael CADJEE]]></dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[reflect]]></category>

		<guid isPermaLink="false">http://cadjee.fr/?p=26</guid>
		<description><![CDATA[Faisons un petit tour sur un sujet bien vaste la réflexion et la généricité. Je vous propose d&#8217;instancier un objet JAVA ayant un constructeur private sans utiliser le mot clé new. Prêt? Un bean Personne avec constructeur privé Pour instancier cet objet utilisons l&#8217;outillage java.lang.reflect.* le setAccessible(true) nous permet en quelque sorte de &#171;&#160;hacker&#160;&#187; cette [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Faisons un petit tour sur un sujet bien vaste la réflexion et la généricité. Je vous propose d&rsquo;instancier un objet JAVA ayant un constructeur private sans utiliser le mot clé <span style="color: #993366;"><strong>new</strong></span>.<br />
Prêt?</p>
<p><span id="more-26"></span></p>
<p><span style="text-decoration: underline;"><strong>Un bean Personne avec constructeur privé</strong></span></p>
<pre class="brush: java; title: ; notranslate">
public class Personne {
	private String nom;

	private Personne() {
	}

	public String getNom() {
		return nom;
	}

	public void setNom(String nom) {
		this.nom = nom;
	}
</pre>
<p>Pour instancier cet objet utilisons l&rsquo;outillage <strong>java.lang.reflect.*</strong></p>
<pre class="brush: java; title: ; notranslate">
public class Genericite {

	public static &lt;T&gt; T instanceObject(Class&lt;T&gt; clazz) throws InstantiationException, IllegalAccessException, SecurityException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException{
		Constructor&lt;T&gt; defaultConstructor =  clazz.getDeclaredConstructor();
		defaultConstructor.setAccessible(true);
		T t = defaultConstructor.newInstance();
		return t;
	}
</pre>
<p>le setAccessible(true) nous permet en quelque sorte de &laquo;&nbsp;hacker&nbsp;&raquo; cette classe et changer la visibilité d&rsquo;un constructeur.</p>
<pre class="brush: java; title: ; notranslate">
	public static void main(String[] args) throws InstantiationException, IllegalAccessException, SecurityException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException {
		Personne p = Genericite.instanceObject(Personne.class);
		p.setNom(&quot;Isma&quot;);
		System.out.println(p.getNom());
	}
</pre>
<p>Ce mécanisme est très utilisé dans les framework tel que <strong>Spring</strong>.</p>
]]></content:encoded>
			<wfw:commentRss>https://cadjee.fr/reflexion-genericite/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
