How to generate bytecode and save to .class file?

Jus12 :

I have the following weird requirement.

I am given:

  1. A list of some method names.
  2. Names and types of parameters of the above methods.
  3. The functionality of the above methods. This is as follows: For each parameter, the method converts it to string using toString and obtains an array of strings. To this array, the method applies a function foo. The function foo takes as input a String [] type and outputs String. The methods return what foo returns. foo's code is given inside a Java object and is to be accessed as a black-box.

The info in 1. and 2. can be in a text or XML file. For this purpose, we can consider it to be available inside a Java object in whatever way we choose.

The task is to create a .class file (i.e., bytecode) that implements those methods and can be run on the JVM.

I think this assembler library would be one way to do it. Can anyone suggest an easier way?

[EDIT:] I can think of one other way: first generate the .java file and then compile it to get the .class file.

[The context:] I have to do this for several hundreds of methods. I want the shortcut so I can automate my job rather than manually write the code.

Barend :

You could generate the required program code in Java syntax and turn it into a class file using the compiler. It's possible to instantiate javac at run time and pass it a byte array instead of the location of the source file. This is probably the easiest for other programmers to maintain.

If you want to generate byte code directly, asm is the library most commonly used.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to use an anonymous class instance in another generate bytecode class

How to Generate TinkerPOP ByteCode?

How can I save an image in code (via pickle, dill, or other bytecode) and save to file if not exist in a file system?

How do I read a Java class file (Java bytecode)

How to save class File object

Difference between bytecode in .class file and that of .dex file

How to generate looping bytecode using Javassist?

Generate and save XML file

How to generate class file in php with php variables

How to Generate .RBI file for an ActiveModel Class

Java conditionals in class-file (on bytecode level)

How can I save a file to the class path

How can I fix error "decompiled .class file bytecode version 52.0 (Java 8)

Is .class file byte code? or Does .class file include bytecode?

How to generate multiple file paths and save them into a excel with python

How can one generate and save a file client side using Blazor?

How to generate and save to file SummaryReport or any other JMeter Report

How to generate an excel file and save it directly to a path in Django

How to manipulate a class' bytecode just before it is loaded?

How to modify bytecode on class load JVM no args

PHP Generate file and promt save as

Django : generate a file then save it in a FileField

How to convert a pdf file to bytecode in php

How do languages/runtimes based on JVM generate Java bytecode?

Generate Bytecode from AST

How to generate current UTC year in tt model class file

How to generate a class diagram or UML from a json file

how to automatically generate a unique c++ class based on a file

Error reassembling Java bytecode into .class file using Krakatau

TOP Ranking

HotTag

Archive