How to automatically generate function headers for .h file in Clion?

Maybe it’s a little late (about 4 years), but here’s the best way i’ve found (for a c file): cut and paste the contents of the .c in the .h file, and for each function, put the cursor on it’s name and press Alt+Enter, and choose “Split function into declaration and definition”. this will keep … Read more

Is there code generation API for TypeScript?

Try ts-morph. Only been working with it for about an hour but it seems really capable. import {Project, Scope, SourceFile} from “ts-morph”; const project = new Project(); const sourceFile = project.createSourceFile(`./target/file.ts`); const classDeclaration = sourceFile.addClass({ name: ‘SomeClass’ }); const constr = classDeclaration.addConstructor({}); constr.setBodyText(‘this.myProp = myProp’); classDeclaration.addProperty({ name: ‘myProp’, type: ‘string’, initializer: ‘hello world!’, scope: Scope.Public … Read more

How to use OpenAPI “oneOf” property with openapi-generator-maven-plugin when generating Spring code

Currently, openapi-generator doesn’t support oneOf. This is a capability that had been newly introduced with OpenAPI v3 (FYI, only v2 and below are called “Swagger”, it has then been renamed to OpenAPI). There are various generators (Java, Spring, lots of other languages). I have seen that contributions have been made during this year to enable … Read more

Do you create your own code generators?

In “Pragmatic Programmer” Hunt and Thomas distinguish between Passive and Active code generators. Passive generators are run-once, after which you edit the result. Active generators are run as often as desired, and you should never edit the result because it will be replaced. IMO, the latter are much more valuable because they approach the DRY … Read more

How to emit and execute Java bytecode at runtime?

Here is a working “hello world” made with ObjectWeb ASM (a library which I recommend): package hello; import java.lang.reflect.Method; import org.objectweb.asm.ClassWriter; import org.objectweb.asm.Label; import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.Opcodes; public class HelloWorldASM implements Opcodes { public static byte[] compile(String name) { ClassWriter cw = new ClassWriter(0); MethodVisitor mv; cw.visit(V1_6, ACC_PUBLIC + ACC_SUPER, “hello/HelloWorld”, null, “java/lang/Object”, null); cw.visitSource(“HelloWorld.java”, … Read more

Code generation by genetic algorithms

If you are sure you want to do this, you want genetic programming, rather than a genetic algorithm. GP allows you to evolve tree-structured programs. What you would do would be to give it a bunch of primitive operations (while($register), read($register), increment($register), decrement($register), divide($result $numerator $denominator), print, progn2 (this is GP speak for “execute two … Read more

Is it possible to programmatically compile java source code in memory only?

To start, look at the JavaCompiler API. Basically: Create the Java class in a string. Put the string into class that extends SimpleJavaFileObject. Compile using a JavaCompiler instance. Finally, call the methods the new class. Here is an example that works with JDK6+: import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; import java.lang.reflect.InvocationTargetException; import java.net.URI; import java.util.Arrays; … Read more

tech