Skip to content

Commit

Permalink
chore: Support aspectj-maven-plugin using AspectJ idea plugin #7
Browse files Browse the repository at this point in the history
  • Loading branch information
gmyasoedov committed Apr 12, 2024
1 parent fa2cfd9 commit 29e4e86
Show file tree
Hide file tree
Showing 20 changed files with 589 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,54 @@
import java.util.Collection;
import java.util.Objects;

import static java.util.Collections.emptyList;

public class CompilerData {
private final LanguageLevel sourceLevel;
private final LanguageLevel targetLevel;
private final LanguageLevel testSourceLevel;
private final LanguageLevel testTargetLevel;
private final Collection<String> annotationProcessorPaths;
private final Collection<String> arguments;
private final Collection<String> pluginSpecificArguments;

public CompilerData(@NotNull LanguageLevel sourceLevel,
@NotNull LanguageLevel targetLevel,
@NotNull LanguageLevel testSourceLevel,
@NotNull LanguageLevel testTargetLevel,
@NotNull Collection<String> annotationProcessorPaths,
@NotNull Collection<String> arguments) {
@NotNull Collection<String> arguments,
@NotNull Collection<String> pluginSpecificArguments) {
this.sourceLevel = Objects.requireNonNull(sourceLevel);
this.targetLevel = Objects.requireNonNull(targetLevel);
this.testSourceLevel = Objects.requireNonNull(testSourceLevel);
this.testTargetLevel = Objects.requireNonNull(testTargetLevel);
this.annotationProcessorPaths = Objects.requireNonNull(annotationProcessorPaths);
this.arguments = Objects.requireNonNull(arguments);
this.pluginSpecificArguments = Objects.requireNonNull(pluginSpecificArguments);
}

public CompilerData(@NotNull LanguageLevel sourceLevel,
@NotNull LanguageLevel targetLevel,
@NotNull LanguageLevel testSourceLevel,
@NotNull LanguageLevel testTargetLevel,
@NotNull Collection<String> annotationProcessorPaths,
@NotNull Collection<String> arguments) {
this(sourceLevel, targetLevel, testSourceLevel, testTargetLevel,
annotationProcessorPaths, arguments, emptyList());
}

public CompilerData(@NotNull LanguageLevel defaultLevel,
@NotNull Collection<String> annotationProcessorPaths,
@NotNull Collection<String> arguments) {
this(defaultLevel, defaultLevel, defaultLevel, defaultLevel, annotationProcessorPaths, arguments);
this(defaultLevel, defaultLevel, defaultLevel, defaultLevel, annotationProcessorPaths, arguments, emptyList());
}

public CompilerData(@NotNull LanguageLevel defaultLevel,
@NotNull Collection<String> annotationProcessorPaths,
@NotNull Collection<String> arguments,
@NotNull Collection<String> pluginSpecificArguments) {
this(defaultLevel, defaultLevel, defaultLevel, defaultLevel, annotationProcessorPaths, arguments, pluginSpecificArguments);
}

@NotNull
Expand Down Expand Up @@ -64,6 +86,10 @@ public Collection<String> getArguments() {
return arguments;
}

public Collection<String> getPluginSpecificArguments() {
return pluginSpecificArguments;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ MainJavaCompilerData getJavaCompilerData(@NotNull MavenProject project,


@Nullable
String getAnnotationProcessorTagName();
default String getAnnotationProcessorTagName() {
return null;
}

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package ru.rzn.gmyasoedov.gmaven.plugins;

import org.jdom.Element;
import ru.rzn.gmyasoedov.gmaven.util.MavenArtifactInfo;
import ru.rzn.gmyasoedov.gmaven.utils.MavenArtifactUtil;
import ru.rzn.gmyasoedov.gmaven.utils.MavenJDOMUtil;
import ru.rzn.gmyasoedov.gmaven.utils.MavenUtils;

import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.*;

public class MavenPluginDescription {
private static final String UNKNOWN = "<unknown>";
Expand All @@ -17,13 +17,15 @@ public class MavenPluginDescription {
private final String myGoalPrefix;
private final Map<String, Mojo> myMojos;
private final Map<String, String> myParams = new HashMap<>();
private final Map<String, MavenArtifactInfo> dependencyByGA;

public MavenPluginDescription(Element plugin) {
public MavenPluginDescription(Element plugin, boolean loadDependencies) {
myGroupId = MavenJDOMUtil.findChildValueByPath(plugin, "groupId", "unknown");
myArtifactId = MavenJDOMUtil.findChildValueByPath(plugin, "artifactId", "unknown");
myVersion = MavenJDOMUtil.findChildValueByPath(plugin, "version", "unknown");
myGoalPrefix = MavenJDOMUtil.findChildValueByPath(plugin, "goalPrefix", "unknown");
myMojos = readMojos(plugin);
dependencyByGA = readDependencies(plugin, loadDependencies);
}

private Map<String, Mojo> readMojos(Element plugin) {
Expand All @@ -46,6 +48,21 @@ private Map<String, Mojo> readMojos(Element plugin) {
return result;
}

private Map<String, MavenArtifactInfo> readDependencies(Element plugin, boolean loadDependencies) {
if (!loadDependencies) return Collections.emptyMap();
Map<String, MavenArtifactInfo> result = new TreeMap<>();
for (Element each : MavenJDOMUtil.findChildrenByPath(plugin, "dependencies", "dependency")) {
var artifactId = each.getChildTextTrim(MavenArtifactUtil.ARTIFACT_ID);
var groupId = each.getChildTextTrim(MavenArtifactUtil.GROUP_ID);
var version = each.getChildTextTrim(MavenArtifactUtil.VERSION);
if (artifactId == null || groupId == null || version == null) continue;

String ga = MavenUtils.toGAString(groupId, artifactId);
result.put(ga, new MavenArtifactInfo(ga, groupId, artifactId, version));
}
return result;
}

public String getGroupId() {
return myGroupId;
}
Expand All @@ -70,6 +87,10 @@ public Mojo findMojo(String name) {
return myMojos.get(name);
}

public MavenArtifactInfo findDependency(String artifactId) {
return dependencyByGA.values().stream().filter(it -> it.getA().equals(artifactId)).findFirst().orElse(null);
}

public Map<String, String> getMyParams() {
return myParams;
}
Expand Down Expand Up @@ -101,7 +122,7 @@ public String getQualifiedGoal() {
}

private static void append(StringBuilder builder, String part) {
if (builder.length() != 0) builder.append(':');
if (!builder.isEmpty()) builder.append(':');
builder.append(part == null ? UNKNOWN : part);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
public final class MavenArtifactUtil {
public static final String GROUP_ID = "groupId";
public static final String VERSION = "version";
public static final String ARTIFACT_ID="artifactId";
public static final String ARTIFACT_ID = "artifactId";
public static final String RELATIVE_PATH = "relativePath";
public static final String PARENT = "parent";
public static final String MODULE = "module";
Expand Down Expand Up @@ -56,13 +56,20 @@ public static void clearPluginDescriptorCache() {

@Nullable
public static MavenPluginDescription readPluginDescriptor(Path localRepository, MavenPlugin plugin) {
return readPluginDescriptor(localRepository, plugin, false, true);
}

@Nullable
public static MavenPluginDescription readPluginDescriptor(
Path localRepository, MavenPlugin plugin, boolean loadDependencies, boolean useCache
) {
MavenPluginDescription description = PLUGIN_DESCRIPTOR_CACHE.get(plugin);
if (description != null) {
if (description != null && useCache) {
return description;
}
Path path = getArtifactNioPath(localRepository, plugin.getGroupId(),
plugin.getArtifactId(), plugin.getVersion(), "jar");
MavenPluginDescription pluginDescriptor = getPluginDescriptor(path);
MavenPluginDescription pluginDescriptor = getPluginDescriptor(path, loadDependencies);
if (pluginDescriptor != null) {
PLUGIN_DESCRIPTOR_CACHE.putIfAbsent(plugin, pluginDescriptor);
}
Expand All @@ -71,12 +78,20 @@ public static MavenPluginDescription readPluginDescriptor(Path localRepository,

@NotNull
public static Path getArtifactNioPathPom(@NotNull Path localRepository,
@NotNull String groupId,
@NotNull String artifactId,
@NotNull String version) {
@NotNull String groupId,
@NotNull String artifactId,
@NotNull String version) {
return getArtifactNioPath(localRepository, groupId, artifactId, version, "pom", null);
}

@NotNull
public static Path getArtifactNioPathJar(@NotNull Path localRepository,
@NotNull String groupId,
@NotNull String artifactId,
@NotNull String version) {
return getArtifactNioPath(localRepository, groupId, artifactId, version, "jar", null);
}

@NotNull
public static Path getArtifactNioPath(@NotNull Path localRepository,
@NotNull String groupId,
Expand Down Expand Up @@ -130,7 +145,7 @@ private static String resolveVersion(Path pluginDir) {


@Nullable
private static MavenPluginDescription getPluginDescriptor(Path file) {
private static MavenPluginDescription getPluginDescriptor(Path file, boolean loadDependencies) {
try {
if (!Files.exists(file)) return null;

Expand All @@ -146,7 +161,7 @@ private static MavenPluginDescription getPluginDescriptor(Path file) {
byte[] bytes = FileUtil.loadBytes(is);
try {
Element pluginDescriptionElement = JDOMUtil.load(bytes);
return new MavenPluginDescription(pluginDescriptionElement);
return new MavenPluginDescription(pluginDescriptionElement, loadDependencies);
} catch (Exception e) {
MavenLog.LOG.error("repository.plugin.corrupt " + file, e);
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import com.intellij.psi.xml.XmlTag
import ru.rzn.gmyasoedov.gmaven.GMavenConstants
import ru.rzn.gmyasoedov.gmaven.settings.MavenSettings
import ru.rzn.gmyasoedov.gmaven.util.CachedModuleDataService
import ru.rzn.gmyasoedov.gmaven.util.MavenCentralArtifactInfo
import ru.rzn.gmyasoedov.gmaven.util.MavenArtifactInfo
import ru.rzn.gmyasoedov.gmaven.utils.MavenArtifactUtil
import java.nio.file.Path
import java.util.*
Expand Down Expand Up @@ -94,7 +94,7 @@ object XmlPsiUtil {
fillProperties(parentXmlFile, propertiesMap, localRepos, deepCount + 1)
}

fun getDependencyManagementLibraryCache(xmlFile: PsiFile): Set<MavenCentralArtifactInfo> {
fun getDependencyManagementLibraryCache(xmlFile: PsiFile): Set<MavenArtifactInfo> {
return CachedValuesManager.getManager(xmlFile.project).getCachedValue(xmlFile) {
CachedValueProvider.Result
.create(
Expand All @@ -104,10 +104,10 @@ object XmlPsiUtil {
}
}

private fun getDependencyManagementLibrary(xmlFile: PsiFile): Set<MavenCentralArtifactInfo> {
private fun getDependencyManagementLibrary(xmlFile: PsiFile): Set<MavenArtifactInfo> {
if (xmlFile !is XmlFile) return emptySet()
val repos = getLocalRepos(xmlFile)
val librarySet = mutableSetOf<MavenCentralArtifactInfo>()
val librarySet = mutableSetOf<MavenArtifactInfo>()
fillDependencyManagement(xmlFile, librarySet, mutableMapOf(), repos, 0)
return librarySet
}
Expand All @@ -117,7 +117,7 @@ object XmlPsiUtil {
}

private fun fillDependencyManagement(
xmlFile: XmlFile, librarySet: MutableSet<MavenCentralArtifactInfo>,
xmlFile: XmlFile, librarySet: MutableSet<MavenArtifactInfo>,
propertiesMap: MutableMap<String, String>, localRepos: List<String>, deepCount: Int = 0
) {
if (deepCount > 500) return
Expand Down Expand Up @@ -153,11 +153,11 @@ object XmlPsiUtil {
fillDependencyManagement(parentXmlFile, librarySet, propertiesMap, localRepos, deepCount + 1)
}

private fun dependencyToLibrary(xmlTag: XmlTag): MavenCentralArtifactInfo? {
private fun dependencyToLibrary(xmlTag: XmlTag): MavenArtifactInfo? {
val groupId = xmlTag.getSubTagText(MavenArtifactUtil.GROUP_ID) ?: return null
val artifactId = xmlTag.getSubTagText(MavenArtifactUtil.ARTIFACT_ID) ?: return null
val version = xmlTag.getSubTagText(MavenArtifactUtil.VERSION) ?: return null
val id = "$groupId:$artifactId:$version"
return MavenCentralArtifactInfo(id, groupId, artifactId, version)
return MavenArtifactInfo(id, groupId, artifactId, version)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import org.jetbrains.concurrency.Promise
import ru.rzn.gmyasoedov.gmaven.GMavenConstants.IDEA_PSI_EDIT_TOKEN
import ru.rzn.gmyasoedov.gmaven.dom.XmlPsiUtil
import ru.rzn.gmyasoedov.gmaven.util.CachedModuleDataService
import ru.rzn.gmyasoedov.gmaven.util.MavenCentralArtifactInfo
import ru.rzn.gmyasoedov.gmaven.util.MavenArtifactInfo
import ru.rzn.gmyasoedov.gmaven.util.MavenCentralClient.find
import ru.rzn.gmyasoedov.gmaven.util.MavenCentralClient.findArtifact
import ru.rzn.gmyasoedov.gmaven.utils.MavenArtifactUtil.*
Expand Down Expand Up @@ -124,7 +124,7 @@ private class VersionContributor(val artifactId: String, val groupId: String) :
Consumer<CompletionResultSet> {

override fun accept(result: CompletionResultSet) {
val promise = AsyncPromise<List<MavenCentralArtifactInfo>>()
val promise = AsyncPromise<List<MavenArtifactInfo>>()
ApplicationManager.getApplication().executeOnPooledThread {
promise.setResult(find(groupId, artifactId))
}
Expand Down Expand Up @@ -174,16 +174,16 @@ private class GAVContributor(val artifactIdTag: XmlTag, val parentXmlTag: XmlTag
setLookupResult(artifactInfoList, result)
}

private fun asyncPromise(queryText: String, groupId: String?): AsyncPromise<List<MavenCentralArtifactInfo>>? {
private fun asyncPromise(queryText: String, groupId: String?): AsyncPromise<List<MavenArtifactInfo>>? {
if (!Registry.`is`("gmaven.search.artifact.maven.central")) return null
val promise = AsyncPromise<List<MavenCentralArtifactInfo>>()
val promise = AsyncPromise<List<MavenArtifactInfo>>()
ApplicationManager.getApplication()
.executeOnPooledThread { promise.setResult(findArtifact(queryText, groupId)) }
return promise
}

private fun setLookupResult(
artifactInfoList: Collection<MavenCentralArtifactInfo>, result: CompletionResultSet
artifactInfoList: Collection<MavenArtifactInfo>, result: CompletionResultSet
) {
artifactInfoList.forEach {
result.addElement(
Expand All @@ -194,16 +194,16 @@ private class GAVContributor(val artifactIdTag: XmlTag, val parentXmlTag: XmlTag
}
}

private fun findInModule(artifactIdTag: XmlTag, query: String): List<MavenCentralArtifactInfo> {
private fun findInModule(artifactIdTag: XmlTag, query: String): List<MavenArtifactInfo> {
if (query.length < 2) return emptyList()
return CachedModuleDataService
.getDataHolder(artifactIdTag.project).modules.asSequence()
.filter { it.artifactId.contains(query, true) }
.map { MavenCentralArtifactInfo(it.groupId + ":" + it.artifactId, it.groupId, it.artifactId, it.version) }
.map { MavenArtifactInfo(it.groupId + ":" + it.artifactId, it.groupId, it.artifactId, it.version) }
.toList()
}

private fun filterArtifact(groupId: String?, artifactId: String, data: MavenCentralArtifactInfo): Boolean {
private fun filterArtifact(groupId: String?, artifactId: String, data: MavenArtifactInfo): Boolean {
if (artifactId.length < 2) return false
return if (groupId == null || groupId.length < 3) {
data.a.contains(artifactId)
Expand All @@ -216,7 +216,7 @@ private class GAVContributor(val artifactIdTag: XmlTag, val parentXmlTag: XmlTag
private object GAVInsertHandler : InsertHandler<LookupElement> {

override fun handleInsert(context: InsertionContext, item: LookupElement) {
val artifactInfo: MavenCentralArtifactInfo = item.`object` as? MavenCentralArtifactInfo ?: return
val artifactInfo: MavenArtifactInfo = item.`object` as? MavenArtifactInfo ?: return
val contextFile = context.file as? XmlFile ?: return
val element = contextFile.findElementAt(context.startOffset)
val xmlTag = PsiTreeUtil.getParentOfType(element, XmlTag::class.java) ?: return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,6 @@ class ApacheMavenCompilerPlugin : MavenCompilerFullImportPlugin {
return CompilerData(source, target, testSource, testTarget, emptyList(), emptyList())
}

private fun getLanguageLevel(value: Any?): LanguageLevel? {
return if (value is String) LanguageLevel.parse(value) else null
}

private fun fillCompilerPropFromMavenProjectProperies(
compilerProp: CompilerProp,
project: MavenProject
Expand Down
Loading

0 comments on commit 29e4e86

Please sign in to comment.