Dynamically generate Java code

zhaozj2021-02-16  71

Dynamically generate Java code

Bcel is Bytecode Engineering Library, which is a third-party byte code generation library that can help us directly generate Java bytecodes directly in memory. BCEL is a source code open free code base (http://sourceforge.net/projects/bcel/), can help us analyze, create, and process binary Java bytecodes. This article will explain any BCEL to dynamically generate Java code.

/ *

? * Create a date 2004-7-2

? * Create a human hongsoft

? * File name Testbcel.java

? * /

Package com.hongsoft.test;

?

Import java.io. *;

Import java.util.stack;

// Download BCEL Code Base from SourceForge.Net/Projects/bcel/

Import de.Fub.bytecode.classfile. *;

Import de.Fub.bytecode.Generic. *;

Import de.fub.bytecode.constants;

?

PUBLIC CLASS TESTBCEL? EXTENDS CLASSLOADER {

?????prate object o;

??? public static void? main (string [] args) {

??????? new testbcel (); ??????

}

?

??? public? testbcel ()

?????? {

?????????? ??????

?????????????? // declared class, trouble_ticket is a class name, superclass is java.Lang.Object

??????????????? // ACC represents Access, new string [] {} is an array of interfaces it wants to implement

??????? ??????? // effect: public Trouble_ticket {}

????????????? Classgen ClassGen

????????????????????????????? = new classgen, "java.lang.object", "", ??????? ?? constants.acc_public | Constants.acc_super, new string [] {};

???????

?????????????? // constant pool

ConstantPoolgen CP = Classgen.getConstantPool ();

????

?????????????????? // constructor, effect: public string creater = ""

????????????? ?? fieldgen fg = new fieldgen (constants.acc_public, type.string, ??????????? ?? "Creater", CP);

?????????????????? // joined FIELD

????????????? ?? Classgen.Addfield (fg.getfield ());

??????

???????????????????? ? // Constructor?????????????????? ? ?????? ???????????? ?? Classgen.adDemptyconstructor (constants.acc_public);

?????????????????????? // Byte form

???????????????????????. Byte [] Data = Classgen.getjavaclass ().

????????????????????? // generated

?????????????????????? Class C = DefineClass (data, 0, data.length); ??????????????????????????????????? ?????? try {

?????????????????????????? o = c.newinstance ();

???????????????????????????? // program effect ???????????????

???????????????????????? system.out.println (O.GetClass (). GetName ());

???????????????????????? system.out.println (O.GetClass (). Getfield ("creater"). GetName ());?? ? ????????????????????

???????????????????????? system.out.println (Classgen.Getfields (). Length);

???????????????????????????????} catch (IllegalaccessException E) {

??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????.

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????} Catch (Exception E) {

??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????.

??????????????????????}

??????}

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

}

The procedure run is as follows:

Trouble_ticket

Creater

1

转载请注明原文地址:https://www.9cbs.com/read-18041.html

New Post(0)