Skip to content

Commit

Permalink
[no ci] Initial work on ASM visitor-based processor
Browse files Browse the repository at this point in the history
  • Loading branch information
lukebemish committed Nov 20, 2023
1 parent c691485 commit 32e03c1
Show file tree
Hide file tree
Showing 24 changed files with 511 additions and 34 deletions.
6 changes: 4 additions & 2 deletions buildSrc/src/main/groovy/opensesame.conventions.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ dependencies {
annotationProcessor(libs.autoservice)
compileOnly(libs.autoservice)

testImplementation libs.junit.api
testRuntimeOnly libs.junit.engine
testImplementation libs.junit.api

testImplementation project(':testtargets')
testModuleClasspath project(':testtargets')
Expand Down Expand Up @@ -101,7 +101,9 @@ tasks.named('check') {
}

processResources {
from rootProject.file('LICENSE')
from(rootProject.file('LICENSE')) {
into 'META-INF'
}
}

jar {
Expand Down
64 changes: 61 additions & 3 deletions compile/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,70 @@ plugins {
java.withSourcesJar()
java.withJavadocJar()

sourceSets {
asm {}
}

java.registerFeature('asm') {
usingSourceSet(sourceSets.asm)
withSourcesJar()
withJavadocJar()
}

configurations {
testSource {
canBeResolved = true
}
}

dependencies {
implementation project(':opensesame-core')
api project(':opensesame-core')
asmImplementation libs.asm
asmImplementation project
asmCompileOnly libs.jetbrains.annotations

testImplementation libs.asm

testSource project(path: ':testtargets', configuration: 'testSource')
}

tasks.named('compileJava', JavaCompile).configure {
it.options.compilerArgs.add '-Xlint:-opens'
def tempClassesDir = layout.buildDirectory.dir("tempClasses/compileJava")

tasks.named('compileTestJava', JavaCompile).configure {
dependsOn(configurations.testSource)
source(configurations.testSource)
destinationDirectory.set(tempClassesDir)
}

tasks.register('processTestClasses', JavaExec) {
dependsOn compileTestJava
dependsOn configurations.asmRuntimeClasspath
inputs.dir(tempClassesDir)
inputs.files(sourceSets.asm.runtimeClasspath)
outputs.dir(sourceSets.test.output.classesDirs)
classpath = sourceSets.asm.runtimeClasspath
mainClass.set 'dev.lukebemish.opensesame.compile.asm.Processor'
args = compileTestJava.outputs.files.files.collectMany { [it.canonicalPath, it.canonicalPath] }
}

tasks.testClasses.dependsOn processTestClasses

processAsmResources {
from(rootProject.file('LICENSE')) {
into 'META-INF'
}
}

asmJar {
manifest {
attributes([
'Specification-Version' : rootProject.version,
'Implementation-Commit-Time': managedVersioning.timestamp.get(),
'Implementation-Commit': managedVersioning.hash.get(),
'Automatic-Module-Name' : 'dev.lukebemish.opensesame.compile.asm',
'FMLModType': 'LIBRARY'
])
}
}

publishing {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package dev.lukebemish.opensesame.compile.javac;
package dev.lukebemish.opensesame.compile.asm;

import dev.lukebemish.opensesame.compile.ConDynUtils;
import dev.lukebemish.opensesame.compile.TypeProvider;
import org.objectweb.asm.ConstantDynamic;
import org.objectweb.asm.Handle;
import org.objectweb.asm.Type;

class ASMTypeProvider implements TypeProvider<Type, ConstantDynamic, Handle> {
public class ASMTypeProvider implements TypeProvider<Type, ConstantDynamic, Handle> {
public static final ASMTypeProvider INSTANCE = new ASMTypeProvider();
public static final ConDynUtils<Type, ConstantDynamic, Handle> CON_DYN_UTILS = new ConDynUtils<>(INSTANCE);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package dev.lukebemish.opensesame.compile.asm;

import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class Processor {
public static void main(String[] args) {
if ((~args.length & 1) != 1) {
System.err.println("Usage: java dev.lukebemish.opensesame.compile.asm.Processor <input> <output> <input> <output> ...");
System.exit(1);
}
for (int i = 0; i < args.length; i += 2) {
var input = Path.of(args[0]);
var output = Path.of(args[1]);
if (Files.isRegularFile(input)) {
try {
processFile(input, output);
} catch (IOException e) {
throw new RuntimeException(e);
}
} else {
try (var paths = Files.walk(input)) {
paths.filter(Files::isRegularFile).forEach(file -> {
try {
var relative = input.relativize(file);
var out = output.resolve(relative);
Files.createDirectories(out.getParent());
processFile(file, out);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}

public static void processFile(Path file, Path out) throws IOException {
try (var inputStream = Files.newInputStream(file)) {
ClassReader reader = new ClassReader(inputStream);
ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
reader.accept(new VisitingOpenProcessor(writer, VisitingOpenProcessor.ANNOTATIONS), 0);
Files.write(out, writer.toByteArray());
}
}
}
Loading

0 comments on commit 32e03c1

Please sign in to comment.