Lab 7 class layout tool in Java

Lab 7

Develop a class layout tool like the Java's built-in executable file “javap” to show the skeleton of a .class file. The tool can display the fields, methods, exceptions and constructors of a given .class file without source code. The following gives an example. After compiling the following NewClass.java file on the left, the byte code NewClass.class is rendered. Your developed tool can accept NewClass.class as input to show the skeleton on the right. Feel free to run “javap ClassName” to verify accuracy. Ensure other .class files can be processed without an error too. Note that constructors and methods that throw exceptions should also be displayed. This tool will be the building block for the next tool that you are going to build. (6 pts)

 

// This is the original .java code.

public class NewClass {

    int w, h, d;

    public static void main(String[] args){

    }

    public void m1(int i, int j, int k){

    }

    public int m2(int i){

        return 0;

    }

    protected Integer m3(double d){

        return null;

    }

} // This is the skeleton.

public class NewClass {

  int w;

  int h;

  int d;

  public NewClass();

  public static void main(java.lang.String[] sa);

  public void m1(int i1, int i2, int i3);

  public int m2(int i);

  protected java.lang.Integer m3(double d);

}

 

 

 

 

 

Hint: run the following sample code to get an idea about how to access information from a Class object. Note that “Lab7.Rectangle” will load in the Rectangle.class not the Rectangle.java file.

 

package Lab7;

public class Rectangle {

    int w, h;

    public Rectangle(){    }

    public Rectangle(int w, int h){

        this.w = w;

        this.h = h;

    }

    public int getArea(){

        return w * h;

    }

}

package Lab7;

import java.lang.reflect.Constructor;

public class MyProg {

    public static void main(String[] args) throws ClassNotFoundException{

        Class c = Class.forName("Lab7.Rectangle");

        Constructor[] ca = c.getConstructors();

        for(Constructor cons: ca){

            System.out.println(cons);

        }

    }

}

 

 

Need a custom answer at your budget?

This assignment has been answered 6 times in private sessions.

© 2024 Codify Tutor. All rights reserved