All analysis using Javassist


Compiled class texts in Java, Compiled class texts, Compiled class texts, Impossible re-compilation repairs, Compiled class texts for Javassist`, Compiled class texts for other cans How to increase, and also undemanding deeply entered compiled language, can be generated.

创 KEN class

Maven item, pull-in Javassist 库

<!-- -->

Use javassist Kenichi Kuruma Personal

package com.y4er.learn;

import javassist.*;

public class CreateClass {
    public static void main(String[] args) throws Exception {
        //Maintenance javassist maintenance pond
        ClassPool pool = ClassPool.getDefault();

        //One individual sky class com.y4er.learn.Person
        CtClass ctClass = pool.makeClass("com.y4er.learn.Person");

        //给 ctClass type addition single string type type character stage name
        CtField name = new CtField(pool.get("java.lang.String"), "name", ctClass);

        //Setting private

        //Initialization name character stage zhangsan
        ctClass.addField(name, CtField.Initializer.constant("zhangsan"));

        //Generate get, set method

        //Additive-free constructor
        CtConstructor ctConstructor = new CtConstructor(new CtClass[]{}, ctClass);

        //Additive participation structure
        CtConstructor ctConstructor1 = new CtConstructor(new CtClass[]{pool.get("java.lang.String")}, ctClass);

        //Jian Jian one public method printName()No participation, no return
        CtMethod printName = new CtMethod(CtClass.voidType, "printName", new CtClass[]{}, ctClass);

        //Copy class text

Person.class image.png

how to use

CtClass representative class, ClassPool CtClass container, ClassPool maintenance CtClass object, demand cautionary measure CtClass quantity over-competition occupancy large amount , CtClass.detach () for demand adjustment.

ClassPool Priority Yes Below Individual Method:

  1. getDefault () Singleton ClassPool
  2. appendClassPath () ClassPath
  3. insertClassPath () Insert ClassPath jar
  4. get () Root name 获tori CtClass object
  5. toClass () General CtClass Converting Class Once converted and cannot be converted
  6. makeClass () New type or interface

Documentation for the migrants:

CtClass demand-related method:

  1. addConstructor () Add Constructor
  2. addField () Add character stage
  3. addInterface () Add interface
  4. addMethod () Add method
  5. freeze () Unusable repair repair
  6. defrost () Rehabilitation
  7. detach () 来 ClassPool
  8. toBytecode () Transformed 节 码
  9. toClass () Transform Class object
  10. writeFile () Copy .class text
  11. setModifiers ()


CtMethod Inheritance CtBehavior, Demand-related method:

  1. insertBefore Methodical starting position Insertion fee
  2. insterAfter Representational possession return
  3. insertAt Specified position insertion fee
  4. setBody General method content setting required Copying qualification, this method abstract repair time, the repair mark transfer
  5. make a new method

More steps:

At setBody () Nakaga 们 Used $ code Representative argument

// $0 representative this$1 Representative 1st individual argument analogy

Use CtClass generation object

Above sentence I have generated one ctClass object. Applicable person.class, Person type generation object for adjustment, attribute or method for adjustment?

Three methods:

  1. For reflection method adjustment
  2. Addition class text
  3. Passage interface

For reflex adjustment

Object o = ctClass.toClass().newInstance();
Method setName = o.getClass().getMethod("setName", String.class);
Method printName1 = o.getClass().getMethod("printName");

Addition class text

ClassPool pool = ClassPool.getDefault();
CtClass PersonClass = pool.get("com.y4er.learn.Person");
Object o = PersonClass.toClass().newInstance();
//For subordinate reflex adjustment

For passing interface adjustment

Newly built one interface IPerson, general Person type method can all be abstracted

package com.y4er.learn;

public interface IPerson {
    String getName();

    void setName(String name);

    void printName();
ClassPool pool = ClassPool.getDefault();

CtClass IPerson = pool.get("com.y4er.learn.IPerson");
CtClass Person = pool.get("com.y4er.learn.Person");
Person.setInterfaces(new CtClass[]{IPerson});

IPerson o = (IPerson) Person.toClass().newInstance();

General Person type implementation IPerson interface, direct conversion type at the time of the case, and direct adjustment.

Renovation existing type

javassist The majority of users are renovated and modified, and the user's face is normal. Personal use of self-explanatory solution:

// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)

package com.y4er.learn;

public class Person implements IPerson {
    private String name = "zhangsan";

    public String getName() {

    public void setName(String var1) { = var1;

    public Person() { = "xiaoming";

    public Person(String var1) { = var1;

    public void printName() {

At this time, my thoughts printName

------ printName start ------
------ printName  over ------

Shaichi Shimodai:

CtClass Person = pool.get("com.y4er.learn.Person");

CtMethod printName1 = Person.getDeclaredMethod("printName", null);
printName1.insertBefore("System.out.println(\"------ printName start ------\");");
printName1.insertAfter("System.out.println(\"------ printName  over ------\");");

Object o = Person.toClass().newInstance();
Method printName2 = o.getClass().getMethod("printName");
printName2.invoke(o, null);

Tsujimatsu Realization Completed Face image.png

Recommended Posts

All analysis using Javassist
[Java] Spam judgment using morphological analysis "lucene-gosen"