Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » Java » Beginners » Downloads
 
Add your own
alternative version

Java Class Viewer

, 25 Apr 2014
Watch the Java class file visually & interactively for the meaning of every byte
articlecodedemo.src.zip
ArticleCodeDemo.src
ArticleCodeDemo
manifest.mf
dist
ArticleCodeDemo.jar
lib
jCFL-2.0.161.jar
build
classes
org
freeinternals
demo
jCFL_CodeDemo.class
run
test
src
org
freeinternals
demo
nbproject
project.properties
genfiles.properties
private
private.properties
config.properties
execuableapp-noexe.zip
ExecuableApp.zip
Readme
Readme.pdf
TheApp
JavaClassViewer.jar
lib
CommonLib.jar
FormatCLASS.jar
javaclassviewer.binary.zip
JavaClassViewer.binary
JavaClassViewer.jar
lib
jCFL-2.0.161.jar
javaclassviewer.src.zip
JavaClassViewer
manifest.mf
src
org
freeinternals
javaclassviewer
.svn
format
entries
tmp
props
prop-base
text-base
props
prop-base
text-base
JDialogAbout.java.svn-base
Main.java.svn-base
JFrameClassFile.java.svn-base
.svn
format
entries
tmp
props
prop-base
text-base
props
prop-base
text-base
.svn
format
entries
tmp
props
prop-base
text-base
props
prop-base
text-base
.svn
format
entries
tmp
props
prop-base
text-base
props
prop-base
text-base
nbproject
project.properties
genfiles.properties
private
config.properties
private.properties
.svn
format
entries
tmp
props
prop-base
text-base
props
prop-base
text-base
config.properties.svn-base
private.properties.svn-base
private.xml.svn-base
.svn
format
entries
tmp
props
prop-base
text-base
props
prop-base
text-base
build-impl.xml.svn-base
genfiles.properties.svn-base
project.properties.svn-base
project.xml.svn-base
test
.svn
format
entries
tmp
props
prop-base
text-base
props
prop-base
text-base
.svn
format
entries
tmp
props
prop-base
text-base
props
prop-base
text-base
build.xml.svn-base
manifest.mf.svn-base
Source.zip
SourceCode
CommonLib
nbproject
genfiles.properties
private
private.properties
project.properties
src
org
freeinternals
biv
plugin
commonlib
core
ui
binviewer
jtreetable
xmltree
util
format
FormatCLASS
nbproject
genfiles.properties
private
private.properties
project.properties
src
org
freeinternals
format
classfile
test
JavaClassViewer
manifest.mf
nbproject
genfiles.properties
private
private.properties
project.properties
src
org
freeinternals
javaclassviewer
ui
test
/*
 * Main.java    Created on Apr 27, 2009
 *
 * Copyright 2009, FreeInternals.org. All rights reserved.
 * Use is subject to license terms.
 */
package org.freeinternals.demo;

import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.jar.JarFile;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.freeinternals.classfile.core.AbstractCPInfo;
import org.freeinternals.classfile.core.AccessFlags;
import org.freeinternals.classfile.core.AttributeCount;
import org.freeinternals.classfile.core.AttributeInfo;
import org.freeinternals.classfile.core.CPCount;
import org.freeinternals.classfile.core.ClassFile;
import org.freeinternals.classfile.core.ClassFormatException;
import org.freeinternals.classfile.core.FieldCount;
import org.freeinternals.classfile.core.FieldInfo;
import org.freeinternals.classfile.core.Interface;
import org.freeinternals.classfile.core.InterfaceCount;
import org.freeinternals.classfile.core.MajorVersion;
import org.freeinternals.classfile.core.MethodCount;
import org.freeinternals.classfile.core.MethodInfo;
import org.freeinternals.classfile.core.MinorVersion;
import org.freeinternals.classfile.core.SuperClass;
import org.freeinternals.classfile.core.ThisClass;
import org.freeinternals.classfile.ui.Tool;

/**
 *
 * @author Amos Shi
 */
public class jCFL_CodeDemo {

