本文整理汇总了Java中javassist.compiler.Javac类的典型用法代码示例。如果您正苦于以下问题:Java Javac类的具体用法?Java Javac怎么用?Java Javac使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Javac类属于javassist.compiler包,在下文中一共展示了Javac类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: make
import javassist.compiler.Javac; //导入依赖的package包/类
/**
* Compiles the given source code and creates a constructor.
* The source code must include not only the constructor body
* but the whole declaration.
*
* @param src the source text.
* @param declaring the class to which the created constructor is added.
*/
public static CtConstructor make(String src, CtClass declaring)
throws CannotCompileException
{
Javac compiler = new Javac(declaring);
try {
CtMember obj = compiler.compile(src);
if (obj instanceof CtConstructor) {
// a stack map table has been already created.
return (CtConstructor)obj;
}
}
catch (CompileError e) {
throw new CannotCompileException(e);
}
throw new CannotCompileException("not a constructor");
}
示例2: make
import javassist.compiler.Javac; //导入依赖的package包/类
/**
* Compiles the given source code and creates a method.
* The source code must include not only the method body
* but the whole declaration, for example,
*
* <ul><pre>"public Object id(Object obj) { return obj; }"</pre></ul>
*
* <p>If the source code includes <code>$proceed()</code>, then
* it is compiled into a method call on the specified object.
*
* @param src the source text.
* @param declaring the class to which the created method is added.
* @param delegateObj the source text specifying the object
* that is called on by <code>$proceed()</code>.
* @param delegateMethod the name of the method
* that is called by <code>$proceed()</code>.
*/
public static CtMethod make(String src, CtClass declaring,
String delegateObj, String delegateMethod)
throws CannotCompileException
{
Javac compiler = new Javac(declaring);
try {
if (delegateMethod != null)
compiler.recordProceed(delegateObj, delegateMethod);
CtMember obj = compiler.compile(src);
if (obj instanceof CtMethod)
return (CtMethod)obj;
}
catch (CompileError e) {
throw new CannotCompileException(e);
}
throw new CannotCompileException("not a method");
}
示例3: make
import javassist.compiler.Javac; //导入依赖的package包/类
/**
* Compiles the given source code and creates a field.
* Examples of the source code are:
*
* <ul><pre>
* "public String name;"
* "public int k = 3;"</pre></ul>
*
* <p>Note that the source code ends with <code>';'</code>
* (semicolon).
*
* @param src the source text.
* @param declaring the class to which the created field is added.
*/
public static CtField make(String src, CtClass declaring)
throws CannotCompileException
{
Javac compiler = new Javac(declaring);
try {
CtMember obj = compiler.compile(src);
if (obj instanceof CtField)
return (CtField)obj; // an instance of Javac.CtFieldWithInit
}
catch (CompileError e) {
throw new CannotCompileException(e);
}
throw new CannotCompileException("not a field");
}
示例4: compile
import javassist.compiler.Javac; //导入依赖的package包/类
/**
* Produces codes in which a new object is created and assigned to
* the field as the initial value.
*/
int compile(CtClass type, String name, Bytecode code,
CtClass[] parameters, Javac drv)
throws CannotCompileException
{
int stacksize;
code.addAload(0);
code.addNew(objectType);
code.add(Bytecode.DUP);
code.addAload(0);
if (stringParams == null)
stacksize = 4;
else
stacksize = compileStringParameter(code) + 4;
if (withConstructorParams)
stacksize += CtNewWrappedMethod.compileParameterList(code,
parameters, 1);
code.addInvokespecial(objectType, "<init>", getDescriptor());
code.addPutfield(Bytecode.THIS, name, Descriptor.of(type));
return stacksize;
}
示例5: compileIfStatic
import javassist.compiler.Javac; //导入依赖的package包/类
/**
* Produces codes for a static field.
*/
int compileIfStatic(CtClass type, String name, Bytecode code,
Javac drv) throws CannotCompileException
{
String desc;
code.addNew(objectType);
code.add(Bytecode.DUP);
int stacksize = 2;
if (stringParams == null)
desc = "()V";
else {
desc = "([Ljava/lang/String;)V";
stacksize += compileStringParameter(code);
}
code.addInvokespecial(objectType, "<init>", desc);
code.addPutstatic(Bytecode.THIS, name, Descriptor.of(type));
return stacksize;
}
示例6: insertAfterAdvice
import javassist.compiler.Javac; //导入依赖的package包/类
private void insertAfterAdvice(Bytecode code, Javac jv, String src,
ConstPool cp, CtClass rtype, int varNo)
throws CompileError
{
if (rtype == CtClass.voidType) {
code.addOpcode(Opcode.ACONST_NULL);
code.addAstore(varNo);
jv.compileStmnt(src);
code.addOpcode(Opcode.RETURN);
if (code.getMaxLocals() < 1)
code.setMaxLocals(1);
}
else {
code.addStore(varNo, rtype);
jv.compileStmnt(src);
code.addLoad(varNo, rtype);
if (rtype.isPrimitive())
code.addOpcode(((CtPrimitiveType)rtype).getReturnOp());
else
code.addOpcode(Opcode.ARETURN);
}
}
示例7: modifyClassConstructor
import javassist.compiler.Javac; //导入依赖的package包/类
private void modifyClassConstructor(ClassFile cf)
throws CannotCompileException, NotFoundException
{
if (fieldInitializers == null)
return;
Bytecode code = new Bytecode(cf.getConstPool(), 0, 0);
Javac jv = new Javac(code, this);
int stacksize = 0;
boolean doInit = false;
for (FieldInitLink fi = fieldInitializers; fi != null; fi = fi.next) {
CtField f = fi.field;
if (Modifier.isStatic(f.getModifiers())) {
doInit = true;
int s = fi.init.compileIfStatic(f.getType(), f.getName(),
code, jv);
if (stacksize < s)
stacksize = s;
}
}
if (doInit) // need an initializer for static fileds.
modifyClassConstructor(cf, code, stacksize, 0);
}
示例8: makeFieldInitializer
import javassist.compiler.Javac; //导入依赖的package包/类
private int makeFieldInitializer(Bytecode code, CtClass[] parameters)
throws CannotCompileException, NotFoundException
{
int stacksize = 0;
Javac jv = new Javac(code, this);
try {
jv.recordParams(parameters, false);
}
catch (CompileError e) {
throw new CannotCompileException(e);
}
for (FieldInitLink fi = fieldInitializers; fi != null; fi = fi.next) {
CtField f = fi.field;
if (!Modifier.isStatic(f.getModifiers())) {
int s = fi.init.compile(f.getType(), f.getName(), code,
parameters, jv);
if (stacksize < s)
stacksize = s;
}
}
return stacksize;
}
示例9: insertAfterAdvice
import javassist.compiler.Javac; //导入依赖的package包/类
private int insertAfterAdvice(Bytecode code, Javac jv, String src,
ConstPool cp, CtClass rtype, int varNo)
throws CompileError
{
int pc = code.currentPc();
if (rtype == CtClass.voidType) {
code.addOpcode(Opcode.ACONST_NULL);
code.addAstore(varNo);
jv.compileStmnt(src);
code.addOpcode(Opcode.RETURN);
if (code.getMaxLocals() < 1)
code.setMaxLocals(1);
}
else {
code.addStore(varNo, rtype);
jv.compileStmnt(src);
code.addLoad(varNo, rtype);
if (rtype.isPrimitive())
code.addOpcode(((CtPrimitiveType)rtype).getReturnOp());
else
code.addOpcode(Opcode.ARETURN);
}
return code.currentPc() - pc;
}
示例10: getConstantValue
import javassist.compiler.Javac; //导入依赖的package包/类
int getConstantValue(ConstPool cp, CtClass type) {
try {
ASTree t = Javac.parseExpr(expression, new SymbolTable());
return getConstantValue2(cp, type, t);
}
catch (CompileError e) {
return 0;
}
}