Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Feb 17, 2022. It is now read-only.
/JavaClassLibPublic archive

A Java class file manipulation library that directly deals with the constructs in the class file

License

NotificationsYou must be signed in to change notification settings

Seggan/JavaClassLib

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This is a Java class file manipulation library that, unlike other bytecode libraries, directly deals with the constructs in the class file without any abstraction. It is still unfinished.

Examples

A generic example can be foundhere.

Creating a class

This will create a basic class namedTheName extendingsome.super.Class compiled with Java version 11. Note that java versions less than 8 are not supported and using one will throw an exception.

JavaClassaClass =newJavaClass("TheName","some/super/Class",11);aClass.setAccessFlags(ClassAccessFlags.PUBLIC);
Adding constant pool entries

Assuming we have the code from above, adding a constant pool entry is simple. For example, here we add aUTF8_info constant pool entry.

UTF8Entryentry =newUTF8Entry(aClass.getConstantPool(),"Write whatever you want in here");

The entry will automatically add itself to the constant pool on creation.

Adding methods

Here comes the juicy part: methods. Adding a method is slightly more complex.

Methodm =newMethod(aClass.getConstantPool(),"yourMethodName","(Lsome/Type;)V");m.setAccessFlags(MethodAccessFlags.PUBLIC);aClass.getMethods().add(m);
Bytecode

Finally, bytecode! In Java, bytecode goes into a thing called acode attribute, which then goes into the method. Here's a sample:

CodeAttributecode =newCodeAttribute(newUTF8Entry(aClass.getConstantPool(),"Code"),5,2,JvmInstructions.ALOAD.create(0),JvmInstructions.DUP.create());m.getAttributes().add(code);

The code attribute has 4 arguments: aUTF8Entry pointing to the valueCode (this is very important!), the max stack size, the max local variable size, and a list of bytecode instructions. In this case the instructions areaload 0, dup

Writing

What's the point of having a Java class if you can't write it? TheJavaClass has a methodwrite(OutputStream out) that writes the class in byte form. It can be used with aFileOutputStream,ByteArrayOutputStream, and all the other variants.

Todo

  • Add the rest of the bytecode instructions
  • Add more attributes
  • Add interface and field capabilities to classes
  • Add exception tables to the code attribute

About

A Java class file manipulation library that directly deals with the constructs in the class file

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp