本文整理匯總了Java中com.github.javaparser.ast.CompilationUnit.addImport方法的典型用法代碼示例。如果您正苦於以下問題:Java CompilationUnit.addImport方法的具體用法?Java CompilationUnit.addImport怎麽用?Java CompilationUnit.addImport使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.github.javaparser.ast.CompilationUnit
的用法示例。
在下文中一共展示了CompilationUnit.addImport方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: generateJavaClasses
import com.github.javaparser.ast.CompilationUnit; //導入方法依賴的package包/類
@TaskAction
void generateJavaClasses() throws IOException, ClassNotFoundException {
if (!inputDir.isDirectory()) {
throw new RuntimeException("inputDir needs to be a directory");
}
CompilationUnit baseResourceCompilationUnit = JavaParser.parseResource("ResourceBase.java");
ClassOrInterfaceDeclaration baseResourceClass = baseResourceCompilationUnit.getClassByName("ResourceBase")
.orElseThrow(() -> new RuntimeException("Could not find ResourceBase class"));
// Add an @JsonSubTypes annotation to the ResourceBase class
ArrayInitializerExpr jsonSubTypesArrayExpr = new ArrayInitializerExpr();
SingleMemberAnnotationExpr jsonSubTypesAnnotation = new SingleMemberAnnotationExpr(
new Name(JsonSubTypes.class.getSimpleName()), jsonSubTypesArrayExpr);
baseResourceClass.addAnnotation(jsonSubTypesAnnotation);
baseResourceCompilationUnit.addImport(JsonSubTypes.class);
File[] inputFiles = inputDir.listFiles((dir, name) -> name.endsWith("Specification.json"));
if (inputFiles == null || inputFiles.length == 0) {
throw new RuntimeException(String.format("Did not find any spec files in directory %s",
inputDir.getName()));
}
ObjectMapper objectMapper = new ObjectMapper();
for (File inputFile : inputFiles) {
// Deserialize specification from JSON file
CloudFormationSpecification spec;
try (FileInputStream inputStream = new FileInputStream(inputFile)) {
spec = objectMapper.readValue(inputStream, CloudFormationSpecification.class);
} catch (JsonProcessingException e) {
LOGGER.info("Error deserializing file: {}. Skipping...", inputFile.getName());
continue;
}
// Generate the compilation unit from the specification
CompilationUnit resourceCompilationUnit = generateCompilationUnitFromCloudFormationSpec(spec,
jsonSubTypesArrayExpr);
// Write the compilation unit out to a file
String resourceClassName = resourceCompilationUnit.getType(0).getNameAsString();
File resourceClassFile = new File(generatedFileDir, resourceClassName + ".java");
writeFile(resourceClassFile, resourceCompilationUnit.toString());
}
// Write ResourceBase.java file
writeFile(new File(generatedFileDir, "ResourceBase.java"), baseResourceCompilationUnit.toString(
new CustomPrettyPrinterConfiguration().setArrayLiteralMembersOnSeparateLines(true)));
// Copy over static files like ValueType.java without modification
for (String fileName : STATIC_FILES) {
InputStream stream = getClass().getClassLoader().getResourceAsStream(fileName);
Files.copy(stream, new File(generatedFileDir, fileName).toPath());
stream.close();
}
}