Skip to content

Commit 2a7b149

Browse files
ArishSultanpkedy
authored andcommitted
Fixed Java Code Structure By adding static classes to easily share data across all instances of class
1 parent 5b5063d commit 2a7b149

File tree

9 files changed

+668
-29
lines changed

9 files changed

+668
-29
lines changed

src/java/src/default-visitor.ts

+13
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,17 @@ export class DefaultVisitor extends BaseVisitor {
4545
const visitor = this.unionVisitor(this.writer);
4646
context.union.accept(context, visitor);
4747
}
48+
49+
visitNamespaceAfter(context: Context) {
50+
this.write(`\n`);
51+
this.write(`\n`);
52+
this.write(`public static void main(String[] args) {`);
53+
this.write(`\n`);
54+
this.write(`\t System.out.println("Welcome to JAVA. Happy Coding :)");`);
55+
this.write(`\n`);
56+
this.write(`\t }`);
57+
this.write(`\n`);
58+
this.write(`}`);
59+
super.visitNamespaceAfter(context);
60+
}
4861
}

src/java/src/visitors/enum-visitor.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export class EnumVisitor extends BaseVisitor {
1313

1414
visitEnum(context: Context) {
1515
const { enum: enumNode } = context;
16-
this.write(`enum ${enumNode.name} {`);
16+
this.write(`public enum ${enumNode.name} {`);
1717
this.write(`\n`);
1818
super.visitEnum(context);
1919
}
+23-17
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,24 @@
11
import { BaseVisitor, Context } from "@apexlang/core/model";
2-
import { camelCase, pascalCase } from "../../../utils";
32

43
export class ImportVisitor extends BaseVisitor {
54
visitNamespaceBefore(context: Context) {
5+
this.write(`import java.util.*;`);
6+
this.write(`\n`);
7+
this.write(`import java.io.*;`);
8+
this.write(`\n`);
9+
this.write(`import java.lang.*;`);
10+
this.write(`\n`);
11+
this.write(`import java.math.*;`);
12+
this.write(`\n`);
13+
this.write(`import java.text.*;`);
14+
this.write(`\n`);
15+
this.write(`import java.time.*;`);
16+
this.write(`\n`);
17+
this.write(`\n`);
18+
this.write(`public class Main {`);
19+
this.write(`\n`);
20+
this.write(`\n`);
21+
622
// const packageName = context.namespace.name
723
// .split(".")
824
// .map((n, i) => {
@@ -18,20 +34,10 @@ export class ImportVisitor extends BaseVisitor {
1834
super.visitNamespaceBefore(context);
1935
}
2036

21-
visitNamespaceAfter(context: Context) {
22-
this.write(`import java.util.*;`);
23-
this.write(`\n`);
24-
this.write(`import java.io.*;`);
25-
this.write(`\n`);
26-
this.write(`import java.lang.*;`);
27-
this.write(`\n`);
28-
this.write(`import java.math.*;`);
29-
this.write(`\n`);
30-
this.write(`import java.text.*;`);
31-
this.write(`\n`);
32-
this.write(`import java.time.*;`);
33-
this.write(`\n`);
34-
this.write(`\n`);
35-
super.visitImport(context);
36-
}
37+
// visitNamespaceAfter(context: Context) {
38+
// this.write(`\n`);
39+
// this.write(`\n`);
40+
// this.write(`}`);
41+
// super.visitNamespaceAfter(context);
42+
// }
3743
}

src/java/src/visitors/interface-visitor.ts

+35-6
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,65 @@ export class InterfaceVisitor extends BaseVisitor {
1111
const { interface: iFace } = context;
1212
iFace.annotations.forEach((a) => {
1313
if (a.name === "service") {
14-
this.write(`interface ${iFace.name} {`);
14+
this.write(`public interface ${iFace.name} {`);
1515
this.write(`\n`);
1616
iFace.operations.forEach((o) => {
17-
this.write(`\t ${convertType(o.type, context.config)} ${o.name}(`);
17+
this.write(
18+
`\t public static ${convertType(o.type, context.config)} ${o.name}(`
19+
);
1820
o.parameters.forEach((p, i) => {
1921
this.write(`${convertType(p.type, context.config)} ${p.name}`);
2022
if (i !== o.parameters.length - 1) {
2123
this.write(`, `);
2224
}
2325
});
2426
this.write(`)`);
25-
this.write(` throws Exception;`);
27+
this.write(`{`);
28+
this.write(`\n`);
29+
this.write(`\t\t`);
30+
if (o.parameters.length > 0) {
31+
this.write(`return ${o.parameters[0].name};`);
32+
} else {
33+
this.write(`return;`);
34+
}
35+
this.write(`\n`);
36+
this.write(`\t }`);
37+
this.write(`\n`);
2638
this.write(`\n`);
2739
});
2840
this.write(`}`);
2941
}
3042

3143
if (a.name === "dependency") {
32-
this.write(`interface ${iFace.name} {`);
44+
this.write(`public interface ${iFace.name} {`);
3345
this.write(`\n`);
3446
iFace.operations.forEach((o) => {
35-
this.write(`\t ${convertType(o.type, context.config)} ${o.name}(`);
47+
this.write(
48+
`\t public static ${convertType(o.type, context.config)} ${o.name}(`
49+
);
3650
o.parameters.forEach((p, i) => {
3751
this.write(`${p.name} ${convertType(p.type, context.config)}`);
3852
if (i !== o.parameters.length - 1) {
3953
this.write(`, `);
4054
}
4155
});
4256
this.write(`)`);
43-
this.write(` throws Exception;`);
57+
this.write(`{`);
58+
this.write(`\n`);
59+
this.write(`\t\t`);
60+
if (o.parameters.length > 0) {
61+
this.write(`return ${o.parameters[0].name};`);
62+
} else if (
63+
iFace.operations.length > 0 &&
64+
convertType(o.type, context.config) !== "void"
65+
) {
66+
this.write(`return null;`);
67+
} else {
68+
this.write(`return;`);
69+
}
70+
this.write(`\n`);
71+
this.write(`\t }`);
72+
this.write(`\n`);
4473
this.write(`\n`);
4574
});
4675
this.write(`}`);

src/java/src/visitors/type-visitor.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export class TypeVisitor extends BaseVisitor {
2020
const { type } = context;
2121

2222
this.write(formatComment(" // ", type.description));
23-
this.write(`class ${pascalCase(type.name)} {\n`);
23+
this.write(`public static class ${pascalCase(type.name)} {\n`);
2424
this.write("\n");
2525
super.visitTypesBefore(context);
2626
}
@@ -82,7 +82,7 @@ export class TypeVisitor extends BaseVisitor {
8282
// this.write(" }\n");
8383
// } else {
8484
this.write(formatComment(" // ", field.description));
85-
this.write(`\t public ${type} ${pascalCase(field.name)};\n\n`);
85+
this.write(`\t public static ${type} ${pascalCase(field.name)};\n\n`);
8686
// }
8787
}
8888

src/java/src/visitors/union-visitor.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@ export class UnionVisitor extends BaseVisitor {
99

1010
visitUnion(context: Context) {
1111
const union = context.union;
12-
this.write(`class ${union.name} { \n`);
12+
this.write(`public static class ${union.name} { \n`);
1313
this.write(`\n`);
1414
union.types.forEach((type) => {
1515
this.write(
16-
`\t public ${convertType(type, context.config)} ${camelCase(
16+
`\t public static ${convertType(type, context.config)} ${camelCase(
1717
convertType(type, context.config)
1818
)};\n`
1919
);
2020
});
21+
this.write(`\n`);
2122
this.write(`}\n`);
2223
this.write(`\n`);
2324
super.visitUnion(context);

0 commit comments

Comments
 (0)