317 references to WriteLine
Aspire.Hosting.CodeGeneration.Java (317)
AtsJavaCodeGenerator.cs (317)
625WriteLine("// Aspire.java - Capability-based Aspire SDK"); 626WriteLine("// GENERATED CODE - DO NOT EDIT"); 627WriteLine(); 628WriteLine("package aspire;"); 629WriteLine(); 634WriteLine("import java.util.ArrayList;"); 635WriteLine("import java.util.HashMap;"); 636WriteLine("import java.util.LinkedHashMap;"); 637WriteLine("import java.util.List;"); 638WriteLine("import java.util.Map;"); 639WriteLine("import java.util.Set;"); 640WriteLine("import java.util.UUID;"); 641WriteLine("import java.util.function.BiFunction;"); 642WriteLine("import java.util.function.Consumer;"); 643WriteLine("import java.util.function.Function;"); 644WriteLine(); 659WriteLine("// ============================================================================"); 660WriteLine("// Enums"); 661WriteLine("// ============================================================================"); 662WriteLine(); 672WriteLine($"/** {enumType.Name} enum. */"); 673WriteLine($"enum {enumName} implements WireValueEnum {{"); 680WriteLine($" {memberName}(\"{member}\"){suffix}"); 682WriteLine(); 683WriteLine(" private final String value;"); 684WriteLine(); 685WriteLine($" {enumName}(String value) {{"); 686WriteLine(" this.value = value;"); 687WriteLine(" }"); 688WriteLine(); 689WriteLine(" public String getValue() { return value; }"); 690WriteLine(); 691WriteLine($" public static {enumName} fromValue(String value) {{"); 692WriteLine($" for ({enumName} e : values()) {{"); 693WriteLine(" if (e.value.equals(value)) return e;"); 694WriteLine(" }"); 695WriteLine(" throw new IllegalArgumentException(\"Unknown value: \" + value);"); 696WriteLine(" }"); 697WriteLine("}"); 698WriteLine(); 709WriteLine("// ============================================================================"); 710WriteLine("// DTOs"); 711WriteLine("// ============================================================================"); 712WriteLine(); 723WriteLine($"/** {dto.Name} DTO. */"); 724WriteLine($"class {dtoName} implements JsonSerializable {{"); 731WriteLine($" private {fieldType} {fieldName};"); 733WriteLine(); 741WriteLine($" public {fieldType} get{methodName}() {{ return {fieldName}; }}"); 742WriteLine($" public void set{methodName}({fieldType} value) {{ this.{fieldName} = value; }}"); 744WriteLine(); 746WriteLine(" @SuppressWarnings(\"unchecked\")"); 747WriteLine($" public static {dtoName} fromMap(Map<String, Object> map) {{"); 748WriteLine($" var value = new {dtoName}();"); 761WriteLine($" var {transportValueName} = map.get(\"{property.Name}\");"); 762WriteLine($" value.set{methodName}({RenderJavaDtoPropertyTransportValueConversion(property.Type, transportValueName, property.IsOptional)});"); 764WriteLine(" return value;"); 765WriteLine(" }"); 766WriteLine(); 769WriteLine(" public Map<String, Object> toMap() {"); 770WriteLine(" Map<String, Object> map = new HashMap<>();"); 780WriteLine($" map.put(\"{property.Name}\", AspireClient.serializeValue({fieldName}));"); 783WriteLine(" return map;"); 784WriteLine(" }"); 786WriteLine("}"); 787WriteLine(); 802WriteLine("// ============================================================================"); 803WriteLine("// Exported Values"); 804WriteLine("// ============================================================================"); 805WriteLine(); 809WriteLine($"final class {name} {{"); 810WriteLine($" private {name}() {{ }}"); 811WriteLine(); 813WriteLine("}"); 814WriteLine(); 831WriteLine($"{indent}/** {valueInfo.Description} */"); 836WriteLine($"{indent}public static final {javaType} {name} = {expression};"); 840WriteLine($"{indent}public static final class {name} {{"); 841WriteLine($"{indent} private {name}() {{ }}"); 842WriteLine(); 844WriteLine($"{indent}}}"); 847WriteLine(); 1038WriteLine("// ============================================================================"); 1039WriteLine("// Options Types"); 1040WriteLine("// ============================================================================"); 1041WriteLine(); 1045WriteLine($"/** Options for {className[..^"Options".Length]}. */"); 1046WriteLine($"final class {className} {{"); 1050WriteLine($" private {MapParameterToJava(parameter)} {parameterName};"); 1052WriteLine(); 1058WriteLine($" public {parameterType} {GetOptionGetterName(parameter)}() {{ return {parameterName}; }}"); 1059WriteLine($" public {className} {parameterName}({parameterType} value) {{"); 1060WriteLine($" this.{parameterName} = value;"); 1061WriteLine(" return this;"); 1062WriteLine(" }"); 1063WriteLine(); 1066WriteLine("}"); 1067WriteLine(); 1204WriteLine($" public {returnType} {methodName}({signature}) {{"); 1207WriteLine($" return {methodName}({string.Join(", ", callArguments)});"); 1211WriteLine($" {methodName}({string.Join(", ", callArguments)});"); 1213WriteLine(" }"); 1214WriteLine(); 1240WriteLine("// ============================================================================"); 1241WriteLine("// Handle Wrappers"); 1242WriteLine("// ============================================================================"); 1243WriteLine(); 1247WriteLine($"/** Wrapper for {handleType.TypeId}. */"); 1248WriteLine($"class {handleType.ClassName} extends {handleType.BaseClassName} {{"); 1249WriteLine($" {handleType.ClassName}(Handle handle, AspireClient client) {{"); 1250WriteLine(" super(handle, client);"); 1251WriteLine(" }"); 1252WriteLine(); 1272WriteLine("}"); 1273WriteLine(); 1280WriteLine(" /** Gets the input with the specified name, or null if no input matches. */"); 1281WriteLine(" public InteractionInput get(String name) {"); 1282WriteLine(" for (var input : toArray()) {"); 1283WriteLine(" if (input.getName() != null && input.getName().equalsIgnoreCase(name)) {"); 1284WriteLine(" return input;"); 1285WriteLine(" }"); 1286WriteLine(" }"); 1287WriteLine(" return null;"); 1288WriteLine(" }"); 1289WriteLine(); 1291WriteLine(" /** Gets the input with the specified name, or throws if no input matches. */"); 1292WriteLine(" public InteractionInput required(String name) {"); 1293WriteLine(" var input = get(name);"); 1294WriteLine(" if (input == null) {"); 1295WriteLine(" throw new IllegalArgumentException(\"no input with name '\" + name + \"' was found\");"); 1296WriteLine(" }"); 1297WriteLine(" return input;"); 1298WriteLine(" }"); 1299WriteLine(); 1301WriteLine(" /** Gets the value of the input with the specified name, or an empty string if no input matches or it has no value. */"); 1302WriteLine(" public String value(String name) {"); 1303WriteLine(" var input = get(name);"); 1304WriteLine(" return input == null || input.getValue() == null ? \"\" : input.getValue();"); 1305WriteLine(" }"); 1306WriteLine(); 1308WriteLine(" /** Gets the value of the input with the specified name, or throws if no input matches. */"); 1309WriteLine(" public String requiredValue(String name) {"); 1310WriteLine(" return required(name).getValue();"); 1311WriteLine(" }"); 1312WriteLine(); 1321WriteLine(" /** Create a new distributed application builder. */"); 1322WriteLine($" public static {builderClassName} CreateBuilder() throws Exception {{"); 1323WriteLine(" return CreateBuilder((String[]) null);"); 1324WriteLine(" }"); 1325WriteLine(); 1326WriteLine(" /** Create a new distributed application builder. */"); 1327WriteLine($" public static {builderClassName} CreateBuilder(String[] args) throws Exception {{"); 1328WriteLine(" CreateBuilderOptions options = new CreateBuilderOptions();"); 1329WriteLine(" if (args != null) {"); 1330WriteLine(" options.setArgs(args);"); 1331WriteLine(" }"); 1332WriteLine(" return CreateBuilder(options);"); 1333WriteLine(" }"); 1334WriteLine(); 1335WriteLine(" /** Create a new distributed application builder. */"); 1336WriteLine($" public static {builderClassName} CreateBuilder(CreateBuilderOptions options) throws Exception {{"); 1337WriteLine(" return Aspire.createBuilder(options);"); 1338WriteLine(" }"); 1339WriteLine(); 1426WriteLine($" public {returnInfo.ReturnType} {methodName}({overloadParameters}) {{"); 1433WriteLine($" return {methodName}({callArguments});"); 1437WriteLine($" {methodName}({callArguments});"); 1439WriteLine(" }"); 1440WriteLine(); 1489WriteLine($" public {returnInfo.ReturnType} {methodName}({overloadParameters}) {{"); 1492WriteLine($" return {methodName}({callArguments});"); 1496WriteLine($" {methodName}({callArguments});"); 1498WriteLine(" }"); 1499WriteLine(); 1534WriteLine($" public {returnInfo.ReturnType} {methodName}({parameterList}) {{"); 1551WriteLine($" return {methodName}({string.Join(", ", callArguments)});"); 1555WriteLine($" {methodName}({string.Join(", ", callArguments)});"); 1557WriteLine(" }"); 1558WriteLine(); 1622WriteLine($" public {returnInfo.ReturnType} {methodName}({overloadParameters}) {{"); 1629WriteLine($" return {methodName}({callArguments}, options);"); 1633WriteLine($" {methodName}({callArguments}, options);"); 1635WriteLine(" }"); 1636WriteLine(); 1647WriteLine($" public {returnInfo.ReturnType} {methodName}({string.Join(", ", requiredParameters.Select(parameter => ReferenceEquals(parameter, unionParameter) ? $"{MapInputTypeToJava(unionType, unionParameter.IsOptional || unionParameter.IsNullable)} {ToCamelCase(parameter.Name)}" : $"{MapParameterToJava(parameter)} {ToCamelCase(parameter.Name)}"))}) {{"); 1650WriteLine($" return {methodName}({callArguments});"); 1654WriteLine($" {methodName}({callArguments});"); 1656WriteLine(" }"); 1657WriteLine(); 1727WriteLine($" /** {capability.Description} */"); 1730WriteLine($" public {returnInfo.ReturnType} {methodName}({publicParameterList}) {{"); 1734WriteLine($" var {paramName} = optionsBag == null ? null : optionsBag.{GetOptionGetterName(parameter)}();"); 1744WriteLine($" return {implementationMethodName}({string.Join(", ", implementationArguments)});"); 1748WriteLine($" {implementationMethodName}({string.Join(", ", implementationArguments)});"); 1750WriteLine(" }"); 1751WriteLine(); 1761WriteLine($" public {returnInfo.ReturnType} {methodName}({requiredParameterList}) {{"); 1764WriteLine($" return {methodName}({AppendArgumentList(requiredParameters.Select(parameter => ToCamelCase(parameter.Name)), "null")});"); 1768WriteLine($" {methodName}({AppendArgumentList(requiredParameters.Select(parameter => ToCamelCase(parameter.Name)), "null")});"); 1770WriteLine(" }"); 1771WriteLine(); 1794WriteLine($" /** {capability.Description} */"); 1798WriteLine($" {accessibility} {returnInfo.ReturnType} {methodName}({paramList}) {{"); 1799WriteLine(" Map<String, Object> reqArgs = new HashMap<>();"); 1800WriteLine($" reqArgs.put(\"{targetParamName}\", AspireClient.serializeValue(getHandle()));"); 1808WriteLine($" if ({paramName}Id != null) {{"); 1809WriteLine($" reqArgs.put(\"{parameter.Name}\", {paramName}Id);"); 1810WriteLine(" }"); 1816WriteLine($" if ({paramName} != null) {{"); 1817WriteLine($" reqArgs.put(\"{parameter.Name}\", {paramName});"); 1818WriteLine(" }"); 1824WriteLine($" if ({paramName} != null) {{"); 1825WriteLine($" reqArgs.put(\"{parameter.Name}\", AspireClient.serializeValue({paramName}));"); 1826WriteLine(" }"); 1830WriteLine($" reqArgs.put(\"{parameter.Name}\", AspireClient.serializeValue({paramName}));"); 1836WriteLine($" getClient().invokeCapability(\"{capability.CapabilityId}\", reqArgs);"); 1837WriteLine(" return this;"); 1841WriteLine($" var result = getClient().invokeCapability(\"{capability.CapabilityId}\", reqArgs);"); 1842WriteLine($" return {RenderJavaTransportValueConversion(capability.ReturnType, "result", capability.ReturnType?.IsNullable == true)};"); 1846WriteLine($" getClient().invokeCapability(\"{capability.CapabilityId}\", reqArgs);"); 1849WriteLine(" }"); 1850WriteLine(); 1917WriteLine($" var {callbackName}Id = {callbackInitializer}getClient().registerCallback(args -> {{"); 1919WriteLine(" });"); 1933WriteLine($" var {callbackParameterName} = {GetCallbackArgumentExpression(callbackParameter, i)};"); 1941WriteLine($" return AspireClient.awaitValue({callbackInvocation});"); 1945WriteLine($" {callbackInvocation};"); 1952WriteLine($"{indent}var __aspireCallbackArguments = new HashMap<String, Object>();"); 1955WriteLine($"{indent}__aspireCallbackArguments.put(\"p{i}\", {arguments[i]});"); 1957WriteLine($"{indent}return __aspireCallbackArguments;"); 1984WriteLine($" map.put(\"{property.Name}\", {fieldName} == null ? null : (java.util.function.Function<Object, Object>) (transportArg -> {{"); 1988WriteLine($" var {callbackParameterName} = {GetCallbackArgumentExpression(callbackParameter, "transportArg")};"); 1995WriteLine($" return AspireClient.awaitValue({invocation});"); 1999WriteLine($" {invocation};"); 2002WriteLine(" }));"); 2265WriteLine($" /** {capability.Description} */"); 2269WriteLine($" private {fullType} {fieldName};"); 2270WriteLine($" public {fullType} {methodName}() {{"); 2271WriteLine($" if ({fieldName} == null) {{"); 2272WriteLine($" {fieldName} = new {wrapperType}<>(getHandle(), getClient(), \"{capability.CapabilityId}\");"); 2273WriteLine(" }"); 2274WriteLine($" return {fieldName};"); 2275WriteLine(" }"); 2276WriteLine(); 2283WriteLine("// ============================================================================"); 2284WriteLine("// Handle wrapper registrations"); 2285WriteLine("// ============================================================================"); 2286WriteLine(); 2287WriteLine("/** Static initializer to register handle wrappers. */"); 2288WriteLine("class AspireRegistrations {"); 2289WriteLine(" static {"); 2293WriteLine($" AspireClient.registerHandleWrapper(\"{handleType.TypeId}\", (h, c) -> new {handleType.ClassName}(h, c));"); 2305WriteLine($" AspireClient.registerHandleWrapper(\"{typeId}\", (h, c) -> new {wrapperType}<>(h, c));"); 2308WriteLine(" }"); 2309WriteLine(); 2310WriteLine(" static void ensureRegistered() {"); 2311WriteLine(" // Called to trigger static initializer"); 2312WriteLine(" }"); 2313WriteLine("}"); 2314WriteLine(); 2323WriteLine("// ============================================================================"); 2324WriteLine("// Connection Helpers"); 2325WriteLine("// ============================================================================"); 2326WriteLine(); 2327WriteLine("/** Main entry point for Aspire SDK. */"); 2328WriteLine("public class Aspire {"); 2329WriteLine(" /** Connect to the AppHost server. */"); 2330WriteLine(" public static AspireClient connect() throws Exception {"); 2331WriteLine(" BaseRegistrations.ensureRegistered();"); 2332WriteLine(" AspireRegistrations.ensureRegistered();"); 2333WriteLine(" String socketPath = System.getenv(\"REMOTE_APP_HOST_SOCKET_PATH\");"); 2334WriteLine(" if (socketPath == null || socketPath.isEmpty()) {"); 2335WriteLine(" throw new RuntimeException(\"REMOTE_APP_HOST_SOCKET_PATH environment variable not set. Run this application using `aspire run`.\");"); 2336WriteLine(" }"); 2337WriteLine(" AspireClient client = new AspireClient(socketPath);"); 2338WriteLine(" client.connect();"); 2339WriteLine(" String authToken = System.getenv(\"ASPIRE_REMOTE_APPHOST_TOKEN\");"); 2340WriteLine(" if (authToken == null || authToken.isEmpty()) {"); 2341WriteLine(" throw new RuntimeException(\"ASPIRE_REMOTE_APPHOST_TOKEN environment variable not set. Run this application using `aspire run`.\");"); 2342WriteLine(" }"); 2343WriteLine(" client.authenticate(authToken);"); 2344WriteLine(" client.onDisconnect(() -> System.exit(1));"); 2345WriteLine(" return client;"); 2346WriteLine(" }"); 2347WriteLine(); 2348WriteLine($" /** Create a new distributed application builder. */"); 2349WriteLine($" public static {builderClassName} createBuilder(CreateBuilderOptions options) throws Exception {{"); 2350WriteLine(" AspireClient client = connect();"); 2351WriteLine(" Map<String, Object> resolvedOptions = new HashMap<>();"); 2352WriteLine(" if (options != null) {"); 2353WriteLine(" resolvedOptions.putAll(options.toMap());"); 2354WriteLine(" }"); 2355WriteLine(" if (resolvedOptions.get(\"Args\") == null) {"); 2364WriteLine(" String forwardedArgs = System.getenv(\"ASPIRE_APPHOST_ARGS\");"); 2365WriteLine(" resolvedOptions.put(\"Args\", forwardedArgs == null || forwardedArgs.isEmpty()"); 2366WriteLine(" ? new String[0]"); 2367WriteLine(" : forwardedArgs.split(\"\\n\", -1));"); 2368WriteLine(" }"); 2371WriteLine(" if (resolvedOptions.get(\"ProjectDirectory\") == null) {"); 2372WriteLine(" String projectDirectory = System.getenv(\"ASPIRE_PROJECT_DIRECTORY\");"); 2373WriteLine(" if (projectDirectory == null || projectDirectory.isEmpty()) {"); 2374WriteLine(" projectDirectory = System.getProperty(\"user.dir\");"); 2375WriteLine(" }"); 2376WriteLine(" resolvedOptions.put(\"ProjectDirectory\", projectDirectory);"); 2377WriteLine(" }"); 2378WriteLine(" if (resolvedOptions.get(\"AppHostFilePath\") == null) {"); 2379WriteLine(" String appHostFilePath = System.getenv(\"ASPIRE_APPHOST_FILEPATH\");"); 2380WriteLine(" if (appHostFilePath != null && !appHostFilePath.isEmpty()) {"); 2381WriteLine(" resolvedOptions.put(\"AppHostFilePath\", appHostFilePath);"); 2382WriteLine(" }"); 2383WriteLine(" }"); 2384WriteLine(" Map<String, Object> args = new HashMap<>();"); 2385WriteLine(" args.put(\"argsOrOptions\", resolvedOptions);"); 2386WriteLine($" return ({builderClassName}) client.invokeCapability(\"Aspire.Hosting/createBuilder\", args);"); 2387WriteLine(" }"); 2388WriteLine("}"); 2389WriteLine();