|
8 | 8 |
|
9 | 9 | package org.elasticsearch.gradle.internal.release;
|
10 | 10 |
|
| 11 | +import com.github.javaparser.GeneratedJavaParserConstants; |
11 | 12 | import com.github.javaparser.StaticJavaParser;
|
12 | 13 | import com.github.javaparser.ast.CompilationUnit;
|
13 | 14 | import com.github.javaparser.ast.NodeList;
|
|
16 | 17 | import com.github.javaparser.ast.body.VariableDeclarator;
|
17 | 18 | import com.github.javaparser.ast.expr.FieldAccessExpr;
|
18 | 19 | import com.github.javaparser.ast.expr.NameExpr;
|
| 20 | +import com.github.javaparser.ast.observer.ObservableProperty; |
| 21 | +import com.github.javaparser.printer.ConcreteSyntaxModel; |
| 22 | +import com.github.javaparser.printer.concretesyntaxmodel.CsmElement; |
19 | 23 | import com.github.javaparser.printer.lexicalpreservation.LexicalPreservingPrinter;
|
20 | 24 | import com.google.common.annotations.VisibleForTesting;
|
21 | 25 |
|
|
28 | 32 | import org.gradle.initialization.layout.BuildLayout;
|
29 | 33 |
|
30 | 34 | import java.io.IOException;
|
| 35 | +import java.lang.reflect.Field; |
31 | 36 | import java.nio.file.Files;
|
32 | 37 | import java.nio.file.Path;
|
33 | 38 | import java.nio.file.StandardOpenOption;
|
|
43 | 48 | import javax.annotation.Nullable;
|
44 | 49 | import javax.inject.Inject;
|
45 | 50 |
|
| 51 | +import static com.github.javaparser.ast.observer.ObservableProperty.TYPE_PARAMETERS; |
| 52 | +import static com.github.javaparser.printer.concretesyntaxmodel.CsmConditional.Condition.FLAG; |
| 53 | +import static com.github.javaparser.printer.concretesyntaxmodel.CsmElement.block; |
| 54 | +import static com.github.javaparser.printer.concretesyntaxmodel.CsmElement.child; |
| 55 | +import static com.github.javaparser.printer.concretesyntaxmodel.CsmElement.comma; |
| 56 | +import static com.github.javaparser.printer.concretesyntaxmodel.CsmElement.comment; |
| 57 | +import static com.github.javaparser.printer.concretesyntaxmodel.CsmElement.conditional; |
| 58 | +import static com.github.javaparser.printer.concretesyntaxmodel.CsmElement.list; |
| 59 | +import static com.github.javaparser.printer.concretesyntaxmodel.CsmElement.newline; |
| 60 | +import static com.github.javaparser.printer.concretesyntaxmodel.CsmElement.none; |
| 61 | +import static com.github.javaparser.printer.concretesyntaxmodel.CsmElement.sequence; |
| 62 | +import static com.github.javaparser.printer.concretesyntaxmodel.CsmElement.space; |
| 63 | +import static com.github.javaparser.printer.concretesyntaxmodel.CsmElement.string; |
| 64 | +import static com.github.javaparser.printer.concretesyntaxmodel.CsmElement.token; |
| 65 | + |
46 | 66 | public class UpdateVersionsTask extends DefaultTask {
|
| 67 | + |
| 68 | + static { |
| 69 | + replaceDefaultJavaParserClassCsm(); |
| 70 | + } |
| 71 | + |
| 72 | + /* |
| 73 | + * The default JavaParser CSM which it uses to format any new declarations added to a class |
| 74 | + * inserts two newlines after each declaration. Our version classes only have one newline. |
| 75 | + * In order to get javaparser lexical printer to use our format, we have to completely replace |
| 76 | + * the statically declared CSM pattern using hacky reflection |
| 77 | + * to access the static map where these are stored, and insert a replacement that is identical |
| 78 | + * apart from only one newline at the end of each member declaration, rather than two. |
| 79 | + */ |
| 80 | + private static void replaceDefaultJavaParserClassCsm() { |
| 81 | + try { |
| 82 | + Field classCsms = ConcreteSyntaxModel.class.getDeclaredField("concreteSyntaxModelByClass"); |
| 83 | + classCsms.setAccessible(true); |
| 84 | + @SuppressWarnings({ "unchecked", "rawtypes" }) |
| 85 | + Map<Class, CsmElement> csms = (Map) classCsms.get(null); |
| 86 | + |
| 87 | + // copied from the static initializer in ConcreteSyntaxModel |
| 88 | + csms.put( |
| 89 | + ClassOrInterfaceDeclaration.class, |
| 90 | + sequence( |
| 91 | + comment(), |
| 92 | + list(ObservableProperty.ANNOTATIONS, newline(), none(), newline()), |
| 93 | + list(ObservableProperty.MODIFIERS, space(), none(), space()), |
| 94 | + conditional( |
| 95 | + ObservableProperty.INTERFACE, |
| 96 | + FLAG, |
| 97 | + token(GeneratedJavaParserConstants.INTERFACE), |
| 98 | + token(GeneratedJavaParserConstants.CLASS) |
| 99 | + ), |
| 100 | + space(), |
| 101 | + child(ObservableProperty.NAME), |
| 102 | + list( |
| 103 | + TYPE_PARAMETERS, |
| 104 | + sequence(comma(), space()), |
| 105 | + string(GeneratedJavaParserConstants.LT), |
| 106 | + string(GeneratedJavaParserConstants.GT) |
| 107 | + ), |
| 108 | + list( |
| 109 | + ObservableProperty.EXTENDED_TYPES, |
| 110 | + sequence(string(GeneratedJavaParserConstants.COMMA), space()), |
| 111 | + sequence(space(), token(GeneratedJavaParserConstants.EXTENDS), space()), |
| 112 | + none() |
| 113 | + ), |
| 114 | + list( |
| 115 | + ObservableProperty.IMPLEMENTED_TYPES, |
| 116 | + sequence(string(GeneratedJavaParserConstants.COMMA), space()), |
| 117 | + sequence(space(), token(GeneratedJavaParserConstants.IMPLEMENTS), space()), |
| 118 | + none() |
| 119 | + ), |
| 120 | + space(), |
| 121 | + block(sequence(newline(), list(ObservableProperty.MEMBERS, sequence(newline()/*, newline()*/), newline(), newline()))) |
| 122 | + ) |
| 123 | + ); |
| 124 | + } catch (ReflectiveOperationException e) { |
| 125 | + throw new AssertionError(e); |
| 126 | + } |
| 127 | + } |
| 128 | + |
47 | 129 | private static final Logger LOGGER = Logging.getLogger(UpdateVersionsTask.class);
|
48 | 130 |
|
49 | 131 | static final String SERVER_MODULE_PATH = "server/src/main/java/";
|
|
0 commit comments