本文整理汇总了Java中com.github.javaparser.ast.CompilationUnit.addClass方法的典型用法代码示例。如果您正苦于以下问题:Java CompilationUnit.addClass方法的具体用法?Java CompilationUnit.addClass怎么用?Java CompilationUnit.addClass使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.github.javaparser.ast.CompilationUnit
的用法示例。
在下文中一共展示了CompilationUnit.addClass方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testGetClassNameFromMethod
import com.github.javaparser.ast.CompilationUnit; //导入方法依赖的package包/类
@Test
public void testGetClassNameFromMethod() {
//Code from: https://github.com/javaparser/javaparser/wiki/Manual
CompilationUnit cu = new CompilationUnit();
// set the package
cu.setPackageDeclaration(new PackageDeclaration(Name.parse("com.aspectsecurity.example")));
// or a shortcut
cu.setPackageDeclaration("com.aspectsecurity.example");
// create the type declaration
ClassOrInterfaceDeclaration type = cu.addClass("GeneratedClass");
// create a method
EnumSet<Modifier> modifiers = EnumSet.of(Modifier.PUBLIC);
MethodDeclaration method = new MethodDeclaration(modifiers, new VoidType(), "main");
modifiers.add(Modifier.STATIC);
method.setModifiers(modifiers);
type.addMember(method);
assertEquals("GeneratedClass", saa.getClassNameFromMethod(method));
}
示例2: CodeStubBuilder
import com.github.javaparser.ast.CompilationUnit; //导入方法依赖的package包/类
public CodeStubBuilder(String className) {
compilationUnit = new CompilationUnit();
this.className = className;
filename = String.format("src/%s.java", className);
clazz = compilationUnit.addClass(className);
}
示例3: create
import com.github.javaparser.ast.CompilationUnit; //导入方法依赖的package包/类
static ClassWrapper create(AbstractGenerator g, String packageName, String className) {
CompilationUnit cu = new CompilationUnit();
cu.setPackageDeclaration(new PackageDeclaration(Name.parse(packageName)));
// create the type declaration
ClassOrInterfaceDeclaration type = cu.addClass(className);
return new ClassWrapper(g, packageName, className, cu, type);
}
示例4: generateCompilationUnitFromCloudFormationSpec
import com.github.javaparser.ast.CompilationUnit; //导入方法依赖的package包/类
static CompilationUnit generateCompilationUnitFromCloudFormationSpec(CloudFormationSpecification spec,
ArrayInitializerExpr jsonSubTypesArrayExpr) {
if (spec.getResourceType().size() != 1) {
throw new RuntimeException("Expected exactly 1 resource per file");
}
Map.Entry<String, ResourceTypeSpecification> resourceTypeNameAndSpec =
spec.getResourceType().entrySet().iterator().next();
// resourceCanonicalName is a name like "AWS::EC2::Instance", used in the JsonSubTypes.Type annotation and
// Javadoc
String resourceCanonicalName = resourceTypeNameAndSpec.getKey();
// resourceClassName is a shortened form, like "EC2Instance", to serve as the name of the generated Java
// class
String resourceClassName = resourceCanonicalName.substring("AWS::".length()).replace("::", "");
CompilationUnit compilationUnit = new CompilationUnit();
compilationUnit.setPackageDeclaration("com.salesforce.cf2pojo.model")
.addImport(JsonProperty.class);
final ClassOrInterfaceDeclaration resourceClass = compilationUnit.addClass(resourceClassName);
// The class declaration is something like "public class EC2Instance extends
// ResourceBase<EC2Instance.Properties>"
resourceClass.setName(resourceClassName).addModifier(Modifier.PUBLIC)
.addExtendedType(String.format("ResourceBase<%s.Properties>", resourceClassName));
// Add an @Generated annotation to the class
final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
resourceClass.addAndGetAnnotation(Generated.class)
.addPair("value", surroundWithQuotes(GenerateTask.class.getCanonicalName()))
.addPair("date", surroundWithQuotes(dateFormat.format(new Date())));
// Add constructor that initializes the properties field to a new Properties object
resourceClass.addConstructor(Modifier.PUBLIC).setBody(new BlockStmt(NodeList.nodeList(
new ExpressionStmt(
new AssignExpr(new NameExpr("properties"),
new ObjectCreationExpr(null, JavaParser.parseClassOrInterfaceType("Properties"),
new NodeList<>()), AssignExpr.Operator.ASSIGN)))));
// Generate the Properties nested class
ResourceTypeSpecification resourceSpec = resourceTypeNameAndSpec.getValue();
ClassOrInterfaceDeclaration resourcePropertiesClass = generatePropertiesClassFromTypeSpec("Properties",
compilationUnit, resourceCanonicalName, resourceSpec);
resourceClass.addMember(resourcePropertiesClass);
// Add a @JsonSubTypes.Type annotation for this resource type to ResourceBase class
jsonSubTypesArrayExpr.getValues().add(new NormalAnnotationExpr()
.addPair("value", resourceClassName + ".class")
.addPair("name", surroundWithQuotes(resourceCanonicalName))
.setName("JsonSubTypes.Type"));
// Generate nested classes for any subproperties of this resource type (e.g.,
// "EC2Instance.BlockDeviceMapping"). These are found in the "PropertyTypes" section of the spec file.
if (spec.getPropertyTypes() != null) {
for (Map.Entry<String, PropertyTypeSpecification> propertyTypeSpecEntry :
spec.getPropertyTypes().entrySet()) {
String subpropertyClassName = substringAfterFinalPeriod(propertyTypeSpecEntry.getKey());
ClassOrInterfaceDeclaration subpropertyClass = generatePropertiesClassFromTypeSpec(
subpropertyClassName, compilationUnit, propertyTypeSpecEntry.getKey(),
propertyTypeSpecEntry.getValue());
// Add the generated subproperty class as a static nested class of the resource class
resourceClass.addMember(subpropertyClass);
}
}
return compilationUnit;
}