Tabnine Logo
MethodGen
Code IndexAdd Tabnine to your IDE (free)

How to use
MethodGen
in
org.apache.bcel.generic

Best Java code snippets using org.apache.bcel.generic.MethodGen (Showing top 20 results out of 315)

Refine searchRefine arrow

  • InstructionHandle
  • InstructionList
  • JavaClass
  • Instruction
  • ClassGen
  • BugInstance
  • Method
  • SourceLineAnnotation
origin: apache/geode

cg.addField(field);
InstructionList ctor = new InstructionList();
MethodGen ctorMethod = new MethodGen(Constants.ACC_PUBLIC, Type.VOID,
  new Type[] {Type.OBJECT}, new String[] {"arg0"}, "<init>",
  "org.apache.geode.internal.util.bcel.SerializableImplWithValue", ctor, cp);
ctorMethod.setMaxStack(2);
InstructionHandle ctor_ih_0 = ctor.append(fac.createLoad(Type.OBJECT, 0));
ctor.append(fac.createInvoke(CLASS_NAME_SERIALIZABLE_IMPL, "<init>", Type.VOID, Type.NO_ARGS,
cg.addMethod(ctorMethod.getMethod());
MethodGen getterMethod = new MethodGen(Constants.ACC_PUBLIC, Type.OBJECT, null, null,
  GET_VALUE, CLASS_NAME_SERIALIZABLE_IMPL_WITH_VALUE, getter, cp);
getterMethod.setMaxStack(1);
cg.addMethod(getterMethod.getMethod());
MethodGen setterMethod = new MethodGen(Constants.ACC_PUBLIC, Type.VOID,
  new Type[] {Type.OBJECT}, new String[] {field.getName()}, SET_VALUE,
  CLASS_NAME_SERIALIZABLE_IMPL_WITH_VALUE, setter, cp);
setterMethod.setMaxStack(2);
cg.addMethod(setterMethod.getMethod());
byte[] bytes = jClazz.getBytes();
return defineClass(jClazz.getClassName(), bytes, 0, bytes.length);
origin: xalan/xalan

public void setMaxLocals() {

// Get the current number of local variable slots
int maxLocals = super.getMaxLocals();
// Get numer of actual variables
final LocalVariableGen[] localVars = super.getLocalVariables();
if (localVars != null) {
  if (localVars.length > maxLocals)
  maxLocals = localVars.length;
}
// We want at least 5 local variable slots (for parameters)
if (maxLocals < 5) maxLocals = 5;
super.setMaxLocals(maxLocals);
}
origin: find-sec-bugs/find-sec-bugs

private void analyzeMethod(Method m, ClassContext classContext) throws CFGBuilderException, DataflowAnalysisException {
  MethodGen methodGen = classContext.getMethodGen(m);
  ConstantPoolGen cpg = classContext.getConstantPoolGen();
  CFG cfg = classContext.getCFG(m);
  if (methodGen == null || methodGen.getInstructionList() == null) {
    return; //No instruction .. nothing to do
  }
  for (Iterator<Location> i = cfg.locationIterator(); i.hasNext(); ) {
    Location location = i.next();
    Instruction inst = location.getHandle().getInstruction();
    if (inst instanceof InvokeInstruction) {
      InvokeInstruction invoke = (InvokeInstruction) inst;
      String methodName = invoke.getMethodName(cpg);
      if ("enableDefaultTyping".equals(methodName)) {
        JavaClass clz = classContext.getJavaClass();
        bugReporter.reportBug(new BugInstance(this, DESERIALIZATION_TYPE, HIGH_PRIORITY)
            .addClass(clz)
            .addMethod(clz, m)
            .addCalledMethod(cpg, invoke)
            .addSourceLine(classContext, m, location)
        );
      }
    }
  }
}
origin: spotbugs/spotbugs

@Override
public String toString() {
  return this.getClass().getSimpleName() + "(" + methodGen.getClassName() + "." + methodGen.getMethod().getName()
      + methodGen.getMethod().getSignature() + ")";
}
origin: spotbugs/spotbugs

/**
 * Factory method for creating a source line annotation describing an entire
 * method.
 *
 * @param methodGen
 *            the method being visited
 * @return the SourceLineAnnotation, or null if we do not have line number
 *         information for the method
 */
public static SourceLineAnnotation fromVisitedMethod(MethodGen methodGen, String sourceFile) {
  LineNumberTable lineNumberTable = methodGen.getLineNumberTable(methodGen.getConstantPool());
  String className = methodGen.getClassName();
  int codeSize = methodGen.getInstructionList().getLength();
  if (lineNumberTable == null) {
    return createUnknown(className, sourceFile, 0, codeSize - 1);
  }
  return forEntireMethod(className, sourceFile, lineNumberTable, codeSize);
}
origin: virjar/vscrawler

private void createSetMethod(String method_name, String field_name, Class field_type) {
  InstructionList tmpIl = new InstructionList();
  MethodGen method = new MethodGen(ACC_PUBLIC | ACC_FINAL, Type.VOID, buildArgs(new Class[] {field_type}), new String[] {"arg"}, method_name, classname, tmpIl, cp);
  tmpIl.append(InstructionConstants.ALOAD_0);
  tmpIl.append(InstructionConstants.ALOAD_1);
  tmpIl.append(factory.createFieldAccess(classname, field_name, getObjectType(Object.class), Constants.PUTFIELD));
  tmpIl.append(InstructionFactory.createReturn(Type.VOID));
  method.setMaxStack();
  method.setMaxLocals();
  cg.addMethod(method.getMethod());
  tmpIl.dispose();
}
origin: org.apache.yoko/yoko-rmi-impl

InstructionList il;
cp = clazz.getConstantPool();
il = new InstructionList();
Type[] methodArgTypes = translate(method.getParameterTypes());
MethodGen mg = new MethodGen(
    Constants.ACC_FINAL | Constants.ACC_PUBLIC, methodReturnType,
    method.getName(), clazz.getClassName(), il, cp);
mg.addAttribute(new Synthetic(cp.addUtf8("Synthetic"), 0, null, cp
    .getConstantPool()));
  mg.addException(throwsException[i].getName());
il.append(InstructionFactory.createThis());
il.append(new INVOKESPECIAL(cp.addMethodref(method.getDeclaringClass()
    .getName(), method.getName(), method.getSignature())));
mg.setMaxStack();
mg.setMaxLocals();
clazz.addMethod(mg.getMethod());
origin: velocity/velocity-dep

    new ClassGen(template, "java.lang.Object", "<generated>",
    Constants.ACC_PUBLIC | Constants.ACC_SUPER, null);
ConstantPoolGen cp = cg.getConstantPool(); // cg creates constant pool
InstructionList il = new InstructionList();
MethodGen mg = new MethodGen(Constants.ACC_STATIC |
LocalVariableGen lg = mg.addLocalVariable("in",
    new ObjectType("java.io.BufferedReader"), null, null);
int in = lg.getIndex();
lg = mg.addLocalVariable("name", Type.STRING, null, null);
int name = lg.getIndex();
il.append(ACONST_NULL);
InstructionHandle handler = il.append(RETURN);
mg.addExceptionHandler(try_start, try_end, handler,
    new ObjectType("java.io.IOException"));
il.append(RETURN);
mg.setMaxStack(5); // Needed stack size
cg.addMethod(mg.getMethod());
  cg.getJavaClass().dump(template + ".class");
origin: contra/JMD

public void replaceStrings() {
  for (ClassGen cg : cgs.values()) {
    int replaced = 0;
    for (Method method : cg.getMethods()) {
      MethodGen mg = new MethodGen(method, cg.getClassName(), cg.getConstantPool());
      InstructionList list = mg.getInstructionList();
      if (list == null) {
        continue;
      InstructionHandle[] handles = list.getInstructionHandles();
      for (int i = 1; i < handles.length; i++) {
        if (GenericMethods.isCall(handles[i].getInstruction()) && handles[i - 1].getInstruction() instanceof LDC) {
          String methodCallClass = GenericMethods.getCallClassName(handles[i].getInstruction(), cg.getConstantPool());
          String methodCallMethod = GenericMethods.getCallMethodName(handles[i].getInstruction(), cg.getConstantPool());
          String methodCallSig = GenericMethods.getCallSignature(handles[i].getInstruction(), cg.getConstantPool());
              continue;
            logger.debug(encryptedString + " -> " + decryptedString + " in " + cg.getClassName() + "." + method.getName());
            int stringRef = cg.getConstantPool().addString(decryptedString);
            LDC lc = new LDC(stringRef);
      mg.setInstructionList(list);
      mg.setMaxLocals();
      mg.setMaxStack();
      cg.replaceMethod(method, mg.getMethod());
origin: org.apache.bcel/bcel

mg.addAnnotationEntry(createSimpleVisibleAnnotation(mg
    .getConstantPool()));
    Type.VOID, new Type[] { new ObjectType("java.io.Reader") },
    Const.INVOKESPECIAL));
LocalVariableGen lg = mg.addLocalVariable("in", new ObjectType(
    "java.io.BufferedReader"), null, null);
final int in = lg.getIndex();
lg.setStart(il.append(new ASTORE(in))); // "in" valid from here
lg = mg.addLocalVariable("name", Type.STRING, null, null);
final int name = lg.getIndex();
il.append(InstructionConst.ACONST_NULL);
final LocalVariableGen var_ex = mg.addLocalVariable("ex", Type
    .getType("Ljava.io.IOException;"), null, null);
final int var_ex_slot = var_ex.getIndex();
var_ex.setStart(handler);
var_ex.setEnd(il.append(InstructionConst.RETURN));
mg.addExceptionHandler(try_start, try_end, handler, new ObjectType(
    "java.io.IOException"));
mg.setMaxStack();
mg.setMaxLocals();
cg.addMethod(mg.getMethod());
il.dispose(); // Allow instruction handles to be reused
cg.addEmptyConstructor(Const.ACC_PUBLIC);
origin: bcel/bcel

setMaxStack(c.getMaxStack());
setMaxLocals(c.getMaxLocals());
   end = il.getEnd();
  } else {
   end = il.findHandle(end_pc);
   end = end.getPrev(); // Make it inclusive
  addExceptionHandler(il.findHandle(ce.getStartPC()), end,
      il.findHandle(ce.getHandlerPC()), c_type);
   addLineNumber(il.findHandle(l.getStartPC()), l.getLineNumber());
  removeLocalVariables();
   addLocalVariable(l.getName(), Type.getType(l.getSignature()),
        l.getIndex(), start, end);
  addCodeAttribute(a);
 addException(names[j]);
 } else
addAttribute(a);
origin: org.jboss.mx/jboss-mbeans

InstructionList invokeInstructions = new InstructionList();
MethodEntry[] operations           = getOperations(info);
  invokeInstructions.append(new ALOAD(1));                                   // Stack:  => ..., arg1 [String]
   invokeInstructions.append(ifOperationEqualsNull);                       // Stack:  => ...
  beginTryBlock = beginTryBlock.getNext();
  endTryBlock = jumpToSuperInvoke.getPrev();
invokeInstructions.append(new ARETURN());
MethodGen invokeMethod = new MethodGen(
   Constants.ACC_PUBLIC,
   Type.OBJECT,
   className, invokeInstructions, cp
);
invokeMethod.setMaxLocals(7);
invokeMethod.setMaxStack(calculateMaxStackSize(info));
invokeMethod.addException(ReflectionException.class.getName());
invokeMethod.addException(MBeanException.class.getName());
  invokeMethod.addExceptionHandler(beginTryBlock, endTryBlock, exceptionHandlerCode, new ObjectType("java.lang.Throwable"));
origin: org.apache.bcel/bcel

if (a instanceof Code) {
  final Code c = (Code) a;
  setMaxStack(c.getMaxStack());
  setMaxLocals(c.getMaxLocals());
  final CodeException[] ces = c.getExceptionTable();
  if (ces != null) {
      addExceptionHandler(il.findHandle(ce.getStartPC()), end, il.findHandle(ce
          .getHandlerPC()), c_type);
          addLineNumber(ih, l.getLineNumber());
      updateLocalVariableTable((LocalVariableTable) a);
    } else if (a instanceof LocalVariableTypeTable) {
      this.local_variable_type_table = (LocalVariableTypeTable) a.copy(cp.getConstantPool());
    } else {
      addCodeAttribute(a);
  final String[] names = ((ExceptionTable) a).getExceptionNames();
  for (final String name2 : names) {
    addException(name2);
  final AnnotationEntry[] aes = runtimeAnnotations.getAnnotationEntries();
  for (final AnnotationEntry element : aes) {
    addAnnotationEntry(new AnnotationEntryGen(element, cp, false));
  addAttribute(a);
origin: spotbugs/spotbugs

ClassGen classGen = new ClassGen(jclass);
Method[] methodList = jclass.getMethods();
for (Method method : methodList) {
  if (method.isAbstract() || method.isNative()) {
    continue;
  if (methodName != null && !method.getName().equals(methodName)) {
    continue;
  MethodGen methodGen = new MethodGen(method, jclass.getClassName(), classGen.getConstantPool());
origin: org.apache.karaf.bundles/org.apache.karaf.bundles.xalan-2.7.1

final ConstantPoolGen cpg = classGen.getConstantPool();
};
final InstructionList il = new InstructionList();
      argTypes, argNames,
      "buildKeys", _className, il,
      classGen.getConstantPool());
buildKeys.addException("org.apache.xalan.xsltc.TransletException");
il.append(RETURN);
origin: org.apache.xalan/com.springsource.org.apache.xalan

public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
ConstantPoolGen cpg = classGen.getConstantPool();
InstructionList il = methodGen.getInstructionList();
il.append(new PUSH(cpg, _value));
}
origin: thesp0nge/owasp-orizon

 public static boolean disassemble(String name) {
  JavaClass mod = null;
  try {
   mod = Repository.lookupClass(name);
  }
  catch (Exception e) {
   System.err.println("Could not get class " + name);
   return false;
  }

  ClassGen modClass = new ClassGen(mod);
  ConstantPoolGen cp = modClass.getConstantPool();

  Method[] methods = mod.getMethods();
  for (int i = 0; i < methods.length; i++) {
   System.out.println("* "+methods[i].getName());
   MethodGen mg = new MethodGen(methods[i], mod.getClassName(), cp);
   InstructionList il = mg.getInstructionList();
   System.out.println(il.toString());
  }

  return true;
 }
}
origin: org.apache.bcel/bcel

public Method injection(final JavaClass clazz, Method method, final ConstantPoolGen cp, final int firstStringOffset) {
  final MethodGen methodGen = new MethodGen(method, clazz.getClassName(), cp);
  final InstructionList instructionList = methodGen.getInstructionList();
  instructionList.insert(instructionList.getStart(), makeWillBeAddedInstructionList(methodGen, firstStringOffset));
  methodGen.setMaxStack();
  methodGen.setMaxLocals();
  method = methodGen.getMethod();
  instructionList.dispose();
  return method;
}
origin: contra/JMD

public void fixStack() {
  for (ClassGen cg : cgs.values()) {
    for (Method method : cg.getMethods()) {
      MethodGen mg = new MethodGen(method, cg.getClassName(), cg.getConstantPool());
      mg.removeNOPs();
      mg.setMaxLocals();
      mg.setMaxStack();
      cg.replaceMethod(method, mg.getMethod());
      logger.debug(String.format("Reset MaxStack and MaxLocals in %s.%s", cg.getClassName(), mg.getName()));
    }
  }
}
origin: org.apache.bcel/bcel

@Override
public void visitMethod( final Method method ) {
  final MethodGen mg = new MethodGen(method, _clazz.getClassName(), _cp);
  _out.println("    InstructionList il = new InstructionList();");
  _out.println("    MethodGen method = new MethodGen("
      + printFlags(method.getAccessFlags(), FLAGS.METHOD) + ", "
      + printType(mg.getReturnType()) + ", "
      + printArgumentTypes(mg.getArgumentTypes()) + ", "
      + "new String[] { " + Utility.printArray(mg.getArgumentNames(), false, true)
      + " }, \"" + method.getName() + "\", \"" + _clazz.getClassName() + "\", il, _cp);");
  _out.println();
  final BCELFactory factory = new BCELFactory(mg, _out);
  factory.start();
  _out.println("    method.setMaxStack();");
  _out.println("    method.setMaxLocals();");
  _out.println("    _cg.addMethod(method.getMethod());");
  _out.println("    il.dispose();");
}
org.apache.bcel.genericMethodGen

Javadoc

Template class for building up a method. This is done by defining exception handlers, adding thrown exceptions, local variables and attributes, whereas the `LocalVariableTable' and `LineNumberTable' attributes will be set automatically for the code. Use stripAttributes() if you don't like this. While generating code it may be necessary to insert NOP operations. You can use the `removeNOPs' method to get rid off them. The resulting method object can be obtained via the `getMethod()' method.

Most used methods

  • getMethod
    Get method object. Never forget to call setMaxStack() or setMaxStack(max), respectively, before call
  • <init>
    Instantiate from existing method.
  • setMaxStack
    Set maximum stack size for this method.
  • setMaxLocals
    Set maximum number of local variables.
  • getInstructionList
  • addLocalVariable
    Adds a local variable to this method and assigns an index automatically.
  • getMaxLocals
  • addException
    Add an exception possibly thrown by this method.
  • addExceptionHandler
    Add an exception handler, i.e., specify region where a handler is active and an instruction where th
  • getConstantPool
  • getLocalVariables
  • getSignature
  • getLocalVariables,
  • getSignature,
  • getClassName,
  • getExceptionHandlers,
  • getName,
  • setInstructionList,
  • getArgumentTypes,
  • isStatic,
  • removeLocalVariable,
  • getLineNumberTable

Popular in Java

  • Reading from database using SQL prepared statement
  • getSharedPreferences (Context)
  • putExtra (Intent)
  • getSupportFragmentManager (FragmentActivity)
  • Component (java.awt)
    A component is an object having a graphical representation that can be displayed on the screen and t
  • GridLayout (java.awt)
    The GridLayout class is a layout manager that lays out a container's components in a rectangular gri
  • MalformedURLException (java.net)
    This exception is thrown when a program attempts to create an URL from an incorrect specification.
  • Random (java.util)
    This class provides methods that return pseudo-random values.It is dangerous to seed Random with the
  • Logger (org.apache.log4j)
    This is the central class in the log4j package. Most logging operations, except configuration, are d
  • Logger (org.slf4j)
    The org.slf4j.Logger interface is the main user entry point of SLF4J API. It is expected that loggin
  • Github Copilot alternatives
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now