<?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>FLiFnotes &#187; java</title>
	<atom:link href="http://www.flifnotes.com/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.flifnotes.com</link>
	<description></description>
	<lastBuildDate>Mon, 06 Sep 2010 16:12:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Using Reflection in Java</title>
		<link>http://www.flifnotes.com/37-using-reflection-in-java/</link>
		<comments>http://www.flifnotes.com/37-using-reflection-in-java/#comments</comments>
		<pubDate>Sat, 23 Jan 2010 18:15:50 +0000</pubDate>
		<dc:creator>Deborah Hawkins</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[reflection]]></category>

		<guid isPermaLink="false">http://www.flifnotes.com/?p=37</guid>
		<description><![CDATA[Last semester, my two main programming courses were CS 315 and CS 310 where we learned to use Lisp and LC-3 assembly code respectively. In both, programs and data were of the same format, so modifying a program was as simple as modifying data. In Java, however, it&#8217;s a bit more awkward. Sure, you can [...]]]></description>
			<content:encoded><![CDATA[<p>Last semester, my two main programming courses were CS 315 and CS 310 where we learned to use Lisp and LC-3 assembly code respectively. In both, programs and data were of the same format, so modifying a program was as simple as modifying data. In Java, however, it&#8217;s a bit more awkward. Sure, you can treat a .java file as text and modify it, but what if you only have the .class file? Or what if you just want an easier way? Fortunately, Java provides a solution called <a href="http://java.sun.com/developer/technicalArticles/ALT/Reflection/" title="Using Java Reflection">reflection</a>. </p>
<p>In our <a href="http://www.cs.utexas.edu/users/dsb/CS378/" title="CS 378">Software Design</a> course, our first assignment of the semester is to use reflection to create Java programs called adaptors. I&#8217;ve just started with the assignment by playing around with the Java reflection library and figuring out how to use these tools, but I thought I&#8217;d share a simple example which may be much quicker to understand than the article linked to above.</p>
<pre><code>import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class Main {
    public static void main(String[] args) { 

        // select the class to look into
        Class myclass = String.class;

        // check how many methods the class declares
        Method[] methods = myclass.getDeclaredMethods();
        System.out.println( "Number of methods in "
                + myclass.getName() + " class: " + methods.length + "\n" );

        // select a method from the class
        int methodNum = 52;
        Method mymethod = methods[methodNum];
        System.out.println( "Checking method #" + methodNum + ":" );

        // name of method
        System.out.println( "Name: " + mymethod.getName() );

        // modifiers such as protected or static
        // note: getModifiers() returns an int that you need the Modifier
        //       class to makes sense of
        System.out.println( "Modifiers: "
                + Modifier.toString(mymethod.getModifiers()) );

        // return type of method
        System.out.println( "Return Type: " + mymethod.getReturnType().getName() );

        // list of method's parameters
        Class<?>[] mytypes = mymethod.getParameterTypes();
        for (Class c: mytypes)
            System.out.println( "Parameter: " + c.getName() );
    }
}</code></pre>
<p>The output for this program is:</p>
<pre><code>Number of methods in java.lang.String class: 68

Checking method #52:
Name: substring
Modifiers: public
Return Type: java.lang.String
Parameter: int
Parameter: int</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.flifnotes.com/37-using-reflection-in-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
