Skip to content

Commit

Permalink
Locate spot to generate TemplateExtensions.java better
Browse files Browse the repository at this point in the history
When locating the folder to generate `TemplateExtension.java` into
during the "generate missing member" Qute quick fix:
- Filter out classpath entries that are in the output (`target`) folder
- Filter out classpath entries that represent test code
- Filter out classpath entries that represent resource folders (folders
  with no Java source files)

Fixes #831

Signed-off-by: David Thompson <davthomp@redhat.com>
  • Loading branch information
datho7561 authored and angelozerr committed Mar 29, 2023
1 parent 30a2d17 commit 8982916
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,22 @@

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.core.Flags;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IField;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
Expand Down Expand Up @@ -378,9 +384,46 @@ private static WorkspaceEdit createNewTemplateExtensionFile(GenerateMissingJavaM

IPackageFragment destPackage = null;
try {

IPath targetFolder = project.getOutputLocation().removeLastSegments(1);

List<IClasspathEntry> sourceClassPathEntries = Stream.of(project.getRawClasspath()) //
.filter(cpe -> {
cpe.getExtraAttributes();
return cpe.getEntryKind() == IClasspathEntry.CPE_SOURCE && !cpe.isTest() && !targetFolder.isPrefixOf(cpe.getPath());
}) //
.toList();
if (sourceClassPathEntries.isEmpty()) {
throw new UnsupportedOperationException(
"Cannot locate source code folder");
}
Optional<IPackageFragmentRoot> sourceFragmentRootOptional = Stream.of(project.getPackageFragmentRoots()) //
.filter(pfr -> {
try {
if (pfr.getKind() == IPackageFragmentRoot.K_BINARY || pfr.getResource() == null) {
return false;
}
return Stream.of(pfr.getChildren()).anyMatch(child -> child.getElementType() == IJavaElement.PACKAGE_FRAGMENT);
} catch (JavaModelException e) {
return false;
}
}) //
.filter(pfr -> {
for (var cpe : sourceClassPathEntries) {
if (cpe.getPath().equals(pfr.getResource().getFullPath())) {
return true;
}
}
return false;
}) //
.findFirst();
if (sourceFragmentRootOptional.isEmpty()) {
throw new UnsupportedOperationException(
"Cannot locate source code folder");
}
IPackageFragmentRoot fragmentRoot = sourceFragmentRootOptional.get();
// TODO: longest substring match of package of original class
// OR: figure out the "correct" one from the groupId?
IPackageFragmentRoot fragmentRoot = project.getPackageFragmentRoots()[0];
destPackage = fragmentRoot.getPackageFragment("");
} catch (JavaModelException e) {
}
Expand Down

0 comments on commit 8982916

Please sign in to comment.