Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

DesertFox

macrumors member
Original poster
Nov 25, 2004
72
0
Hello,

Im taking an object orientated design class and the professor has given an assignment in which we have to build a "code analyzer". Its suppose to take a .java package and look inside and count the number of classes and see how the methods are hooked up. I have no clue even where to begin. Are there some special methods that spider a package for classes/methods?

The program needs to print out:
Depth of methods/class
instability
responsibility
workload

But in order to get these, I need get data on the methods.

Can someone with java skills help me out here? Maybe someone could post some code to start with?
 

mufflon

macrumors 6502
Sep 15, 2006
264
2
the java standard library is all you need for this, really - from a cursory glance of "Class" there's some methods you could use:
java std library: Class

the method "getMethods()" will return an array of methods, all with declared parameters and names.

the method class got getName() and getParameterTypes() (and classes got getName()) - so you're all set =)

I would propose that any consequent reading would be in the java standard library or your assignment / course litterature - if you're told everything then what will you actually have learned from this endevour?
 

MarkCollette

macrumors 68000
Mar 6, 2003
1,559
36
Toronto, Canada
Do you mean look inside a .jar package?

Source file: .java
Compiled file: .class
Zip file holding all the .class files and sometimes the .java files too: .jar
 

DesertFox

macrumors member
Original poster
Nov 25, 2004
72
0
Thanks for the replies so far guys. I've been going through some pages on reflection an now Im kinda stuck.

Our professor wants us to invoke our program from the command line, like so: "java MethodDump c:\\projects\\demo", where demo is the name of package that is to be analyzed.

I've been trying to get this code to load the demo package but with no luck.
When I do "java MethodDump.java /Users/john/Documents/Demo.java", but that throws an error "java.lang.ClassNotFoundException"

Code:
import java.lang.reflect.*;
 
   public class DumpMethods {
      public static void main(String args[])
      {
         try {
            Class c = Class.forName(args[0]);
            Method m[] = c.getDeclaredMethods();
            for (int i = 0; i < m.length; i++)
            System.out.println(m[i].toString());
         }
         catch (Throwable e) {
            System.err.println(e);
         }
      }
   }

Does someone know how i can get the demo package into this program?

Also, here is the demo java file that Im trying to analyze:
Code:
package demo;


class A {
 public void meth1() {
  System.out.println("calling " + getClass() + ".meth1");
 }

 public void meth2() {
  System.out.println("calling " + getClass() + ".meth2");
 }
}

class B extends A {
 public void meth1() {
  System.out.println("calling " + getClass() + ".meth1");
 }

 public void meth2() {
  System.out.println("calling " + getClass() + ".meth2");
 }
}

class C extends B {
 public void meth1() {
  System.out.println("calling " + getClass() + ".meth1");
 }

 public void meth2() {
  System.out.println("calling " + getClass() + ".meth2");
 }

 public void meth3() {
  System.out.println("calling " + getClass() + ".meth3");
 }

 public void meth4() {
  System.out.println("calling " + getClass() + ".meth4");
 }
}

class D {
 private B b = new B();
 public void meth1() {
  System.out.println("calling " + getClass() + ".meth1");
 }
}

class E {
 public void meth1(D d, A a) {
  System.out.println("calling " + getClass() + ".meth1");
 }

 public void meth2() {
  System.out.println("calling " + getClass() + ".meth2");
 }
}

class F {
 private D d = new D();
 public void meth1(E e) {
  System.out.println("calling " + getClass() + ".meth1");
 }

 public void meth2() {
  System.out.println("calling " + getClass() + ".meth2");
 }
}


public class Demo {

 public static void main(String[] args) {
  A a = new A();
  a.meth1();
  a.meth2();
  B b = new B();
  b.meth1();
  b.meth2();
  // etc.
 }
}

To make it a bit more clear, here is the output, when passing the demo package to the analyzer:

Code:
C     inDepth(C)     instability(C) responsibility(C)     workload(C)

A        1              0                 .28                 .14
B        2              .14               .28                 .14
C        3              .14               0                   .28
D        1              .14               .28                 .07
E        1              .28               .14                 .14
F        1              .28               0                   .14
Demo     1              0                 0                   .07

Hopefully someone could help me a little further here.

Thanks in advance!
 

psingh01

macrumors 68000
Apr 19, 2004
1,586
629
Thanks for the replies so far guys. I've been going through some pages on reflection an now Im kinda stuck.

Our professor wants us to invoke our program from the command line, like so: "java MethodDump c:\\projects\\demo", where demo is the name of package that is to be analyzed.

I've been trying to get this code to load the demo package but with no luck.
When I do "java MethodDump.java /Users/john/Documents/Demo.java", but that throws an error "java.lang.ClassNotFoundException"

Thanks in advance!

You have to compile your code first:

javac MethodDump.java

This will compile MethodDump.java and create MethodDump.class (assuming no errors are found).

Then you call it with:

java MethodDump "c:\projects\demo"

This runs the MethodDump.class program (but you don't put .class when you call it). The "c:\projects\demo" part will be passed in as an argument.

Now the reflection method works if you have your classes compiled and are running code with them. Is that what your assignment is? Or do you have to actually read the source code (i.e. as text) and analyze that? TBH that maybe harder and probably not a good assignment for a noob :)
 

DesertFox

macrumors member
Original poster
Nov 25, 2004
72
0
You have to compile your code first:
Then you call it with:

java MethodDump "c:\projects\demo"

This runs the MethodDump.class program (but you don't put .class when you call it). The "c:\projects\demo" part will be passed in as an argument.

Now the reflection method works if you have your classes compiled and are running code with them. Is that what your assignment is? Or do you have to actually read the source code (i.e. as text) and analyze that? TBH that maybe harder and probably not a good assignment for a noob :)

Hmm, the program compiles, but when i run the java MethodDump "/Users/john/Documents/Demo" I get the error: "java.lang.ClassNotFoundException"

My ultimate goal for this project is to figure out which classes are referencing each other, so I need to get some general data about the classes in the Demo file...so that I get the data that I posted in my last post...
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.