    /**
     * Before running this application, please ensure the .class file and .jar
     * file exist in the specified location.
     *
     * @param args command line arguments
     */
    public static void main(final String[] args) {
        try {
            jCFL_CodeDemo.extractClassFile();
            jCFL_CodeDemo.extractJarFile();
        } catch (IOException ex) {
            Logger.getLogger(jCFL_CodeDemo.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println(ex.toString());
        } catch (ClassFormatException ex) {
            Logger.getLogger(jCFL_CodeDemo.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println(ex.toString());
        }
    }

    private static void extractClassFile() throws IOException, ClassFormatException {

        File file = new File("C:/Temp/File.class");
        byte[] classByteArray = Tool.readClassFile(file);
        ClassFile classfile = new ClassFile(classByteArray);

        jCFL_CodeDemo.printClassFile(classfile);
    }

    private static void extractJarFile() throws IOException, ClassFormatException {
        File file = new File("C:/Temp/tools.jar");
        JarFile jarFile = new JarFile(file, false, JarFile.OPEN_READ);
        ZipFile zipFile = jarFile;

        final Enumeration zipEntries = zipFile.entries();
        while (zipEntries.hasMoreElements()) {
            ZipEntry zipEntry = (ZipEntry) zipEntries.nextElement();
            if (!zipEntry.getName().endsWith(".class")) {
                continue;
            }

            byte[] classByteArray = Tool.readClassFile(zipFile, zipEntry);
            ClassFile classfile = new ClassFile(classByteArray);

            System.out.println();
            System.out.println(zipEntry.getName());
            jCFL_CodeDemo.printClassFile(classfile);
        }
    }

    private static void printClassFile(ClassFile classfile) {
        // Minor & Major version
        MinorVersion minorVersion = classfile.getMinorVersion();
        System.out.println("Class File Minor Version: " + minorVersion.getValue());

        MajorVersion majorVersion = classfile.getMajorVersion();
        System.out.println("Class File Major Version: " + majorVersion.getValue());

        // Constant Pool
        CPCount cpCount = classfile.getCPCount();
        System.out.println("Constant Pool size: " + cpCount.getValue());

        AbstractCPInfo[] cpArray = classfile.getConstantPool();
        for (int i = 1; i < cpCount.getValue(); i++) {
            System.out.println(
                    String.format("Constant Pool [%d]: %s", i, classfile.getCPDescription(i)));
            short tag = cpArray[i].getTag();
            if ((tag == AbstractCPInfo.CONSTANT_Double) || (tag == AbstractCPInfo.CONSTANT_Long)) {
                i++;
            }
        }

        // Access flag, this & super class
        AccessFlags accessFlags = classfile.getAccessFlags();
        System.out.println("Class Modifier: " + accessFlags.getModifiers());

        ThisClass thisClass = classfile.getThisClass();
        System.out.println("This Class Name Index: " + thisClass.getValue());
        System.out.println("This Class Name: " + classfile.getCPDescription(thisClass.getValue()));

        SuperClass superClass = classfile.getSuperClass();
        System.out.println("Super Class Name Index: " + superClass.getValue());
        if (superClass.getValue() == 0) {
            System.out.println("Super Class Name: java.lang.Object");
        } else {
            System.out.println("Super Class Name: " + classfile.getCPDescription(superClass.getValue()));
        }

        // Interfaces
        InterfaceCount interfactCount = classfile.getInterfacesCount();
        System.out.println("Interface Count: " + interfactCount.getValue());

        if (interfactCount.getValue() > 0) {
            Interface[] interfaceArray = classfile.getInterfaces();
            for (int i = 0; i < interfaceArray.length; i++) {
                System.out.println(
                        String.format("Interface [%d] Name Index: %d", i, interfaceArray[i].getValue()));
                System.out.println(
                        String.format("Interface [%d] Name: %s", i, classfile.getCPDescription(interfaceArray[i].getValue())));
            }
        }

        // Fields
        FieldCount fieldCount = classfile.getFieldCount();
        System.out.println("Field count: " + fieldCount.getValue());

        if (fieldCount.getValue() > 0) {
            FieldInfo[] fieldArray = classfile.getFields();
            for (int i = 0; i < fieldArray.length; i++) {
                System.out.println(String.format("Field [%d]: %s", i, fieldArray[i].getDeclaration()));
            }
        }

        // Methods
        MethodCount methodCount = classfile.getMethodCount();
        System.out.println("Method count: " + methodCount.getValue());

        if (methodCount.getValue() > 0) {
            MethodInfo[] methodArray = classfile.getMethods();
            for (int i = 0; i < methodArray.length; i++) {
                System.out.println(String.format("Method [%d]: %s", i, methodArray[i].getDeclaration()));
            }
        }

        // Attributes
        AttributeCount attributeCount = classfile.getAttributeCount();
        System.out.println("Attribute count: " + attributeCount.getValue());

        AttributeInfo[] attributeArray = classfile.getAttributes();
        for (int i = 0; i < attributeArray.length; i++) {
            System.out.println(String.format("Attribute [%d]: %s", i, attributeArray[i].getName()));
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License

About the Author

AmosShi
Software Developer (Senior)
United States United States
Deliver useful software to the world.

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 25 Apr 2014
Article Copyright 2009 by AmosShi
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid