|
3 | 3 |
|
4 | 4 | package com.microsoft.typespec.http.client.generator.core.customization; |
5 | 5 |
|
| 6 | +import com.github.javaparser.StaticJavaParser; |
6 | 7 | import com.github.javaparser.ast.CompilationUnit; |
7 | | -import java.lang.reflect.Modifier; |
8 | | -import java.util.List; |
9 | 8 | import java.util.function.Consumer; |
10 | 9 |
|
11 | 10 | /** |
12 | 11 | * The class level customization for an AutoRest generated class. |
13 | 12 | */ |
14 | | -public interface ClassCustomization extends CodeCustomization { |
15 | | - /** |
16 | | - * Gets the name of the class this customization is using. |
17 | | - * |
18 | | - * @return The name of the class. |
19 | | - */ |
20 | | - String getClassName(); |
| 13 | +public final class ClassCustomization extends CodeCustomization { |
| 14 | + private final String className; |
21 | 15 |
|
22 | | - /** |
23 | | - * Adds imports to the class. |
24 | | - * |
25 | | - * @param imports Imports to add. |
26 | | - * @return A new {@link ClassCustomization} updated with the new imports for chaining. |
27 | | - */ |
28 | | - ClassCustomization addImports(String... imports); |
29 | | - |
30 | | - /** |
31 | | - * Adds a static block to the class. The {@code staticCodeBlock} should include the static keyword followed by |
32 | | - * the static code. |
33 | | - * |
34 | | - * @param staticCodeBlock The static code block including the static keyword. |
35 | | - * @return The updated {@link ClassCustomization}. |
36 | | - */ |
37 | | - ClassCustomization addStaticBlock(String staticCodeBlock); |
38 | | - |
39 | | - /** |
40 | | - * Adds a static block to the class. |
41 | | - * |
42 | | - * @param staticCodeBlock The static code block. If this is {@code null} or an empty string, the class is not |
43 | | - * modified and the {@link ClassCustomization} instance is returned without any change. |
44 | | - * @param importsToAdd The list of imports to add to the class. |
45 | | - * @return The updated {@link ClassCustomization}. |
46 | | - */ |
47 | | - ClassCustomization addStaticBlock(String staticCodeBlock, List<String> importsToAdd); |
48 | | - |
49 | | - /** |
50 | | - * Gets the method level customization for a method in the class. |
51 | | - * |
52 | | - * @param methodNameOrSignature the method name or signature |
53 | | - * @return the method level customization |
54 | | - */ |
55 | | - MethodCustomization getMethod(String methodNameOrSignature); |
| 16 | + ClassCustomization(Editor editor, String packageName, String className) { |
| 17 | + super(editor, packageName, className); |
| 18 | + this.className = className; |
| 19 | + } |
56 | 20 |
|
57 | 21 | /** |
58 | | - * Gets the constructor level customization for a constructor in the class. |
59 | | - * <p> |
60 | | - * If only the constructor name is passed and the class has multiple constructors an error will be thrown to prevent |
61 | | - * ambiguous runtime behavior. |
62 | | - * |
63 | | - * @param constructorNameOrSignature The constructor name or signature. |
64 | | - * @return The constructor level customization. |
65 | | - * @throws IllegalStateException If only the constructor name is passed and the class has multiple constructors. |
66 | | - */ |
67 | | - ConstructorCustomization getConstructor(String constructorNameOrSignature); |
68 | | - |
69 | | - /** |
70 | | - * Gets the property level customization for a property in the class. |
71 | | - * <p> |
72 | | - * For constant properties use {@link #getConstant(String)}. |
73 | | - * |
74 | | - * @param propertyName the property name |
75 | | - * @return the property level customization |
76 | | - */ |
77 | | - PropertyCustomization getProperty(String propertyName); |
78 | | - |
79 | | - /** |
80 | | - * Gets the constant level customization for a constant in the class. |
81 | | - * <p> |
82 | | - * For instance properties use {@link #getProperty(String)}. |
83 | | - * |
84 | | - * @param constantName The constant name. |
85 | | - * @return The constant level customization. |
86 | | - */ |
87 | | - ConstantCustomization getConstant(String constantName); |
88 | | - |
89 | | - /** |
90 | | - * Gets the Javadoc customization for this class. |
91 | | - * |
92 | | - * @return the Javadoc customization |
93 | | - */ |
94 | | - JavadocCustomization getJavadoc(); |
95 | | - |
96 | | - /** |
97 | | - * Adds a constructor to this class. |
98 | | - * |
99 | | - * @param constructor The entire constructor as a literal string. |
100 | | - * @return The constructor level customization for the added constructor. |
101 | | - */ |
102 | | - ConstructorCustomization addConstructor(String constructor); |
103 | | - |
104 | | - /** |
105 | | - * Adds a constructor to this class. |
106 | | - * |
107 | | - * @param constructor The entire constructor as a literal string. |
108 | | - * @param importsToAdd Any additional imports required by the constructor. These will be custom types or types that |
109 | | - * are ambiguous on which to use such as {@code List} or the utility class {@code Arrays}. |
110 | | - * @return The constructor level customization for the added constructor. |
111 | | - */ |
112 | | - ConstructorCustomization addConstructor(String constructor, List<String> importsToAdd); |
113 | | - |
114 | | - /** |
115 | | - * Adds a method to this class. |
116 | | - * |
117 | | - * @param method The entire method as a literal string. |
118 | | - * @return The method level customization for the added method. |
119 | | - */ |
120 | | - MethodCustomization addMethod(String method); |
121 | | - |
122 | | - /** |
123 | | - * Adds a method to this class. |
124 | | - * |
125 | | - * @param method The entire method as a literal string. |
126 | | - * @param importsToAdd Any additional imports required by the constructor. These will be custom types or types that |
127 | | - * are ambiguous on which to use such as {@code List} or the utility class {@code Arrays}. |
128 | | - * @return The method level customization for the added method. |
129 | | - */ |
130 | | - MethodCustomization addMethod(String method, List<String> importsToAdd); |
131 | | - |
132 | | - /** |
133 | | - * Removes a method from this class. |
134 | | - * <p> |
135 | | - * If there exists multiple methods with the same name or signature only the first one found will be removed. |
136 | | - * <p> |
137 | | - * This method doesn't update usages of the method being removed. If the method was used elsewhere those usages will |
138 | | - * have to be updated or removed in another customization, or customizations. |
139 | | - * <p> |
140 | | - * If this removes the only method contained in the class this will result in a class with no methods. |
141 | | - * |
142 | | - * @param methodNameOrSignature The name or signature of the method being removed. |
143 | | - * @return The current ClassCustomization. |
144 | | - */ |
145 | | - ClassCustomization removeMethod(String methodNameOrSignature); |
146 | | - |
147 | | - /** |
148 | | - * Renames a class in the package. |
149 | | - * |
150 | | - * @param newName the new simple name for this class |
151 | | - * @return The current ClassCustomization. |
152 | | - */ |
153 | | - ClassCustomization rename(String newName); |
154 | | - |
155 | | - /** |
156 | | - * Replace the modifier for this class. |
157 | | - * <p> |
158 | | - * For compound modifiers such as {@code public abstract} use bitwise OR ({@code |}) of multiple Modifiers, {@code |
159 | | - * Modifier.PUBLIC | Modifier.ABSTRACT}. |
160 | | - * <p> |
161 | | - * Pass {@code 0} for {@code modifiers} to indicate that the method has no modifiers. |
162 | | - * |
163 | | - * @param modifiers The {@link Modifier Modifiers} for the class. |
164 | | - * @return The updated ClassCustomization object. |
165 | | - * @throws IllegalArgumentException If the {@code modifier} is less than {@code 0} or any {@link Modifier} included |
166 | | - * in the bitwise OR isn't a valid class {@link Modifier}. |
167 | | - */ |
168 | | - ClassCustomization setModifier(int modifiers); |
169 | | - |
170 | | - /** |
171 | | - * Add an annotation on the class. The annotation class will be automatically imported. |
172 | | - * |
173 | | - * @param annotation the annotation to add to the class. The leading @ can be omitted. |
174 | | - * @return the current class customization for chaining |
175 | | - */ |
176 | | - ClassCustomization addAnnotation(String annotation); |
177 | | - |
178 | | - /** |
179 | | - * Remove an annotation from the class. |
180 | | - * |
181 | | - * @param annotation the annotation to remove from the class. The leading @ can be omitted. |
182 | | - * @return the current class customization for chaining |
183 | | - */ |
184 | | - ClassCustomization removeAnnotation(String annotation); |
185 | | - |
186 | | - /** |
187 | | - * Rename an enum member if the current class is an enum class. |
| 22 | + * Gets the name of the class this customization is using. |
188 | 23 | * |
189 | | - * @param enumMemberName the current enum member name |
190 | | - * @param newName the new enum member name |
191 | | - * @return the current class customization for chaining |
| 24 | + * @return The name of the class. |
192 | 25 | */ |
193 | | - ClassCustomization renameEnumMember(String enumMemberName, String newName); |
| 26 | + public String getClassName() { |
| 27 | + return className; |
| 28 | + } |
194 | 29 |
|
195 | 30 | /** |
196 | 31 | * Allows for a fully controlled modification of the abstract syntax tree that represents this class. |
197 | 32 | * |
198 | 33 | * @param astCustomization The abstract syntax tree customization callback. |
199 | | - * @return A new ClassCustomization for this class with the abstract syntax tree changes applied. |
| 34 | + * @return This ClassCustomization with the abstract syntax tree changes applied. |
200 | 35 | */ |
201 | | - ClassCustomization customizeAst(Consumer<CompilationUnit> astCustomization); |
| 36 | + public ClassCustomization customizeAst(Consumer<CompilationUnit> astCustomization) { |
| 37 | + CompilationUnit astToEdit = StaticJavaParser.parse(editor.getFileContent(fileName)); |
| 38 | + astCustomization.accept(astToEdit); |
| 39 | + editor.replaceFile(fileName, astToEdit.toString()); |
202 | 40 |
|
203 | | - ClassCustomization refreshSymbol(); |
| 41 | + return this; |
| 42 | + } |
204 | 43 | } |
0 commit comments