JavaTypeName.VOID,
JavaExpression.MethodInvocation.InvocationType.VIRTUAL)));
configCheckFailureBody.addStatement(new JavaStatement.ReturnStatement());
final JavaStatement configCheck =
new JavaStatement.IfThenElseStatement(
new JavaExpression.OperatorExpression.Unary(
JavaOperator.LOGICAL_NEGATE,
new JavaExpression.MethodInvocation.Instance(
generatedCodeInfoLocalVar,
"isCompatibleWithCurrentConfiguration",
JavaTypeName.BOOLEAN,
JavaExpression.MethodInvocation.InvocationType.VIRTUAL)),
configCheckFailureBody);
mainMethod.addStatement(configCheck);
////
/// Generate code to obtain the class loader which loaded this main class.
///
/// It is necessary to obtain this class loader and pass it into the execution context and the resource access
/// because the class loader which loaded the CAL Platform classes may be an *ancestor* of the one which loaded
/// the standalone JAR (e.g. the bootstrap class loader), and thus may not have access to the foreign classes
/// and localized resources necessary for the standalone JAR to run.
//
// Code fragment:
//
// ClassLoader classloader = {MainClassName}.class.getClassLoader();
//
final JavaTypeName javaTypeName_ClassLoader = JavaTypeName.make(ClassLoader.class);
final JavaExpression classloaderInit =
new JavaExpression.MethodInvocation.Instance(
new JavaExpression.ClassLiteral(mainClassName),
"getClassLoader",
javaTypeName_ClassLoader,
JavaExpression.MethodInvocation.InvocationType.VIRTUAL);
final JavaExpression.LocalVariable classloaderLocalVar = new JavaExpression.LocalVariable("classloader", javaTypeName_ClassLoader);
final JavaStatement classloaderDecl = new JavaStatement.LocalVariableDeclaration(classloaderLocalVar, classloaderInit);
mainMethod.addStatement(classloaderDecl);
////
/// Generate code to set up the execution context to have access to a standalone-JAR-specific
/// ResourceAccess implementation.
//
// Code fragment:
//
// RTExecutionContext executionContext = new RTExecutionContext(
// new ExecutionContextProperties.Builder().toProperties(),
// new StandaloneRuntimeEnvironment(
// classloader,
// new StandaloneJarResourceAccess(classloader)));
//
final JavaTypeName javaTypeName_ExecutionContextProperties = JavaTypeName.make(ExecutionContextProperties.class);
final JavaTypeName javaTypeName_ResourceAccess = JavaTypeName.make(ResourceAccess.class);
final JavaExpression newRuntimeEnvironment =
new JavaExpression.ClassInstanceCreationExpression(
JavaTypeNames.STANDALONE_RUNTIME_ENVIRONMENT,
new JavaExpression[] {
classloaderLocalVar,
new JavaExpression.ClassInstanceCreationExpression(
JavaTypeName.make(StandaloneJarResourceAccess.class),
classloaderLocalVar,
javaTypeName_ClassLoader)},
new JavaTypeName[] {
javaTypeName_ClassLoader,
javaTypeName_ResourceAccess}
);
final JavaExpression executionContextInit =
new JavaExpression.ClassInstanceCreationExpression(
JavaTypeNames.RTEXECUTION_CONTEXT,
new JavaExpression[] {
new JavaExpression.MethodInvocation.Instance(
new JavaExpression.ClassInstanceCreationExpression(JavaTypeName.make(ExecutionContextProperties.Builder.class)),
"toProperties",
javaTypeName_ExecutionContextProperties,
JavaExpression.MethodInvocation.InvocationType.VIRTUAL),
newRuntimeEnvironment
},
new JavaTypeName[] {
javaTypeName_ExecutionContextProperties,
JavaTypeNames.RUNTIME_ENIVONMENT
});
final JavaExpression.LocalVariable executionContextLocalVar = new JavaExpression.LocalVariable(EXECUTION_CONTEXT_USER_FRIENDLY_NAME, JavaTypeNames.RTEXECUTION_CONTEXT);
final JavaStatement executionContextDecl = new JavaStatement.LocalVariableDeclaration(executionContextLocalVar, executionContextInit, true);
mainMethod.addStatement(executionContextDecl);
////
/// Generate code to run the entry point.