Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
d-kozak committed Dec 30, 2023
1 parent 72f14c1 commit 1b658c7
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
import java.util.function.Consumer;
import java.util.stream.Collectors;

import com.oracle.svm.hosted.prophet.ProphetPlugin;
import org.graalvm.collections.Pair;
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.c.function.CEntryPoint;
Expand Down Expand Up @@ -73,6 +72,7 @@
import com.oracle.svm.hosted.code.CEntryPointData;
import com.oracle.svm.hosted.image.AbstractImage.NativeImageKind;
import com.oracle.svm.hosted.option.HostedOptionParser;
import com.oracle.svm.hosted.prophet.ProphetPlugin;
import com.oracle.svm.util.ClassUtil;
import com.oracle.svm.util.LogUtils;
import com.oracle.svm.util.ModuleSupport;
Expand Down Expand Up @@ -438,17 +438,18 @@ private int buildImage(ImageClassLoader classLoader) {
throw UserError.abort(classLoader.getMainClassNotFoundErrorMessage(className));
}
} catch (ClassNotFoundException ex) {
throw UserError.abort(classLoader.getMainClassNotFoundErrorMessage(className));} catch (UnsupportedClassVersionError ex) {
if (ex.getMessage().contains("compiled by a more recent version of the Java Runtime")) {
throw UserError.abort("Unable to load '%s' due to a Java version mismatch.%n" +
"Please take one of the following actions:%n" +
" 1) Recompile the source files for your application using Java %s, then try running native-image again%n" +
" 2) Use a version of native-image corresponding to the version of Java with which you compiled the source files for your application%n%n" +
"Root cause: %s",
className, Runtime.version().feature(), ex);
} else {
throw UserError.abort(ex.getMessage());
}
throw UserError.abort(classLoader.getMainClassNotFoundErrorMessage(className));
} catch (UnsupportedClassVersionError ex) {
if (ex.getMessage().contains("compiled by a more recent version of the Java Runtime")) {
throw UserError.abort("Unable to load '%s' due to a Java version mismatch.%n" +
"Please take one of the following actions:%n" +
" 1) Recompile the source files for your application using Java %s, then try running native-image again%n" +
" 2) Use a version of native-image corresponding to the version of Java with which you compiled the source files for your application%n%n" +
"Root cause: %s",
className, Runtime.version().feature(), ex);
} else {
throw UserError.abort(ex.getMessage());
}
}
String mainEntryPointName = SubstrateOptions.Method.getValue(parsedHostedOptions);
if (mainEntryPointName.isEmpty()) {
Expand All @@ -464,57 +465,59 @@ private int buildImage(ImageClassLoader classLoader) {
} catch (NoSuchMethodException ignored2) {
Method javaMainMethod;

/*
* If no C-level main method was found, look for a Java-level mainmethod
* and use our wrapper to invoke it.
*/
if ("main".equals(mainEntryPointName) && JavaMainWrapper.instanceMainMethodSupported()) {
// Instance main method only supported for "main" method name
try {
/*
* JDK-8306112: Implementation of JEP 445: Unnamed Classes and
* Instance Main Methods (Preview)
*
* MainMethodFinder will perform all the necessary checks
*/
String mainMethodFinderClassName = JavaVersionUtil.JAVA_SPEC >= 22 ? "jdk.internal.misc.MethodFinder" : "jdk.internal.misc.MainMethodFinder";
Class<?> mainMethodFinder = ReflectionUtil.lookupClass(false, mainMethodFinderClassName);
Method findMainMethod = ReflectionUtil.lookupMethod(mainMethodFinder, "findMainMethod", Class.class);
javaMainMethod = (Method) findMainMethod.invoke(null, mainClass);
} catch (InvocationTargetException ex) {
assert ex.getTargetException() instanceof NoSuchMethodException;throw UserError.abort(ex.getCause(),
"Method '%s.%s' is declared as the main entry point but it can not be found. " +
"Make sure that class '%s' is on the classpath and that non-private " +
"method '%s()' or '%s(String[])'.",
mainClass.getName(),
mainEntryPointName,
mainClass.getName(),
mainEntryPointName,
mainEntryPointName);
}
/*
* If no C-level main method was found, look for a Java-level mainmethod
* and use our wrapper to invoke it.
*/
if ("main".equals(mainEntryPointName) && JavaMainWrapper.instanceMainMethodSupported()) {
// Instance main method only supported for "main" method name
try {
/*
* JDK-8306112: Implementation of JEP 445: Unnamed Classes and
* Instance Main Methods (Preview)
*
* MainMethodFinder will perform all the necessary checks
*/
String mainMethodFinderClassName = JavaVersionUtil.JAVA_SPEC >= 22 ? "jdk.internal.misc.MethodFinder" : "jdk.internal.misc.MainMethodFinder";
Class<?> mainMethodFinder = ReflectionUtil.lookupClass(false, mainMethodFinderClassName);
Method findMainMethod = ReflectionUtil.lookupMethod(mainMethodFinder, "findMainMethod", Class.class);
javaMainMethod = (Method) findMainMethod.invoke(null, mainClass);
} catch (InvocationTargetException ex) {
assert ex.getTargetException() instanceof NoSuchMethodException;
throw UserError.abort(ex.getCause(),
"Method '%s.%s' is declared as the main entry point but it can not be found. " +
"Make sure that class '%s' is on the classpath and that non-private " +
"method '%s()' or '%s(String[])'.",
mainClass.getName(),
mainEntryPointName,
mainClass.getName(),
mainEntryPointName,
mainEntryPointName);
}
} else {
try {
javaMainMethod = ReflectionUtil.lookupMethod(mainClass, mainEntryPointName, String[].class);
final int mainMethodModifiers = javaMainMethod.getModifiers();
if (!Modifier.isStatic(mainMethodModifiers)) {
throw UserError.abort("Java main method '%s.%s(String[])' is not static.", mainClass.getName(), mainEntryPointName);
javaMainMethod = ReflectionUtil.lookupMethod(mainClass, mainEntryPointName, String[].class);
final int mainMethodModifiers = javaMainMethod.getModifiers();
if (!Modifier.isStatic(mainMethodModifiers)) {
throw UserError.abort("Java main method '%s.%s(String[])' is not static.", mainClass.getName(), mainEntryPointName);
}
if (!Modifier.isPublic(mainMethodModifiers)) {
throw UserError.abort("Java main method '%s.%s(String[])' is not public.", mainClass.getName(), mainEntryPointName);
}
} catch (ReflectionUtilError ex) {
throw UserError.abort(ex.getCause(),
"Method '%s.%s' is declared as the main entry point but it can not be found. " +
"Make sure that class '%s' is on the classpath and that method '%s(String[])' exists in that class.",
mainClass.getName(),
mainEntryPointName,
mainClass.getName(),
mainEntryPointName);
}
}
if (!Modifier.isPublic(mainMethodModifiers)) {
throw UserError.abort("Java main method '%s.%s(String[])' is not public.", mainClass.getName(), mainEntryPointName);}
} catch (ReflectionUtilError ex) {
throw UserError.abort(ex.getCause(),
"Method '%s.%s' is declared as the main entry point but it can not be found. " +
"Make sure that class '%s' is on the classpath and that method '%s(String[])' exists in that class.",
mainClass.getName(),
mainEntryPointName,
mainClass.getName(),
mainEntryPointName);
}
}

if (javaMainMethod.getReturnType() != void.class) {
throw UserError.abort("Java main method '%s.%s(%s)' does not have the return type 'void'.", mainClass.getName(), mainEntryPointName,
javaMainMethod.getParameterCount() == 1 ? "String[]" : "");
if (javaMainMethod.getReturnType() != void.class) {
throw UserError.abort("Java main method '%s.%s(%s)' does not have the return type 'void'.", mainClass.getName(), mainEntryPointName,
javaMainMethod.getParameterCount() == 1 ? "String[]" : "");
}
javaMainSupport = createJavaMainSupport(javaMainMethod, classLoader);
mainEntryPoint = getMainEntryMethod(classLoader);
Expand All @@ -528,9 +531,6 @@ private int buildImage(ImageClassLoader classLoader) {
reporter.printStart(imageName, imageKind);
}

int maxConcurrentThreads = NativeImageOptions.getMaximumNumberOfConcurrentThreads(parsedHostedOptions);
analysisExecutor = NativeImagePointsToAnalysis.createExecutor(debug, NativeImageOptions.getMaximumNumberOfAnalysisThreads(parsedHostedOptions));
compilationExecutor = NativeImagePointsToAnalysis.createExecutor(debug, maxConcurrentThreads);
generator = createImageGenerator(classLoader, optionParser, mainEntryPointData, reporter);
generator.run(entryPoints, javaMainSupport, imageName, imageKind, SubstitutionProcessor.IDENTITY, optionParser.getRuntimeOptionNames(), timerCollection);
wasSuccessfulBuild = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
package com.oracle.svm.hosted.prophet;

import java.io.FileWriter;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.oracle.graal.pointsto.meta.AnalysisMetaAccess;
import com.oracle.graal.pointsto.meta.AnalysisMethod;
import com.oracle.graal.pointsto.meta.AnalysisType;
Expand All @@ -12,25 +23,15 @@
import com.oracle.svm.hosted.prophet.model.Field;
import com.oracle.svm.hosted.prophet.model.Module;
import com.oracle.svm.hosted.prophet.model.Name;
import org.graalvm.compiler.graph.Node;
import org.graalvm.compiler.graph.NodeInputList;
import org.graalvm.compiler.nodes.CallTargetNode;
import org.graalvm.compiler.nodes.Invoke;
import org.graalvm.compiler.nodes.InvokeWithExceptionNode;
import org.graalvm.compiler.nodes.StructuredGraph;
import org.graalvm.compiler.nodes.ValueNode;
import org.graalvm.compiler.options.Option;

import java.io.FileWriter;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import jdk.graal.compiler.graph.Node;
import jdk.graal.compiler.graph.NodeInputList;
import jdk.graal.compiler.nodes.CallTargetNode;
import jdk.graal.compiler.nodes.Invoke;
import jdk.graal.compiler.nodes.InvokeWithExceptionNode;
import jdk.graal.compiler.nodes.StructuredGraph;
import jdk.graal.compiler.nodes.ValueNode;
import jdk.graal.compiler.options.Option;

// todo move to a separate module for a faster compilation ?
public class ProphetPlugin {
Expand Down Expand Up @@ -135,7 +136,7 @@ private Module processClasses(List<Class<?>> classes) {
private void processMethods(Class<?> clazz) {
AnalysisType analysisType = metaAccess.lookupJavaType(clazz);
try {
for (AnalysisMethod method : analysisType.getDeclaredMethods()) {
for (AnalysisMethod method : ((AnalysisMethod[]) analysisType.getDeclaredMethods())) {
try {
StructuredGraph decodedGraph = ReachabilityAnalysisMethod.getDecodedGraph(bb, method);
for (Node node : decodedGraph.getNodes()) {
Expand Down

0 comments on commit 1b658c7

Please sign in to comment.