Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
xxDark committed May 28, 2023
0 parents commit 2e1aaea
Show file tree
Hide file tree
Showing 20 changed files with 863 additions and 0 deletions.
45 changes: 45 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
.gradle
build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/

### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
out/
!**/src/main/**/out/
!**/src/test/**/out/

### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/

### VS Code ###
.vscode/

### Mac OS ###
.DS_Store
.run

.idea
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 xxDark

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# better-repeatable

Eliminates need of using container to group repeatable annotations together.
Once the compiler plugin sees any annotation that is marked with `dev.xdark.betterrepeatable.Repetable`, any use of `java.lang.annotation.Repeatable` for the place where the annotation is used (class, field, method) will be ignored and annotations will be dumped as-is, in preserved order.

At the moment, IntelliJ will complain about duplicate annotations, I plan on fixing it with plugin for it in the future.
Currently only JDK 8 is supported, due to large amount of changes in javac internals on higher versions.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package dev.xdark.betterrepeatable;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Repeatable {
}
38 changes: 38 additions & 0 deletions better-repeatable-gradle/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
plugins {
id 'java-gradle-plugin'
}

repositories {
mavenCentral()
}

configurations {
shade
implementation.extendsFrom shade
}

dependencies {
shade asm
shade project(':better-repeatable-javac')
}

jar {
from { configurations.shade.collect { zipTree(it) } }
}

// TODO move me
jar {
manifest {
attributes 'Can-Retransform-Classes': 'true',
'Agent-Class': 'dev.xdark.betterrepeatable.InstrumentationAgent'
}
}

gradlePlugin {
plugins {
betterRepeatable {
id = 'dev.xdark.betterrepeatable'
implementationClass = 'dev.xdark.betterrepeatable.GradlePlugin'
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package dev.xdark.betterrepeatable;

import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.UnknownDomainObjectException;
import org.gradle.api.file.ConfigurableFileCollection;
import org.gradle.api.file.FileCollection;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.tasks.compile.CompileOptions;
import org.gradle.api.tasks.compile.JavaCompile;
import org.gradle.jvm.toolchain.JavaCompiler;
import org.gradle.jvm.toolchain.JavaLanguageVersion;

import java.io.File;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

public class GradlePlugin implements Plugin<Project> {

@Override
public void apply(Project project) {
File pluginPath = findPluginPath(project).toFile();
ObjectFactory objects = project.getObjects();
JavaLanguageVersion jdk9Version = JavaLanguageVersion.of(9);
for (JavaCompile compileJava : project.getTasks().withType(JavaCompile.class)) {
compileJava.doFirst("better-repeatable inject", __ -> {
CompileOptions options = compileJava.getOptions();
ConfigurableFileCollection newCollection = objects.fileCollection();
FileCollection collection = options.getAnnotationProcessorPath();
if (collection != null) {
newCollection = newCollection.from(collection);
}
options.setAnnotationProcessorPath(newCollection.from(pluginPath));
JavaCompiler compiler = compileJava.getJavaCompiler().getOrNull();
List<String> args = options.getCompilerArgs();
args.add("-Xplugin:BetterRepeatable");
if (compiler != null) {
JavaLanguageVersion version = compiler.getMetadata().getLanguageVersion();
if (version.canCompileOrRun(jdk9Version)) {
args.add("-Djdk.attach.allowAttachSelf=true");
// In light of recent events...
args.add("-XX:+EnableDynamicAgentLoading");
// TODO do we need to fork?
options.setFork(true);
}
}
});
}
}

private static Path findPluginPath(Project project) {
try {
Object o = project.getExtensions().getByName("better_repeatable_plugin");
if (o instanceof Path) {
return (Path) o;
} else if (o instanceof File) {
return ((File) o).toPath();
}
} catch (UnknownDomainObjectException ignored) {
}
try {
Class<?> c = Class.forName("dev.xdark.betterrepeatable.JavacPlugin");
return Paths.get(c.getProtectionDomain().getCodeSource().getLocation().toURI());
} catch (ClassNotFoundException ignored) {
} catch (URISyntaxException e) {
throw new IllegalStateException("Cannot get path to plugin jar", e);
}
throw new IllegalStateException("Cannot find plugin path for project " + project);
}
}
15 changes: 15 additions & 0 deletions better-repeatable-javac/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
repositories {
mavenCentral()
}

dependencies {
implementation asm
compileOnly files("${System.getProperty('java.home')}/../lib/tools.jar")
}

jar {
manifest {
attributes 'Can-Retransform-Classes': 'true',
'Agent-Class': 'dev.xdark.betterrepeatable.InstrumentationAgent'
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package dev.xdark.betterrepeatable;

import com.sun.codemodel.internal.JOp;

import javax.swing.*;
import java.lang.instrument.Instrumentation;

public final class InstrumentationAgent {

public static final Object LOCK = new Object();
public static volatile Instrumentation instrumentation;

public static void agentmain(String args, Instrumentation instrumentation) {
synchronized (InstrumentationAgent.LOCK) {
InstrumentationAgent.instrumentation = instrumentation;
InstrumentationAgent.LOCK.notifyAll();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package dev.xdark.betterrepeatable;

import com.sun.source.util.JavacTask;
import com.sun.source.util.Plugin;
import dev.xdark.betterrepeatable.java8.Initializer;

import javax.swing.*;

public final class JavacPlugin implements Plugin {
private static final Object INIT_LOCK = new Object();
private static volatile boolean initialized;

@Override
public String getName() {
return "BetterRepeatable";
}

@Override
public void init(JavacTask task, String... args) {
synchronized (INIT_LOCK) {
if (initialized) return;
try {
Class.forName("java.lang.Module");
throw new UnsupportedOperationException("Not yet supported on Java 9+, internals changed");
} catch (ClassNotFoundException ignored) {
Initializer.init();
initialized = true;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package dev.xdark.betterrepeatable;

import sun.misc.Unsafe;

import java.lang.invoke.MethodHandles;
import java.lang.reflect.Field;

public final class UnsafeUtil {
private static final Unsafe UNSAFE;
private static final MethodHandles.Lookup LOOKUP;

private UnsafeUtil() {
}

public static Unsafe unsafe() {
return UNSAFE;
}

public static MethodHandles.Lookup lookup() {
return LOOKUP;
}

static {
try {
Field f = Unsafe.class.getDeclaredField("theUnsafe");
f.setAccessible(true);
Unsafe u = UNSAFE = (Unsafe) f.get(null);
f = MethodHandles.Lookup.class.getDeclaredField("IMPL_LOOKUP");
MethodHandles.publicLookup();
LOOKUP = (MethodHandles.Lookup) u.getObject(u.staticFieldBase(f), u.staticFieldOffset(f));
} catch (ReflectiveOperationException ex) {
throw new ExceptionInInitializerError(ex);
}
}
}
Loading

0 comments on commit 2e1aaea

Please sign in to comment.