-
Notifications
You must be signed in to change notification settings - Fork 366
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TypeTable
as a substitute for packing whole jars into `META-INF/rew…
…rite/classpath` (#4932)
- Loading branch information
1 parent
11a0924
commit 7661418
Showing
8 changed files
with
876 additions
and
103 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
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
77 changes: 77 additions & 0 deletions
77
rewrite-java/src/main/java/org/openrewrite/java/internal/parser/JavaParserCaller.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,77 @@ | ||
/* | ||
* Copyright 2025 the original author or authors. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.openrewrite.java.internal.parser; | ||
|
||
import org.openrewrite.ExecutionContext; | ||
import org.openrewrite.java.JavaParser; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.util.function.Function; | ||
import java.util.function.Predicate; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* This class is used to find the caller of {@link JavaParser#dependenciesFromResources(ExecutionContext, String...)}, | ||
* which is used to load classpath resources for {@link JavaParser}. | ||
*/ | ||
class JavaParserCaller { | ||
private JavaParserCaller() { | ||
} | ||
|
||
public static Class<?> findCaller() { | ||
Class<?> caller; | ||
try { | ||
// StackWalker is only available in Java 15+, but right now we only use classloader isolated | ||
// recipe instances in Java 17 environments, so we can safely use StackWalker there. | ||
Class<?> options = Class.forName("java.lang.StackWalker$Option"); | ||
Object retainOption = options.getDeclaredField("RETAIN_CLASS_REFERENCE").get(null); | ||
|
||
Class<?> walkerClass = Class.forName("java.lang.StackWalker"); | ||
Method getInstance = walkerClass.getDeclaredMethod("getInstance", options); | ||
Object walker = getInstance.invoke(null, retainOption); | ||
Method getDeclaringClass = Class.forName("java.lang.StackWalker$StackFrame").getDeclaredMethod("getDeclaringClass"); | ||
caller = (Class<?>) walkerClass.getMethod("walk", Function.class).invoke(walker, (Function<Stream<Object>, Object>) s -> s | ||
.map(f -> { | ||
try { | ||
return (Class<?>) getDeclaringClass.invoke(f); | ||
} catch (IllegalAccessException | InvocationTargetException e) { | ||
throw new RuntimeException(e); | ||
} | ||
}) | ||
// Drop anything before the parser or builder class, as well as those classes themselves | ||
.filter(new Predicate<Class<?>>() { | ||
boolean parserOrBuilderFound = false; | ||
|
||
@Override | ||
public boolean test(Class<?> c1) { | ||
if (c1.getName().equals(JavaParser.class.getName()) || | ||
c1.getName().equals(JavaParser.Builder.class.getName())) { | ||
parserOrBuilderFound = true; | ||
return false; | ||
} | ||
return parserOrBuilderFound; | ||
} | ||
}) | ||
.findFirst() | ||
.orElseThrow(() -> new IllegalStateException("Unable to find caller of JavaParser.dependenciesFromResources(..)"))); | ||
} catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException | | ||
NoSuchMethodException | InvocationTargetException e) { | ||
caller = JavaParser.class; | ||
} | ||
return caller; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...te-java/src/main/java/org/openrewrite/java/internal/parser/JavaParserClasspathLoader.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,38 @@ | ||
/* | ||
* Copyright 2025 the original author or authors. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.openrewrite.java.internal.parser; | ||
|
||
import org.jspecify.annotations.Nullable; | ||
import org.openrewrite.ExecutionContext; | ||
import org.openrewrite.java.JavaParser; | ||
|
||
import java.nio.file.Path; | ||
|
||
/** | ||
* Prepares classpath resources for use by {@link JavaParser}. | ||
*/ | ||
public interface JavaParserClasspathLoader { | ||
|
||
/** | ||
* Load a classpath resource. | ||
* | ||
* @param artifactName A descriptor for the classpath resource to load. | ||
* @return The path a JAR or classes directory that is suitable for use | ||
* as a classpath entry in a compilation step. | ||
*/ | ||
@Nullable | ||
Path load(String artifactName); | ||
} |
Oops, something went wrong.