Created
October 16, 2012 22:26
-
-
Save fikovnik/3902457 to your computer and use it in GitHub Desktop.
ClassAnalyzer template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tp2; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
public class ClassAnalyzer { | |
public static void analyzeClass(String className) | |
throws ClassNotFoundException { | |
Class<?> cl = loadClass(className); | |
printClassDeclaration(cl); | |
System.out.println(" {"); | |
System.out.println(); | |
printFields(cl); | |
System.out.println(); | |
printConstructors(cl); | |
System.out.println(); | |
printMethods(cl); | |
System.out.println("}"); | |
} | |
/** | |
* Loads a class {@code className} from a classpath. | |
* | |
* @param className | |
* - name of a class | |
* @return a class object representing the given class | |
* @throws ClassNotFoundException | |
* in case no such class could have been loaded | |
*/ | |
public static Class<?> loadClass(String nomClasse) | |
throws ClassNotFoundException { | |
// TODO: finish | |
return null; | |
} | |
/** | |
* Outputs a class declaration. | |
* | |
* For example for class: {@code java.util.ArrayList} it will output: | |
* | |
* <pre> | |
* public class ArrayList extends AbstractList implements List, RandomAccess, Cloneable, Serializable | |
* </pre> | |
*/ | |
public static void printClassDeclaration(Class<?> cl) { | |
// TODO: print modifiers (look at java.lang.reflect.Modifier) | |
// TODO: print out class name | |
System.out.print(" class "); | |
// TODO: print out super class (if any, java.lang.Object is implicit) | |
System.out.print(" extends "); | |
// TODO: print out all implemented interfaces (if any) | |
System.out.print(" implements "); | |
} | |
/** | |
* Outputs class' constructors. | |
* | |
* For example for class: {@code java.util.ArrayList} it will output: | |
* | |
* <pre> | |
* public ArrayList(int initialCapacity) | |
* public ArrayList() | |
* public ArrayList(Collection c) | |
* </pre> | |
*/ | |
public static void printConstructors(Class<?> cl) { | |
// TODO: print all class constructors | |
} | |
/** | |
* Outputs class' fields. | |
* | |
* For example for class: {@code java.util.ArrayList} it will output: | |
* | |
* <pre> | |
* private static final long serialVersionUID; | |
* private transient Object[] elementData; | |
* private int size; | |
* ... | |
* </pre> | |
*/ | |
public static void printFields(Class<?> cl) { | |
// TODO: print all class fields | |
} | |
/** | |
* Outputs class' methods. | |
* | |
* For example for class: {@code java.util.ArrayList} it will output: | |
* | |
* <pre> | |
* public int size(); | |
* public boolean isEmpty(); | |
* public boolean contains(Object o); | |
* public int indexOf(Object o); | |
* ... | |
* </pre> | |
*/ | |
public static void printMethods(Class<?> cl) { | |
// TODO: print all class methods | |
} | |
public static void main(String[] args) { | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
String input = null; | |
while (true) { | |
try { | |
System.out | |
.print("Enter a fully qualified class name (ex : java.util.ArrayList): "); | |
input = br.readLine(); | |
if (input == null || "".equals(input)) { | |
break; | |
} | |
analyzeClass(input); | |
System.out.println(); | |
} catch (ClassNotFoundException e) { | |
System.out.println("Ups, class does not seem to exist in the current classpath"); | |
} catch (IOException e) { | |
System.out.println("I/O error"); | |
e.printStackTrace(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment