@alloy-js/java 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (232) hide show
  1. package/LICENSE.txt +7 -0
  2. package/api-extractor.json +4 -0
  3. package/babel.config.cjs +4 -0
  4. package/dist/src/arguments.d.ts +15 -0
  5. package/dist/src/arguments.d.ts.map +1 -0
  6. package/dist/src/arguments.js +27 -0
  7. package/dist/src/arguments.js.map +1 -0
  8. package/dist/src/builtins/index.d.ts +2 -0
  9. package/dist/src/builtins/index.d.ts.map +1 -0
  10. package/dist/src/builtins/index.js +1 -0
  11. package/dist/src/builtins/index.js.map +1 -0
  12. package/dist/src/builtins/util.d.ts +4 -0
  13. package/dist/src/builtins/util.d.ts.map +1 -0
  14. package/dist/src/builtins/util.js +10 -0
  15. package/dist/src/builtins/util.js.map +1 -0
  16. package/dist/src/components/Annotation.d.ts +11 -0
  17. package/dist/src/components/Annotation.d.ts.map +1 -0
  18. package/dist/src/components/Annotation.js +12 -0
  19. package/dist/src/components/Annotation.js.map +1 -0
  20. package/dist/src/components/Block.d.ts +6 -0
  21. package/dist/src/components/Block.d.ts.map +1 -0
  22. package/dist/src/components/Block.js +4 -0
  23. package/dist/src/components/Block.js.map +1 -0
  24. package/dist/src/components/Class.d.ts +10 -0
  25. package/dist/src/components/Class.d.ts.map +1 -0
  26. package/dist/src/components/Class.js +36 -0
  27. package/dist/src/components/Class.js.map +1 -0
  28. package/dist/src/components/Constructor.d.ts +13 -0
  29. package/dist/src/components/Constructor.d.ts.map +1 -0
  30. package/dist/src/components/Constructor.js +23 -0
  31. package/dist/src/components/Constructor.js.map +1 -0
  32. package/dist/src/components/Declaration.d.ts +12 -0
  33. package/dist/src/components/Declaration.d.ts.map +1 -0
  34. package/dist/src/components/Declaration.js +18 -0
  35. package/dist/src/components/Declaration.js.map +1 -0
  36. package/dist/src/components/Enum.d.ts +13 -0
  37. package/dist/src/components/Enum.d.ts.map +1 -0
  38. package/dist/src/components/Enum.js +38 -0
  39. package/dist/src/components/Enum.js.map +1 -0
  40. package/dist/src/components/Generics.d.ts +6 -0
  41. package/dist/src/components/Generics.d.ts.map +1 -0
  42. package/dist/src/components/Generics.js +8 -0
  43. package/dist/src/components/Generics.js.map +1 -0
  44. package/dist/src/components/ImportStatement.d.ts +11 -0
  45. package/dist/src/components/ImportStatement.d.ts.map +1 -0
  46. package/dist/src/components/ImportStatement.js +9 -0
  47. package/dist/src/components/ImportStatement.js.map +1 -0
  48. package/dist/src/components/Interface.d.ts +9 -0
  49. package/dist/src/components/Interface.d.ts.map +1 -0
  50. package/dist/src/components/Interface.js +35 -0
  51. package/dist/src/components/Interface.js.map +1 -0
  52. package/dist/src/components/MavenProject.d.ts +13 -0
  53. package/dist/src/components/MavenProject.d.ts.map +1 -0
  54. package/dist/src/components/MavenProject.js +159 -0
  55. package/dist/src/components/MavenProject.js.map +1 -0
  56. package/dist/src/components/Method.d.ts +11 -0
  57. package/dist/src/components/Method.d.ts.map +1 -0
  58. package/dist/src/components/Method.js +25 -0
  59. package/dist/src/components/Method.js.map +1 -0
  60. package/dist/src/components/Name.d.ts +2 -0
  61. package/dist/src/components/Name.d.ts.map +1 -0
  62. package/dist/src/components/Name.js +10 -0
  63. package/dist/src/components/Name.js.map +1 -0
  64. package/dist/src/components/ObjectDeclaration.d.ts +13 -0
  65. package/dist/src/components/ObjectDeclaration.d.ts.map +1 -0
  66. package/dist/src/components/ObjectDeclaration.js +17 -0
  67. package/dist/src/components/ObjectDeclaration.js.map +1 -0
  68. package/dist/src/components/PackageDirectory.d.ts +16 -0
  69. package/dist/src/components/PackageDirectory.d.ts.map +1 -0
  70. package/dist/src/components/PackageDirectory.js +58 -0
  71. package/dist/src/components/PackageDirectory.js.map +1 -0
  72. package/dist/src/components/Parameters.d.ts +11 -0
  73. package/dist/src/components/Parameters.d.ts.map +1 -0
  74. package/dist/src/components/Parameters.js +17 -0
  75. package/dist/src/components/Parameters.js.map +1 -0
  76. package/dist/src/components/ProjectDirectory.d.ts +23 -0
  77. package/dist/src/components/ProjectDirectory.d.ts.map +1 -0
  78. package/dist/src/components/ProjectDirectory.js +42 -0
  79. package/dist/src/components/ProjectDirectory.js.map +1 -0
  80. package/dist/src/components/Reference.d.ts +6 -0
  81. package/dist/src/components/Reference.d.ts.map +1 -0
  82. package/dist/src/components/Reference.js +6 -0
  83. package/dist/src/components/Reference.js.map +1 -0
  84. package/dist/src/components/SourceFile.d.ts +17 -0
  85. package/dist/src/components/SourceFile.d.ts.map +1 -0
  86. package/dist/src/components/SourceFile.js +70 -0
  87. package/dist/src/components/SourceFile.js.map +1 -0
  88. package/dist/src/components/Value.d.ts +5 -0
  89. package/dist/src/components/Value.d.ts.map +1 -0
  90. package/dist/src/components/Value.js +17 -0
  91. package/dist/src/components/Value.js.map +1 -0
  92. package/dist/src/components/Variable.d.ts +9 -0
  93. package/dist/src/components/Variable.d.ts.map +1 -0
  94. package/dist/src/components/Variable.js +9 -0
  95. package/dist/src/components/Variable.js.map +1 -0
  96. package/dist/src/components/index.d.ts +21 -0
  97. package/dist/src/components/index.d.ts.map +1 -0
  98. package/dist/src/components/index.js +21 -0
  99. package/dist/src/components/index.js.map +1 -0
  100. package/dist/src/components/stc/index.d.ts +86 -0
  101. package/dist/src/components/stc/index.d.ts.map +1 -0
  102. package/dist/src/components/stc/index.js +24 -0
  103. package/dist/src/components/stc/index.js.map +1 -0
  104. package/dist/src/create-library.d.ts +15 -0
  105. package/dist/src/create-library.d.ts.map +1 -0
  106. package/dist/src/create-library.js +34 -0
  107. package/dist/src/create-library.js.map +1 -0
  108. package/dist/src/generics.d.ts +28 -0
  109. package/dist/src/generics.d.ts.map +1 -0
  110. package/dist/src/generics.js +51 -0
  111. package/dist/src/generics.js.map +1 -0
  112. package/dist/src/index.d.ts +9 -0
  113. package/dist/src/index.d.ts.map +1 -0
  114. package/dist/src/index.js +9 -0
  115. package/dist/src/index.js.map +1 -0
  116. package/dist/src/name-policy.d.ts +5 -0
  117. package/dist/src/name-policy.d.ts.map +1 -0
  118. package/dist/src/name-policy.js +21 -0
  119. package/dist/src/name-policy.js.map +1 -0
  120. package/dist/src/object-modifiers.d.ts +24 -0
  121. package/dist/src/object-modifiers.d.ts.map +1 -0
  122. package/dist/src/object-modifiers.js +31 -0
  123. package/dist/src/object-modifiers.js.map +1 -0
  124. package/dist/src/symbols/index.d.ts +6 -0
  125. package/dist/src/symbols/index.d.ts.map +1 -0
  126. package/dist/src/symbols/index.js +6 -0
  127. package/dist/src/symbols/index.js.map +1 -0
  128. package/dist/src/symbols/java-output-symbol.d.ts +14 -0
  129. package/dist/src/symbols/java-output-symbol.d.ts.map +1 -0
  130. package/dist/src/symbols/java-output-symbol.js +21 -0
  131. package/dist/src/symbols/java-output-symbol.js.map +1 -0
  132. package/dist/src/symbols/java-package-scope.d.ts +6 -0
  133. package/dist/src/symbols/java-package-scope.d.ts.map +1 -0
  134. package/dist/src/symbols/java-package-scope.js +8 -0
  135. package/dist/src/symbols/java-package-scope.js.map +1 -0
  136. package/dist/src/symbols/java-project-scope.d.ts +67 -0
  137. package/dist/src/symbols/java-project-scope.d.ts.map +1 -0
  138. package/dist/src/symbols/java-project-scope.js +29 -0
  139. package/dist/src/symbols/java-project-scope.js.map +1 -0
  140. package/dist/src/symbols/reference.d.ts +8 -0
  141. package/dist/src/symbols/reference.d.ts.map +1 -0
  142. package/dist/src/symbols/reference.js +21 -0
  143. package/dist/src/symbols/reference.js.map +1 -0
  144. package/dist/src/symbols/scopes.d.ts +4 -0
  145. package/dist/src/symbols/scopes.d.ts.map +1 -0
  146. package/dist/src/symbols/scopes.js +1 -0
  147. package/dist/src/symbols/scopes.js.map +1 -0
  148. package/dist/test/annotation.test.d.ts +2 -0
  149. package/dist/test/annotation.test.d.ts.map +1 -0
  150. package/dist/test/class.test.d.ts +2 -0
  151. package/dist/test/class.test.d.ts.map +1 -0
  152. package/dist/test/constructor.test.d.ts +2 -0
  153. package/dist/test/constructor.test.d.ts.map +1 -0
  154. package/dist/test/enum.test.d.ts +2 -0
  155. package/dist/test/enum.test.d.ts.map +1 -0
  156. package/dist/test/externals.test.d.ts +2 -0
  157. package/dist/test/externals.test.d.ts.map +1 -0
  158. package/dist/test/generics.d.ts +2 -0
  159. package/dist/test/generics.d.ts.map +1 -0
  160. package/dist/test/interface.test.d.ts +2 -0
  161. package/dist/test/interface.test.d.ts.map +1 -0
  162. package/dist/test/maven.d.ts +2 -0
  163. package/dist/test/maven.d.ts.map +1 -0
  164. package/dist/test/method.test.d.ts +2 -0
  165. package/dist/test/method.test.d.ts.map +1 -0
  166. package/dist/test/package.test.d.ts +2 -0
  167. package/dist/test/package.test.d.ts.map +1 -0
  168. package/dist/test/reference.test.d.ts +2 -0
  169. package/dist/test/reference.test.d.ts.map +1 -0
  170. package/dist/test/utils.d.ts +6 -0
  171. package/dist/test/utils.d.ts.map +1 -0
  172. package/dist/test/values.test.d.ts +2 -0
  173. package/dist/test/values.test.d.ts.map +1 -0
  174. package/dist/test/variables.test.d.ts +2 -0
  175. package/dist/test/variables.test.d.ts.map +1 -0
  176. package/dist/tsconfig.tsbuildinfo +1 -0
  177. package/package.json +49 -0
  178. package/src/arguments.ts +30 -0
  179. package/src/builtins/index.ts +1 -0
  180. package/src/builtins/util.ts +117 -0
  181. package/src/components/Annotation.tsx +23 -0
  182. package/src/components/Block.tsx +8 -0
  183. package/src/components/Class.tsx +33 -0
  184. package/src/components/Constructor.tsx +25 -0
  185. package/src/components/Declaration.tsx +24 -0
  186. package/src/components/Enum.tsx +40 -0
  187. package/src/components/Generics.tsx +12 -0
  188. package/src/components/ImportStatement.tsx +21 -0
  189. package/src/components/Interface.tsx +31 -0
  190. package/src/components/MavenProject.tsx +151 -0
  191. package/src/components/Method.tsx +31 -0
  192. package/src/components/Name.tsx +10 -0
  193. package/src/components/ObjectDeclaration.tsx +22 -0
  194. package/src/components/PackageDirectory.tsx +84 -0
  195. package/src/components/Parameters.tsx +21 -0
  196. package/src/components/ProjectDirectory.tsx +76 -0
  197. package/src/components/Reference.tsx +12 -0
  198. package/src/components/SourceFile.tsx +84 -0
  199. package/src/components/Value.tsx +22 -0
  200. package/src/components/Variable.tsx +16 -0
  201. package/src/components/index.ts +20 -0
  202. package/src/components/stc/index.ts +24 -0
  203. package/src/create-library.ts +67 -0
  204. package/src/generics.ts +59 -0
  205. package/src/index.ts +8 -0
  206. package/src/name-policy.ts +33 -0
  207. package/src/object-modifiers.ts +48 -0
  208. package/src/symbols/index.ts +5 -0
  209. package/src/symbols/java-output-symbol.ts +30 -0
  210. package/src/symbols/java-package-scope.ts +17 -0
  211. package/src/symbols/java-project-scope.ts +104 -0
  212. package/src/symbols/reference.ts +24 -0
  213. package/src/symbols/scopes.ts +4 -0
  214. package/temp/api.json +5805 -0
  215. package/test/annotation.test.tsx +56 -0
  216. package/test/class.test.tsx +133 -0
  217. package/test/constructor.test.tsx +96 -0
  218. package/test/enum.test.tsx +105 -0
  219. package/test/externals.test.tsx +46 -0
  220. package/test/generics.tsx +58 -0
  221. package/test/interface.test.tsx +84 -0
  222. package/test/maven.tsx +261 -0
  223. package/test/method.test.tsx +162 -0
  224. package/test/package.test.tsx +18 -0
  225. package/test/reference.test.tsx +88 -0
  226. package/test/utils.tsx +72 -0
  227. package/test/values.test.tsx +29 -0
  228. package/test/variables.test.tsx +101 -0
  229. package/test/vitest.d.ts +10 -0
  230. package/tsconfig.json +11 -0
  231. package/tsdoc-metadata.json +11 -0
  232. package/vitest.config.ts +18 -0
@@ -0,0 +1,84 @@
1
+ import {
2
+ ComponentContext,
3
+ SourceFile as CoreSourceFile,
4
+ createContext,
5
+ OutputSymbol,
6
+ reactive,
7
+ Scope,
8
+ } from "@alloy-js/core";
9
+ import { Children } from "@alloy-js/core/jsx-runtime";
10
+ import { JavaOutputSymbol } from "../symbols/index.js";
11
+ import { ImportStatements, ImportSymbol } from "./ImportStatement.js";
12
+ import { usePackage } from "./PackageDirectory.js";
13
+ import { Reference } from "./Reference.js";
14
+
15
+ export interface SourceFileContext {
16
+ addImport(symbol: OutputSymbol): string;
17
+ }
18
+
19
+ export const SourceFileContext: ComponentContext<SourceFileContext> =
20
+ createContext();
21
+
22
+ export interface SourceFileProps {
23
+ path: string;
24
+ children?: Children;
25
+ }
26
+
27
+ /**
28
+ * Represents a Java source file.
29
+ *
30
+ * Handles top level package declaration, as well as importing other sources
31
+ */
32
+ export function SourceFile(props: SourceFileProps) {
33
+ const packageCtx = usePackage();
34
+
35
+ if (!packageCtx) {
36
+ throw new Error("SourceFile must be declared inside a package");
37
+ }
38
+
39
+ // Collection of import symbols
40
+ const importRecords: ImportSymbol[] = reactive([]);
41
+ // Map a symbol to import name, keep track of already imported symbols
42
+ const importedSymbols = new Map<OutputSymbol, string>();
43
+
44
+ // Add import to file if not already, returns name of imported symbol
45
+ function addImport(symbol: JavaOutputSymbol): string {
46
+ if (importedSymbols.has(symbol)) {
47
+ return importedSymbols.get(symbol)!;
48
+ }
49
+
50
+ // Only need to import if not in same package, or comes from java.lang
51
+ if (
52
+ symbol.package !== packageCtx?.qualifiedName ||
53
+ symbol.package?.startsWith("java.lang")
54
+ ) {
55
+ importRecords.push({
56
+ package: symbol.package ?? "",
57
+ name: symbol.name,
58
+ wildcard: false, // TODO
59
+ });
60
+ }
61
+ importedSymbols.set(symbol, symbol.name);
62
+
63
+ return symbol.name;
64
+ }
65
+
66
+ const sfContext: SourceFileContext = {
67
+ addImport,
68
+ };
69
+
70
+ return <CoreSourceFile path={props.path} filetype="java" reference={Reference}>
71
+ package {packageCtx.qualifiedName};
72
+
73
+ {importRecords.length > 0 ? (
74
+ <>
75
+ <ImportStatements imports={importRecords} />
76
+ {"\n"}
77
+ </>
78
+ ) : undefined}<SourceFileContext.Provider value={sfContext}>
79
+ <Scope name={props.path} kind="source-file">
80
+ {props.children}
81
+ </Scope>
82
+ </SourceFileContext.Provider>
83
+ </CoreSourceFile>;
84
+ }
@@ -0,0 +1,22 @@
1
+ import { memo } from "@alloy-js/core";
2
+
3
+ export interface ValueProps {
4
+ value?: unknown;
5
+ }
6
+
7
+ export function Value(props: ValueProps): any {
8
+ return memo(() => {
9
+ const value = props.value;
10
+
11
+ if (typeof value === "undefined") {
12
+ return "null";
13
+ } else if (typeof value === "number" || typeof value === "boolean") {
14
+ return String(value);
15
+ } else if (typeof value === "string") {
16
+ return `"${value}"`;
17
+ } else if (typeof value === "function") {
18
+ // functions are inserted as-is.
19
+ return value;
20
+ }
21
+ });
22
+ }
@@ -0,0 +1,16 @@
1
+ import { Child, Children, code } from "@alloy-js/core";
2
+ import { useJavaNamePolicy } from "../name-policy.js";
3
+ import { collectModifiers, ObjectModifiers } from "../object-modifiers.js";
4
+
5
+ export interface VariableProps extends ObjectModifiers {
6
+ type: Child;
7
+ name: string;
8
+ value?: Children;
9
+ }
10
+
11
+ export function Variable(props: VariableProps) {
12
+ const name = useJavaNamePolicy().getName(props.name, "variable");
13
+ const modifiers = collectModifiers(props);
14
+
15
+ return code`${modifiers}${props.type} ${name}${props.value ? code` = ${props.value}` : ""};`;
16
+ }
@@ -0,0 +1,20 @@
1
+ export * from "./Annotation.js";
2
+ export * from "./Block.js";
3
+ export * from "./Class.js";
4
+ export * from "./Constructor.js";
5
+ export * from "./Declaration.js";
6
+ export * from "./Enum.js";
7
+ export * from "./Generics.js";
8
+ export * from "./ImportStatement.js";
9
+ export * from "./Interface.js";
10
+ export * from "./MavenProject.js";
11
+ export * from "./Method.js";
12
+ export * from "./Name.js";
13
+ export * from "./ObjectDeclaration.js";
14
+ export * from "./PackageDirectory.js";
15
+ export * from "./Parameters.js";
16
+ export * from "./ProjectDirectory.js";
17
+ export * from "./Reference.js";
18
+ export * from "./SourceFile.js";
19
+ export * from "./Value.js";
20
+ export * from "./Variable.js";
@@ -0,0 +1,24 @@
1
+ import { stc } from "@alloy-js/core";
2
+ import * as base from "../index.js";
3
+
4
+ export const Annotation = stc(base.Annotation);
5
+ export const Block = stc(base.Block);
6
+ export const Class = stc(base.Class);
7
+ export const Constructor = stc(base.Constructor);
8
+ export const Declaration = stc(base.Declaration);
9
+ export const Enum = stc(base.Enum);
10
+ export const Generics = stc(base.Generics);
11
+ export const EnumMember = stc(base.EnumMember);
12
+ export const ImportStatement = stc(base.ImportStatement);
13
+ export const Interface = stc(base.Interface);
14
+ export const MavenProject = stc(base.MavenProject);
15
+ export const Method = stc(base.Method);
16
+ export const Name = stc(base.Name);
17
+ export const ObjectDeclaration = stc(base.ObjectDeclaration);
18
+ export const PackageDirectory = stc(base.PackageDirectory);
19
+ export const Parameters = stc(base.Parameters);
20
+ export const ProjectDirectory = stc(base.ProjectDirectory);
21
+ export const Reference = stc(base.Reference);
22
+ export const SourceFile = stc(base.SourceFile);
23
+ export const Value = stc(base.Value);
24
+ export const Variable = stc(base.Variable);
@@ -0,0 +1,67 @@
1
+ // Declare libraries (dependencies) that you are adding to the project.
2
+ // Allows discovery of symbols from these libraries for use in the program
3
+
4
+ import {
5
+ Binder,
6
+ getSymbolCreatorSymbol,
7
+ Refkey,
8
+ refkey,
9
+ SymbolCreator,
10
+ } from "@alloy-js/core";
11
+ import { createJavaProjectScope, JavaOutputSymbol } from "./symbols/index.js";
12
+ import { createJavaPackageScope } from "./symbols/java-package-scope.js";
13
+
14
+ export interface LibraryDescriptor {
15
+ [pkg: string]: string[];
16
+ }
17
+
18
+ function createSymbols(
19
+ binder: Binder,
20
+ props: CreateLibraryProps<LibraryDescriptor>,
21
+ refkeys: Record<string, any>,
22
+ ) {
23
+ const projectScope = createJavaProjectScope(binder, undefined, props.groupId);
24
+
25
+ for (const [pkg, symbols] of Object.entries(props.descriptor)) {
26
+ const packageScope = createJavaPackageScope(binder, projectScope, pkg);
27
+
28
+ for (const symB of symbols) {
29
+ const _sym = binder.createSymbol<JavaOutputSymbol>({
30
+ name: symB,
31
+ scope: packageScope,
32
+ refkey: refkeys[symB],
33
+ package: packageScope.name,
34
+ });
35
+ }
36
+ }
37
+ }
38
+
39
+ export type LibraryRefkeys<T extends LibraryDescriptor> = {
40
+ [S in T[keyof T] extends readonly string[] ? T[keyof T][number]
41
+ : never]: Refkey;
42
+ };
43
+
44
+ export interface CreateLibraryProps<T extends LibraryDescriptor> {
45
+ groupId: string;
46
+ artifactId: string;
47
+ version: string;
48
+ descriptor: T;
49
+ }
50
+
51
+ export function createLibrary<const T extends LibraryDescriptor>(
52
+ props: CreateLibraryProps<T>,
53
+ ): LibraryRefkeys<T> & SymbolCreator {
54
+ const refkeys: any = {
55
+ [getSymbolCreatorSymbol()](binder: Binder) {
56
+ createSymbols(binder, props, refkeys);
57
+ },
58
+ };
59
+
60
+ for (const [pkg, symbols] of Object.entries(props.descriptor)) {
61
+ for (const symb of symbols) {
62
+ refkeys[symb] = refkey(pkg, symb);
63
+ }
64
+ }
65
+
66
+ return refkeys;
67
+ }
@@ -0,0 +1,59 @@
1
+ import { Child, Children, code, mapJoin } from "@alloy-js/core";
2
+
3
+ /**
4
+ * Allow component to specify generic typing
5
+ */
6
+ export interface GenericTypes {
7
+ generics?: Record<string, Children>;
8
+ }
9
+
10
+ /**
11
+ * Collect generics types to pass definitions to components
12
+ *
13
+ * @remarks
14
+ *
15
+ * Input of `{ T: 'String' }` will produce `<T extends String>`.
16
+ *
17
+ * Can then be passed to class definition, method definition, using a generic class etc.
18
+ *
19
+ * @param generics - Record of type variable, (T, Z, etc), mapped to its bounds (if any).
20
+ */
21
+ export function collectGenerics(generics: Record<string, Children>) {
22
+ const genericTypes = mapJoin(
23
+ new Map(Object.entries(generics)),
24
+ (key, value) => {
25
+ const extendsExpression = value ? code` extends ${value}` : "";
26
+ return [key, extendsExpression];
27
+ },
28
+ {
29
+ joiner: ", ",
30
+ },
31
+ );
32
+ return code`<${genericTypes}>`;
33
+ }
34
+
35
+ /**
36
+ * Pass generics types to an actual object. For instance if we are using the `List` class, we define it like `List<String>`
37
+ * or whatever type we like. These are where we define the types. Can also pass wildcard types like `List<? extends String>`, which
38
+ * allows you to pass unknown types.
39
+ *
40
+ * @param generics - Takes an array of either direct child (refkey or defining type), or a record which passes a wildcard type with optional bounds
41
+ */
42
+ export function passGenerics(generics: (Child | Record<"?", Children>)[]) {
43
+ return mapJoin(
44
+ generics,
45
+ (val) => {
46
+ if (typeof val === "object" && val) {
47
+ const key = Object.keys(val)[0];
48
+ if (key === "?") {
49
+ // @ts-expect-error Key is ? in this case
50
+ const value = val[key];
51
+ const extendsExpression = value ? code` extends ${value}` : "";
52
+ return [key, extendsExpression];
53
+ }
54
+ }
55
+ return val as Child;
56
+ },
57
+ { joiner: ", " },
58
+ );
59
+ }
package/src/index.ts ADDED
@@ -0,0 +1,8 @@
1
+ export * from "./arguments.js";
2
+ export * from "./builtins/index.js";
3
+ export * from "./components/index.js";
4
+ export * from "./create-library.js";
5
+ export * from "./generics.js";
6
+ export * from "./name-policy.js";
7
+ export * from "./object-modifiers.js";
8
+ export * from "./symbols/index.js";
@@ -0,0 +1,33 @@
1
+ import { createNamePolicy, NamePolicy, useNamePolicy } from "@alloy-js/core";
2
+ import { camelCase, constantCase, pascalCase } from "change-case";
3
+
4
+ export type JavaElements =
5
+ | "class"
6
+ | "interface"
7
+ | "enum"
8
+ | "enum-member"
9
+ | "function"
10
+ | "parameter"
11
+ | "constant"
12
+ | "variable"
13
+ | "method";
14
+
15
+ export function createJavaNamePolicy(): NamePolicy<JavaElements> {
16
+ return createNamePolicy((name, element) => {
17
+ switch (element) {
18
+ case "class":
19
+ case "interface":
20
+ case "enum":
21
+ return pascalCase(name);
22
+ case "enum-member":
23
+ case "constant":
24
+ return constantCase(name);
25
+ default:
26
+ return camelCase(name);
27
+ }
28
+ });
29
+ }
30
+
31
+ export function useJavaNamePolicy(): NamePolicy<JavaElements> {
32
+ return useNamePolicy();
33
+ }
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Objects extend this interface to allow to take modifiers as boolean props.
3
+ * Then can declare components like so
4
+ *
5
+ * `<Class public final abstract />`
6
+ *
7
+ * Error will be thrown if try to specify multiple access modifiers
8
+ */
9
+ export interface ObjectModifiers {
10
+ public?: boolean;
11
+ protected?: boolean;
12
+ private?: boolean;
13
+
14
+ default?: boolean;
15
+ static?: boolean;
16
+ final?: boolean;
17
+ synchronized?: boolean;
18
+ abstract?: boolean;
19
+ native?: boolean;
20
+ strictfp?: boolean;
21
+ transient?: boolean;
22
+ volatile?: boolean;
23
+ }
24
+
25
+ export function collectModifiers<T extends ObjectModifiers>(props: T) {
26
+ const modifiers = [];
27
+
28
+ // If more than one access modifier is specified, throw an error
29
+ if (
30
+ [props.public, props.protected, props.private].filter(Boolean).length > 1
31
+ ) {
32
+ throw new Error("Cannot specify multiple access modifiers");
33
+ }
34
+
35
+ if (props.public) modifiers.push("public");
36
+ if (props.protected) modifiers.push("protected");
37
+ if (props.private) modifiers.push("private");
38
+ if (props.abstract) modifiers.push("abstract");
39
+ if (props.static) modifiers.push("static");
40
+ if (props.synchronized) modifiers.push("synchronized");
41
+ if (props.transient) modifiers.push("transient");
42
+ if (props.volatile) modifiers.push("volatile");
43
+ if (props.final) modifiers.push("final");
44
+ if (props.default) modifiers.push("default");
45
+ if (props.native) modifiers.push("native");
46
+ if (props.strictfp) modifiers.push("strictfp");
47
+ return modifiers.join(" ") + (modifiers.length > 0 ? " " : "");
48
+ }
@@ -0,0 +1,5 @@
1
+ export * from "./java-output-symbol.js";
2
+ export * from "./java-package-scope.js";
3
+ export * from "./java-project-scope.js";
4
+ export * from "./reference.js";
5
+ export * from "./scopes.js";
@@ -0,0 +1,30 @@
1
+ import { OutputSymbol, refkey, useBinder, useScope } from "@alloy-js/core";
2
+ import { DeclarationProps, usePackage } from "../components/index.js";
3
+ import { JavaOutputScope } from "./scopes.js";
4
+
5
+ /**
6
+ * Represents an 'exported' symbol from a .java file. Class, enum, interface etc.
7
+ * Not considered exported if private
8
+ */
9
+ export interface JavaOutputSymbol extends OutputSymbol {
10
+ /**
11
+ * Fully qualified package name
12
+ */
13
+ package?: string;
14
+ }
15
+
16
+ export function createJavaSymbol(props: DeclarationProps) {
17
+ const binder = useBinder();
18
+ const scope = useScope() as JavaOutputScope;
19
+
20
+ const parentPackage = usePackage();
21
+
22
+ const sym = binder.createSymbol<JavaOutputSymbol>({
23
+ name: props.name,
24
+ scope,
25
+ refkey: props.refkey ?? refkey(props.name),
26
+ package: parentPackage !== null ? parentPackage?.qualifiedName : "",
27
+ });
28
+
29
+ return sym;
30
+ }
@@ -0,0 +1,17 @@
1
+ import { Binder, OutputScope } from "@alloy-js/core";
2
+
3
+ export interface JavaPackageScope extends OutputScope {
4
+ kind: "package";
5
+ }
6
+
7
+ export function createJavaPackageScope(
8
+ binder: Binder,
9
+ parent: OutputScope | undefined,
10
+ name: string,
11
+ ): JavaPackageScope {
12
+ return binder.createScope<JavaPackageScope>({
13
+ kind: "package",
14
+ name,
15
+ parent,
16
+ });
17
+ }
@@ -0,0 +1,104 @@
1
+ import { Binder, OutputScope } from "@alloy-js/core";
2
+
3
+ /**
4
+ * Represents an external dependency imported through a maven or gradle project
5
+ */
6
+ export interface JavaDependency {
7
+ groupId: string;
8
+ artifactId: string;
9
+ version?: string;
10
+
11
+ scope?: "compile" | "test" | "runtime" | "provided";
12
+ }
13
+
14
+ export interface MavenProjectConfig {
15
+ groupId: string;
16
+ artifactId: string;
17
+ version: string;
18
+ javaVersion: number;
19
+
20
+ name?: string;
21
+ description?: string;
22
+
23
+ repositories?: {
24
+ id: string;
25
+ url: string;
26
+ }[];
27
+
28
+ pluginRepositories?: {
29
+ id: string;
30
+ url: string;
31
+ }[];
32
+
33
+ build?: {
34
+ plugins?: {
35
+ groupId: string;
36
+ artifactId: string;
37
+ version?: string;
38
+ executions?: {
39
+ id?: string;
40
+ phase?: string;
41
+ goals?: string[];
42
+ }[];
43
+ // TODO: Maybe worry about multi-level config such as maven shade plugin
44
+ configuration?: Record<string, any>;
45
+ }[];
46
+ resources?: {
47
+ directory: string;
48
+ includes?: string[];
49
+ excludes?: string[];
50
+ filtering?: boolean;
51
+ targetPath?: string;
52
+ }[];
53
+ };
54
+ }
55
+
56
+ // TODO: Support gradle projects
57
+ export interface GradleProjectConfig {}
58
+
59
+ /**
60
+ * Represents the java project itself (maven, gradle, etc)
61
+ */
62
+ export interface JavaProjectScope extends OutputScope {
63
+ kind: "project";
64
+
65
+ /**
66
+ * The dependencies of this project
67
+ * Map qualified package name to dependency
68
+ */
69
+ dependencies: Map<string, JavaDependency>;
70
+ addDependency(dependency: JavaDependency): string;
71
+
72
+ /**
73
+ * Define project configuration. Either maven or gradle
74
+ */
75
+ mavenProjectConfig?: MavenProjectConfig;
76
+ gradleProjectConfig?: GradleProjectConfig;
77
+ }
78
+
79
+ export function createJavaProjectScope(
80
+ binder: Binder,
81
+ parent: OutputScope | undefined,
82
+ name: string,
83
+ mavenProjectConfig?: MavenProjectConfig,
84
+ gradleProjectConfig?: GradleProjectConfig,
85
+ ): JavaProjectScope {
86
+ return binder.createScope<JavaProjectScope>({
87
+ kind: "project",
88
+ name,
89
+ parent,
90
+ dependencies: new Map(),
91
+ addDependency(dependency: JavaDependency): string {
92
+ const depKey = `${dependency.groupId}.${dependency.artifactId}.${dependency.version}`;
93
+
94
+ if (this.dependencies.has(depKey)) {
95
+ return depKey;
96
+ }
97
+
98
+ this.dependencies.set(depKey, dependency);
99
+ return depKey;
100
+ },
101
+ mavenProjectConfig,
102
+ gradleProjectConfig,
103
+ });
104
+ }
@@ -0,0 +1,24 @@
1
+ import { memo, Refkey, resolve, untrack, useContext } from "@alloy-js/core";
2
+ import { SourceFileContext } from "../components/index.js";
3
+ import { JavaOutputSymbol } from "./java-output-symbol.js";
4
+ import { JavaOutputScope } from "./scopes.js";
5
+
6
+ /**
7
+ * Resolve reference to symbol reference, and handle dependency management
8
+ *
9
+ * @param refkey - Reference key to symbol
10
+ */
11
+ export function ref(refkey: Refkey) {
12
+ const sourceFile = useContext(SourceFileContext);
13
+ const result = resolve<JavaOutputScope, JavaOutputSymbol>(refkey as Refkey);
14
+
15
+ return memo(() => {
16
+ if (result.value === undefined) {
17
+ return "<Unresolved Symbol>";
18
+ }
19
+
20
+ const { targetDeclaration } = result.value;
21
+
22
+ return untrack(() => sourceFile!.addImport(targetDeclaration));
23
+ });
24
+ }
@@ -0,0 +1,4 @@
1
+ import { JavaPackageScope } from "./java-package-scope.js";
2
+ import { JavaProjectScope } from "./java-project-scope.js";
3
+
4
+ export type JavaOutputScope = JavaProjectScope | JavaPackageScope;