-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 2e1aaea
Showing
20 changed files
with
863 additions
and
0 deletions.
There are no files selected for viewing
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,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 |
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,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. |
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,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.
11 changes: 11 additions & 0 deletions
11
better-repeatable-api/src/main/java/dev/xdark/betterrepeatable/Repeatable.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,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 { | ||
} |
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,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' | ||
} | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
better-repeatable-gradle/src/main/java/dev/xdark/betterrepeatable/GradlePlugin.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,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); | ||
} | ||
} |
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,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' | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
better-repeatable-javac/src/main/java/dev/xdark/betterrepeatable/InstrumentationAgent.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,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(); | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
better-repeatable-javac/src/main/java/dev/xdark/betterrepeatable/JavacPlugin.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,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; | ||
} | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
better-repeatable-javac/src/main/java/dev/xdark/betterrepeatable/UnsafeUtil.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,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); | ||
} | ||
} | ||
} |
Oops, something went wrong.