-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for automatically setting a module main class if module…
…-info.java is part of the compilation operation and a main class was provided by the project.
- Loading branch information
Showing
3 changed files
with
80 additions
and
4 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
src/main/java/rife/bld/instrument/ModuleMainClassAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright 2001-2024 Geert Bevin (gbevin[remove] at uwyn dot com) | ||
* Licensed under the Apache License, Version 2.0 (the "License") | ||
*/ | ||
package rife.bld.instrument; | ||
|
||
import rife.asm.*; | ||
|
||
/** | ||
* This utility class will modify a Java module {@code module-info.class} to add | ||
* a module main class to its attributes. | ||
* | ||
* @author Geert Bevin (gbevin[remove] at uwyn dot com) | ||
* @since 2.1 | ||
*/ | ||
public class ModuleMainClassAdapter extends ClassVisitor implements Opcodes { | ||
private final String mainClass_; | ||
|
||
/** | ||
* Performs the actual modification of the module info class's bytecode. | ||
* | ||
* @param origBytes the bytes of the module class that should be modified | ||
* @param mainClass the main class of the module | ||
* @return the modified bytes | ||
* @since 2.1 | ||
*/ | ||
public static byte[] addModuleMainClassToBytes(byte[] origBytes, String mainClass) { | ||
var cw = new ClassWriter(0); | ||
new ClassReader(origBytes).accept(new ModuleMainClassAdapter(mainClass, cw), 0); | ||
return cw.toByteArray(); | ||
} | ||
|
||
private ModuleMainClassAdapter(String mainClass, ClassVisitor writer) { | ||
super(ASM9, writer); | ||
mainClass_ = mainClass.replace('.', '/'); | ||
} | ||
|
||
@Override | ||
public ModuleVisitor visitModule(String name, int access, String version) { | ||
var module_visitor = super.visitModule(name, access, version); | ||
module_visitor.visitMainClass(mainClass_); | ||
return module_visitor; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters