1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
/*
* BCELTest.java
*/
/*
* Copyright (c) 2002 by Matthias Pfisterer <Matthias.Pfisterer@web.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as published
* by the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package org.tritonus.saol.compiler;
import java.io.IOException;
import org.apache.bcel.Constants;
import org.apache.bcel.classfile.Field;
import org.apache.bcel.classfile.JavaClass;
import org.apache.bcel.generic.*;
/** Example program using the BCEL.
BCEL is the Byte Code Engineering Library, part of the
jakarta project (http://jakarta.apache.org/bcel/).
*/
public class BCELTest
{
public static void main(String[] args)
{
String strClassName = "tone";
ClassGen classGen = new ClassGen(strClassName,
"AbstractInstrument",
"<generated>",
Constants.ACC_PUBLIC | Constants.ACC_SUPER,
null);
ConstantPoolGen constantPoolGen = classGen.getConstantPool();
int nAInitValueIndex = constantPoolGen.addFloat(0.196307F);
int nXInitValueIndex = constantPoolGen.addFloat(0.5F);
FieldGen fieldGen;
fieldGen = new FieldGen(Constants.ACC_PRIVATE,
Type.FLOAT,
"a",
constantPoolGen);
classGen.addField(fieldGen.getField());
fieldGen = new FieldGen(Constants.ACC_PRIVATE,
Type.FLOAT,
"x",
constantPoolGen);
classGen.addField(fieldGen.getField());
fieldGen = new FieldGen(Constants.ACC_PRIVATE,
Type.FLOAT,
"y",
constantPoolGen);
classGen.addField(fieldGen.getField());
fieldGen = new FieldGen(Constants.ACC_PRIVATE,
Type.FLOAT,
"init",
constantPoolGen);
classGen.addField(fieldGen.getField());
InstructionList il = new InstructionList();
MethodGen methodGen;
methodGen = new MethodGen(Constants.ACC_PUBLIC,
Type.VOID,
new Type[]{new ObjectType("RTSystem")},
new String[]{"rtSystem"},
"doAPass",
strClassName,
il,
constantPoolGen);
InstructionFactory ifac = new InstructionFactory(constantPoolGen);
il.append(InstructionConstants.ALOAD_0);
il.append(new LDC(nAInitValueIndex));
il.append(ifac.createPutField(strClassName, "a", Type.FLOAT));
il.append(InstructionConstants.ALOAD_0);
il.append(ifac.createGetField(strClassName, "init", Type.FLOAT));
il.append(InstructionConstants.FCONST_0);
il.append(InstructionConstants.FCMPL);
IFNE ifne0 = new IFNE(null);
il.append(ifne0);
il.append(InstructionConstants.ALOAD_0);
il.append(InstructionConstants.FCONST_1);
il.append(ifac.createPutField(strClassName, "init", Type.FLOAT));
il.append(InstructionConstants.ALOAD_0);
il.append(new LDC(nXInitValueIndex));
il.append(ifac.createPutField(strClassName, "x", Type.FLOAT));
InstructionHandle ih0 = il.append(InstructionConstants.ALOAD_0);
ifne0.setTarget(ih0);
il.append(InstructionConstants.ALOAD_0);
il.append(ifac.createGetField(strClassName, "x", Type.FLOAT));
il.append(InstructionConstants.ALOAD_0);
il.append(ifac.createGetField(strClassName, "a", Type.FLOAT));
il.append(InstructionConstants.ALOAD_0);
il.append(ifac.createGetField(strClassName, "y", Type.FLOAT));
il.append(InstructionConstants.FMUL);
il.append(InstructionConstants.FSUB);
il.append(ifac.createPutField(strClassName, "x", Type.FLOAT));
il.append(InstructionConstants.ALOAD_0);
il.append(InstructionConstants.ALOAD_0);
il.append(ifac.createGetField(strClassName, "y", Type.FLOAT));
il.append(InstructionConstants.ALOAD_0);
il.append(ifac.createGetField(strClassName, "a", Type.FLOAT));
il.append(InstructionConstants.ALOAD_0);
il.append(ifac.createGetField(strClassName, "x", Type.FLOAT));
il.append(InstructionConstants.FMUL);
il.append(InstructionConstants.FADD);
il.append(ifac.createPutField(strClassName, "y", Type.FLOAT));
il.append(InstructionConstants.ALOAD_1);
il.append(InstructionConstants.ALOAD_0);
il.append(ifac.createGetField(strClassName, "y", Type.FLOAT));
il.append(ifac.createInvoke("RTSystem", "output", Type.VOID, new Type[]{Type.FLOAT}, Constants.INVOKEVIRTUAL));
il.append(InstructionConstants.RETURN);
methodGen.setMaxStack();
classGen.addMethod(methodGen.getMethod());
classGen.addEmptyConstructor(Constants.ACC_PUBLIC);
JavaClass javaClass = classGen.getJavaClass();
try
{
javaClass.dump("tone.class");
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
/*** BCELTest.java ***/
|