Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions jabel-javac-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ sourceCompatibility = targetCompatibility = 8
dependencies {
compile 'net.bytebuddy:byte-buddy:1.10.8'
compile 'net.bytebuddy:byte-buddy-agent:1.10.8'
compile 'org.apache.maven:maven-plugin-api:3.0'
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.bytebuddy.agent.ByteBuddyAgent;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.dynamic.loading.ClassReloadingStrategy;
import org.apache.maven.plugin.logging.SystemStreamLog;

import javax.annotation.processing.Completion;
import javax.annotation.processing.ProcessingEnvironment;
Expand All @@ -29,7 +30,11 @@
import static net.bytebuddy.matcher.ElementMatchers.takesArguments;

public class JabelJavacProcessor implements Processor {
private static org.apache.maven.plugin.logging.Log log = new SystemStreamLog();

/**
* Set of the all the features we want to force enable in our 8 target
*/
private static final Set<Source.Feature> ENABLED_FEATURES = Stream
.of(
"PRIVATE_SAFE_VARARGS",
Expand Down Expand Up @@ -61,10 +66,19 @@ public class JabelJavacProcessor implements Processor {
.collect(Collectors.toSet());

static {
ByteBuddyAgent.install();
// log that we've started, otherwise if there's certain types of errors, no output is seen at all from Jabel (e.g. errors compiling our code)
// helps for verifying Jabel is being picked up correctly from project settings
log.info("Jabel static initialising ByteBuddy");

ByteBuddyAgent.install();
ByteBuddy byteBuddy = new ByteBuddy();

/**
* Inject our code into the JDK version checks
*
* @see JavacParser#checkSourceLevel
* @see JavaTokenizer#checkSourceLevel
*/
for (Class<?> clazz : Arrays.asList(JavacParser.class, JavaTokenizer.class)) {
byteBuddy
.redefine(clazz)
Expand All @@ -76,19 +90,26 @@ public class JabelJavacProcessor implements Processor {
.load(clazz.getClassLoader(), ClassReloadingStrategy.fromInstalledAgent());
}

/**
* For all the features in {@link #ENABLED_FEATURES}, override the min JDK level, reducing it to 8
*
* @see ENABLED_FEATURES
*/
try {
Field field = Source.Feature.class.getDeclaredField("minLevel");
field.setAccessible(true);

for (Source.Feature feature : ENABLED_FEATURES) {
field.set(feature, Source.JDK8);
// sanity check our code
if (!feature.allowedInSource(Source.JDK8)) {
throw new IllegalStateException(feature.name() + " minLevel instrumentation failed!");
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
log.info("Jabel ByteBuddy initialisation complete");
}

@Override
Expand All @@ -98,7 +119,7 @@ public SourceVersion getSupportedSourceVersion() {

@Override
public void init(ProcessingEnvironment processingEnv) {
System.out.println(
log.debug(
ENABLED_FEATURES.stream()
.map(Enum::name)
.collect(Collectors.joining(
Expand Down