Java parse class files

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

a wrapper of ‘javap’ to read java class info from ‘.class’ file

License

villadora/java-class-parser

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Читайте также:  Type case in php

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

A simple lib wrapped javap to parse java .class files into json format. Only parse public members by default.

$ npm install java-class-parser --save [-g]
$ jjs-parser ./Model.class Service.class
var parser = require('java-class-parser'); var clazzes = ['./Config.class']; parser.parse(clazzes, function(err, rs)  for (var clzName in rs)  var clz = rs[clzName]; clz.constructors; // constructors, each constructor includes property scope, name, args clz.fields; // an array of fields clz.methods; // an array of methods; clz.methods[0]; // method, includes property scope, ret, name, args > >);

parser(clazzesPaths, [options], callback);

  • clazzesPaths Array Paths of classes
  • options Object current only =args= and =outputParser= is available
  • callback function

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

A Java class file parser based on Java Visual Machine Specification,just like javap.

jordonyang/ClassParser

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

A Java class file parser based on Java Visual Machine Specification,just like javap which is an inclusive powerful tool of Java Developer’s Kit to parser byte code.

source file of the test case

package decompile; import java.util.Iterator; public class IterableClass implements IterableString>< private String[] arr; public IterableClass(String[] arr) < this.arr = arr; > @Override public IteratorString> iterator() < return new IteratorString>() < private int index = 0; @Override public boolean hasNext() < return index < arr.length; > @Override public String next() < return arr[index++]; > @Override public void remove() < //TODO > >; > >

hexadecimal data of the class file

CA, FE, BA, BE, 00, 00, 00, 34, 00, 25, 09, 00, 05, 00, 1C, 0A 00, 06, 00, 1D, 07, 00, 1E, 0A, 00, 03, 00, 1F, 07, 00, 20, 07 00, 21, 07, 00, 22, 01, 00, 0C, 49, 6E, 6E, 65, 72, 43, 6C, 61 73, 73, 65, 73, 01, 00, 03, 61, 72, 72, 01, 00, 13, 5B, 4C, 6A 61, 76, 61, 2F, 6C, 61, 6E, 67, 2F, 53, 74, 72, 69, 6E, 67, 3B 01, 00, 06, 3C, 69, 6E, 69, 74, 3E, 01, 00, 16, 28, 5B, 4C, 6A 61, 76, 61, 2F, 6C, 61, 6E, 67, 2F, 53, 74, 72, 69, 6E, 67, 3B 29, 56, 01, 00, 04, 43, 6F, 64, 65, 01, 00, 0F, 4C, 69, 6E, 65 4E, 75, 6D, 62, 65, 72, 54, 61, 62, 6C, 65, 01, 00, 12, 4C, 6F 63, 61, 6C, 56, 61, 72, 69, 61, 62, 6C, 65, 54, 61, 62, 6C, 65 01, 00, 04, 74, 68, 69, 73, 01, 00, 19, 4C, 64, 65, 63, 6F, 6D 70, 69, 6C, 65, 2F, 49, 74, 65, 72, 61, 62, 6C, 65, 43, 6C, 61 73, 73, 3B, 01, 00, 08, 69, 74, 65, 72, 61, 74, 6F, 72, 01, 00 16, 28, 29, 4C, 6A, 61, 76, 61, 2F, 75, 74, 69, 6C, 2F, 49, 74 65, 72, 61, 74, 6F, 72, 3B, 01, 00, 09, 53, 69, 67, 6E, 61, 74 75, 72, 65, 01, 00, 2A, 28, 29, 4C, 6A, 61, 76, 61, 2F, 75, 74 69, 6C, 2F, 49, 74, 65, 72, 61, 74, 6F, 72, 3C, 4C, 6A, 61, 76 61, 2F, 6C, 61, 6E, 67, 2F, 53, 74, 72, 69, 6E, 67, 3B, 3E, 3B 01, 00, 0A, 61, 63, 63, 65, 73, 73, 24, 30, 30, 30, 01, 00, 2E 28, 4C, 64, 65, 63, 6F, 6D, 70, 69, 6C, 65, 2F, 49, 74, 65, 72 61, 62, 6C, 65, 43, 6C, 61, 73, 73, 3B, 29, 5B, 4C, 6A, 61, 76 61, 2F, 6C, 61, 6E, 67, 2F, 53, 74, 72, 69, 6E, 67, 3B, 01, 00 02, 78, 30, 01, 00, 3A, 4C, 6A, 61, 76, 61, 2F, 6C, 61, 6E, 67 2F, 4F, 62, 6A, 65, 63, 74, 3B, 4C, 6A, 61, 76, 61, 2F, 6C, 61 6E, 67, 2F, 49, 74, 65, 72, 61, 62, 6C, 65, 3C, 4C, 6A, 61, 76 61, 2F, 6C, 61, 6E, 67, 2F, 53, 74, 72, 69, 6E, 67, 3B, 3E, 3B 01, 00, 0A, 53, 6F, 75, 72, 63, 65, 46, 69, 6C, 65, 01, 00, 12 49, 74, 65, 72, 61, 62, 6C, 65, 43, 6C, 61, 73, 73, 2E, 6A, 61 76, 61, 0C, 00, 09, 00, 0A, 0C, 00, 0B, 00, 23, 01, 00, 19, 64 65, 63, 6F, 6D, 70, 69, 6C, 65, 2F, 49, 74, 65, 72, 61, 62, 6C 65, 43, 6C, 61, 73, 73, 24, 31, 0C, 00, 0B, 00, 24, 01, 00, 17 64, 65, 63, 6F, 6D, 70, 69, 6C, 65, 2F, 49, 74, 65, 72, 61, 62 6C, 65, 43, 6C, 61, 73, 73, 01, 00, 10, 6A, 61, 76, 61, 2F, 6C 61, 6E, 67, 2F, 4F, 62, 6A, 65, 63, 74, 01, 00, 12, 6A, 61, 76 61, 2F, 6C, 61, 6E, 67, 2F, 49, 74, 65, 72, 61, 62, 6C, 65, 01 00, 03, 28, 29, 56, 01, 00, 1C, 28, 4C, 64, 65, 63, 6F, 6D, 70 69, 6C, 65, 2F, 49, 74, 65, 72, 61, 62, 6C, 65, 43, 6C, 61, 73 73, 3B, 29, 56, 00, 21, 00, 05, 00, 06, 00, 01, 00, 07, 00, 01 00, 02, 00, 09, 00, 0A, 00, 00, 00, 03, 00, 01, 00, 0B, 00, 0C 00, 01, 00, 0D, 00, 00, 00, 46, 00, 02, 00, 02, 00, 00, 00, 0A 2A, B7, 00, 02, 2A, 2B, B5, 00, 01, B1, 00, 00, 00, 02, 00, 0E 00, 00, 00, 0E, 00, 03, 00, 00, 00, 09, 00, 04, 00, 0A, 00, 09 00, 0B, 00, 0F, 00, 00, 00, 16, 00, 02, 00, 00, 00, 0A, 00, 10 00, 11, 00, 00, 00, 00, 00, 0A, 00, 09, 00, 0A, 00, 01, 00, 01 00, 12, 00, 13, 00, 02, 00, 0D, 00, 00, 00, 33, 00, 03, 00, 01 00, 00, 00, 09, BB, 00, 03, 59, 2A, B7, 00, 04, B0, 00, 00, 00 02, 00, 0E, 00, 00, 00, 06, 00, 01, 00, 00, 00, 0F, 00, 0F, 00 00, 00, 0C, 00, 01, 00, 00, 00, 09, 00, 10, 00, 11, 00, 00, 00 14, 00, 00, 00, 02, 00, 15, 10, 08, 00, 16, 00, 17, 00, 01, 00 0D, 00, 00, 00, 2F, 00, 01, 00, 01, 00, 00, 00, 05, 2A, B4, 00 01, B0, 00, 00, 00, 02, 00, 0E, 00, 00, 00, 06, 00, 01, 00, 00 00, 05, 00, 0F, 00, 00, 00, 0C, 00, 01, 00, 00, 00, 05, 00, 18 00, 11, 00, 00, 00, 03, 00, 14, 00, 00, 00, 02, 00, 19, 00, 1A 00, 00, 00, 02, 00, 1B, 00, 08, 00, 00, 00, 0A, 00, 01, 00, 03 00, 00, 00, 00, 00, 00 

analytical result of the constant pool

 #1 = Filedref #5, #28 // decompile/IterableClass, arr:[Ljava/lang/String; #2 = Methodref #6, #29 // java/lang/Object, :()V #3 = Class #30 // decompile/IterableClass$1 #4 = Methodref #3, #31 // decompile/IterableClass$1, :(Ldecompile/IterableClass;)V #5 = Class #32 // decompile/IterableClass #6 = Class #33 // java/lang/Object #7 = Class #34 // java/lang/Iterable #8 = Utf8 InnerClasses #9 = Utf8 arr #10 = Utf8 [Ljava/lang/String; #11 = Utf8 #12 = Utf8 ([Ljava/lang/String;)V #13 = Utf8 Code #14 = Utf8 LineNumberTable #15 = Utf8 LocalVariableTable #16 = Utf8 this #17 = Utf8 Ldecompile/IterableClass; #18 = Utf8 iterator #19 = Utf8 ()Ljava/util/Iterator; #20 = Utf8 Signature #21 = Utf8 ()Ljava/util/Iterator; #22 = Utf8 access$000 #23 = Utf8 (Ldecompile/IterableClass;)[Ljava/lang/String; #24 = Utf8 x0 #25 = Utf8 Ljava/lang/Object;Ljava/lang/Iterable; #26 = Utf8 SourceFile #27 = Utf8 IterableClass.java #28 = NameAndType #9, #10 // arr:[Ljava/lang/String; #29 = NameAndType #11, #35 // :()V #30 = Utf8 decompile/IterableClass$1 #31 = NameAndType #11, #36 // :(Ldecompile/IterableClass;)V #32 = Utf8 decompile/IterableClass #33 = Utf8 java/lang/Object #34 = Utf8 java/lang/Iterable #35 = Utf8 ()V #36 = Utf8 (Ldecompile/IterableClass;)V 

About

A Java class file parser based on Java Visual Machine Specification,just like javap.

Источник

Beyond Java

If you want to parse a java class, it is a clever idea to fetch a java grammar and let JavaCC do the work for you. However, I didn’t find a grammar for Java 7, so I decided to write my own parser. This parser can also read Groovy source code and most of the Java 8 source code (apart from defender methods).

Another reason to write the parser was to show how simple file and text manipulations can be. Groovy is optimized for programming these tasks (at the cost of being a little slower). The parser consists of merely 263 lines, including a couple of comments and blank lines. I guess the Java version is a lot more verbose.

License

Feel free to use the source code if you need it. Please note that I provide it to you on a «as-is» basis: if you use the source code, you use it at your own risk. You can download it here.

The regular expressions

The most important file of the project contains the regular expressions I use to extract classes, variable, methods, assignments and annotations:

The main class

The class definition

This class is the definition of a single class.

package de.beyondjava.VariableParser; import java.util.List; import java.util.regex.Matcher import java.util.regex.Pattern; import static RegularExpressions.* /** * Description of a class, including a list of its variables and methods (but ignoring the parameter lists of the methods) */ public class ClassDefinition < String packageName; String className; List variables = [] List methods = [] List modifiers=[]; List annotations=[]; /** * extract the variables and methods from the implementation */ ClassDefinition (String p_packageName, String p_classDeclaration, String p_impl) < Matcher m = p_classDeclaration =~ ANNOTATION m.each() // simplify expression by removing annotations p_classDeclaration = p_classDeclaration.replaceAll(ANNOTATION,"") List words = p_classDeclaration.split("class"); words = words.findAll className = words[-1].trim() if (words.size()>1) < modifiers = words. [3] collect > packageName=p_packageName def implementation=removeImplementationDetails(p_impl) extractVariablesAndMethods(implementation) > /** * In order to keep things simple, we remove the implementation of the methods. */ public String removeImplementationDetails(String p_theSourcecode) < // This regular expression removes everything enclosed between curly braces p_theSourcecode.replaceAll(JAVA_BLOCK_REGEXP,"\n") >private void extractVariablesAndMethods(String p_impl) < Matcher m = p_impl =~ VARIABLE_OR_METHOD_REGEXP def meths = m.findAll.collect methods = meths.collect < new MethodDefinition(it)>def vars = m.findAll.collect variables = vars.collect < new VariableDefinition(it)>> >

package de.beyondjava.VariableParser; import static RegularExpressions.* import java.util.List; import java.util.regex.Matcher /** * This is the definition of a method (without the implementation details and without the parameter list). */ public class MethodDefinition < List visibility = [] String name String type List annotations=[] String parameters boolean isConstructor MethodDefinition(String p_method) < Matcher m = p_method =~ ANNOTATION m.each() // simplify expression by removing annotations p_method = p_method.replaceAll(ANNOTATION, «») // extract parameter list m = p_method =~ PARAMETERS m.each() // simplify expression by removing parameters p_method = p_method.replaceAll(PARAMETERS, «») String[] words = p_method.split(NON_EMPTY_SPACE.toString()) name=words[-1] if (words.size()==1 || words[-2] == «public» || words[-2] == «private» || words[-2] == «protected») < isConstructor = true if (words.length >1) < visibility=words [4] > > else < type=words[-2] if (words.length >2) < visibility=words [5] > > > >

The description of a variable

. is almost identical to the description of a method:

package de.beyondjava.VariableParser; import java.util.regex.Matcher import java.util.regex.Pattern; import static RegularExpressions.* /** * This a the definition of a variable. */ class VariableDefinition < List modifiers=[]; String name; String type; List annotations=[]; String value=null; // optional /** Receives the complete variable definition source code and extracts the name, * the type and the visibility. * @param p_variable variable definition (without assignment) */ VariableDefinition(String p_variable) < Matcher m = p_variable =~ ANNOTATION m.each() // simplify expression by removing annotations p_variable = p_variable.replaceAll(ANNOTATION,"") // and assignments m = p_variable =~ ASSIGNMENT m.each() p_variable = p_variable.replaceAll(ASSIGNMENT,"") String[] words = p_variable.split(NON_EMPTY_SPACE.toString()) name=words[-1] type=words[-2] if (words.length > 2) < modifiers=words [6] > > >

The JUnit test and example classes

package de.beyondjava.VariableParser; import java.util.List; import de.beyondjava.Beans.Address import groovy.util.GroovyTestCase; class VariableParserTest extends GroovyTestCase < public void testParser() < SimpleClassParser parser = new SimpleClassParser() List classes = parser.parse("src/de/beyondjava/Beans") assertEquals(2, classes.size()) int vars=0 int meths=0 classes.each(println "$ contains $ variables and $ methods" vars += c.variables?.size() meths += c.methods?.size() >) assertEquals(7, vars) assertEquals(4, meths) > > package de.beyondjava.Beans; import javax.faces.bean.ManagedBean; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; @ManagedBean public class Address < @Size(max=40) String street = "< lorem ipsum >«; @NotNull @Size(min=1, max=40) public String city= null; private int zipCode = 64546; /** * This is the default constructor. */ public Address() < street = "unknown"; city = "in the middle of nowhere"; zipCode = 0; >protected void initRandomly() < zipCode = (int)( System.currentTimeMillis() % 100000l); >public void initRandomly(int p_value) < zipCode = (int)( System.currentTimeMillis() % 100000l); >void increaseZipCode() < zipCode++; >> package de.beyondjava.Beans; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; public class Person

Alternatives

Of course, there are more professional class parsers out there. For instance, you can use the class parser and AST generator of Eclipse. Two small frameworks making Eclipse JDT easily accessible are

Источник

Оцените статью