当前位置: 首页>>代码示例>>Java>>正文


Java CompilationUnit.setPackage方法代码示例

本文整理汇总了Java中com.github.javaparser.ast.CompilationUnit.setPackage方法的典型用法代码示例。如果您正苦于以下问题:Java CompilationUnit.setPackage方法的具体用法?Java CompilationUnit.setPackage怎么用?Java CompilationUnit.setPackage使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.github.javaparser.ast.CompilationUnit的用法示例。


在下文中一共展示了CompilationUnit.setPackage方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: mergeContent

import com.github.javaparser.ast.CompilationUnit; //导入方法依赖的package包/类
public static String mergeContent(CompilationUnit one, CompilationUnit two) throws Exception {

        // 包声明不同,返回null
        if (!one.getPackage().equals(two.getPackage())) return null;

        CompilationUnit cu = new CompilationUnit();

        // add package declaration to the compilation unit
        PackageDeclaration pd = new PackageDeclaration();
        pd.setName(one.getPackage().getName());
        cu.setPackage(pd);

        // check and merge file comment;
        Comment fileComment = mergeSelective(one.getComment(), two.getComment());
        cu.setComment(fileComment);

        // check and merge imports
        List<ImportDeclaration> ids = mergeListNoDuplicate(one.getImports(), two.getImports());
        cu.setImports(ids);

        // check and merge Types
        List<TypeDeclaration> types = mergeTypes(one.getTypes(), two.getTypes());
        cu.setTypes(types);

        return cu.toString();
    }
 
开发者ID:beihaifeiwu,项目名称:dolphin,代码行数:27,代码来源:JavaSourceUtils.java

示例2: visit

import com.github.javaparser.ast.CompilationUnit; //导入方法依赖的package包/类
@Override public Node visit(final CompilationUnit n, final A arg) {
	if (n.getPackage() != null) {
		n.setPackage((PackageDeclaration) n.getPackage().accept(this, arg));
	}
	final List<ImportDeclaration> imports = n.getImports();
	if (imports != null) {
		for (int i = 0; i < imports.size(); i++) {
			imports.set(i, (ImportDeclaration) imports.get(i).accept(this, arg));
		}
		removeNulls(imports);
	}
	final List<TypeDeclaration> types = n.getTypes();
	if (types != null) {
		for (int i = 0; i < types.size(); i++) {
			types.set(i, (TypeDeclaration) types.get(i).accept(this, arg));
		}
		removeNulls(types);
	}
	return n;
}
 
开发者ID:plum-umd,项目名称:java-sketch,代码行数:21,代码来源:ModifierVisitorAdapter.java

示例3: execute

import com.github.javaparser.ast.CompilationUnit; //导入方法依赖的package包/类
@Override
public void execute(PathResolver pathResolver) throws Exception {
    CompilationUnit cu = new CompilationUnit();
    String basePackage = BasePackageDetector.detect();
    cu.setPackage(new PackageDeclaration(ASTHelper.createNameExpr(basePackage + "form")));

    ClassOrInterfaceDeclaration formClass = new ClassOrInterfaceDeclaration(
            ModifierSet.PUBLIC, false, CaseConverter.pascalCase(tableName) + "Form");
    ASTHelper.addTypeDeclaration(cu, formClass);
    formClass.setExtends(Collections.singletonList(
            new ClassOrInterfaceType("FormBase")
    ));

    fields.stream()
            .filter(f -> !f.isId())
            .forEach(f -> ASTHelper.addMember(formClass, fieldDeclaration(f)));
    fields.stream()
            .filter(f -> !f.isId())
            .forEach(f -> ASTHelper.addMember(formClass, getterDeclaration(f)));
    fields.stream()
            .filter(f -> !f.isId())
            .forEach(f -> ASTHelper.addMember(formClass, setterDeclaration(f)));

    try (Writer writer = new OutputStreamWriter(pathResolver.destinationAsStream(destination))) {
        writer.write(cu.toString());
    }
}
 
开发者ID:kawasima,项目名称:enkan,代码行数:28,代码来源:FormTask.java

示例4: execute

import com.github.javaparser.ast.CompilationUnit; //导入方法依赖的package包/类
public void execute(PathResolver pathResolver) throws Exception {
    CompilationUnit cu = new CompilationUnit();
    cu.setPackage(new PackageDeclaration(ASTHelper.createNameExpr(pkgName)));
    cu.setImports(Arrays.asList(
            new ImportDeclaration(ASTHelper.createNameExpr("org.seasar.doma.jdbc.Config"), false, false),
            new ImportDeclaration(ASTHelper.createNameExpr("org.seasar.doma.jdbc.dialect.Dialect"), false, false),
            new ImportDeclaration(ASTHelper.createNameExpr("org.seasar.doma.jdbc.dialect.H2Dialect"), false, false),
            new ImportDeclaration(ASTHelper.createNameExpr("javax.sql.DataSource"), false, false)
    ));

    ClassOrInterfaceDeclaration type = new ClassOrInterfaceDeclaration(ModifierSet.PUBLIC, false, "DomaConfig");
    type.setImplements(Collections.singletonList(new ClassOrInterfaceType("Config")));
    ASTHelper.addTypeDeclaration(cu, type);


    MethodDeclaration getDataSourceMethod = new MethodDeclaration(ModifierSet.PUBLIC, ASTHelper.createReferenceType("DataSource", 0), "getDataSource");
    getDataSourceMethod.setAnnotations(Collections.singletonList(OVERRIDE_ANNOTATION));
    BlockStmt getDataSourceBody = new BlockStmt();
    ASTHelper.addStmt(getDataSourceBody, new ReturnStmt(new NullLiteralExpr()));
    getDataSourceMethod.setBody(getDataSourceBody);
    ASTHelper.addMember(type, getDataSourceMethod);

    MethodDeclaration getDialectMethod = new MethodDeclaration(ModifierSet.PUBLIC, ASTHelper.createReferenceType("Dialect", 0), "getDialect");
    getDialectMethod.setAnnotations(Collections.singletonList(OVERRIDE_ANNOTATION));
    BlockStmt getDialectBody = new BlockStmt();

    ObjectCreationExpr newDialect = new ObjectCreationExpr(null, new ClassOrInterfaceType("H2Dialect"), null);
    ASTHelper.addStmt(getDialectBody, new ReturnStmt(newDialect));
    getDialectMethod.setBody(getDialectBody);
    ASTHelper.addMember(type, getDialectMethod);

    try (OutputStreamWriter writer = new OutputStreamWriter(
            pathResolver.destinationAsStream(destination))) {
        writer.write(cu.toString());
    }
}
 
开发者ID:kawasima,项目名称:enkan,代码行数:37,代码来源:DomaConfigTask.java

示例5: createCU

import com.github.javaparser.ast.CompilationUnit; //导入方法依赖的package包/类
/**
 * creates the compilation unit
 */
private static CompilationUnit createCU() {
    CompilationUnit cu = new CompilationUnit();
    // set the package
    cu.setPackage(new PackageDeclaration(ASTHelper.createNameExpr("java.parser.test")));

    // create the type declaration
    ClassOrInterfaceDeclaration type = new ClassOrInterfaceDeclaration(ModifierSet.PUBLIC, false, "GeneratedClass");
    ASTHelper.addTypeDeclaration(cu, type);

    // create a method
    MethodDeclaration method = new MethodDeclaration(ModifierSet.PUBLIC, ASTHelper.VOID_TYPE, "main");
    method.setModifiers(ModifierSet.addModifier(method.getModifiers(), ModifierSet.STATIC));
    ASTHelper.addMember(type, method);

    // add a parameter to the method
    Parameter param = ASTHelper.createParameter(ASTHelper.createReferenceType("String", 0), "args");
    param.setVarArgs(true);
    ASTHelper.addParameter(method, param);

    // add a body to the method
    BlockStmt block = new BlockStmt();
    method.setBody(block);

    // add a statement do the method body
    NameExpr clazz = new NameExpr("System");
    FieldAccessExpr field = new FieldAccessExpr(clazz, "out");
    MethodCallExpr call = new MethodCallExpr(field, "println");
    ASTHelper.addArgument(call, new StringLiteralExpr("Hello World!"));
    ASTHelper.addStmt(block, call);

    return cu;
}
 
开发者ID:bingoohuang,项目名称:javacode-demo,代码行数:36,代码来源:ClassCreator.java

示例6: setup

import com.github.javaparser.ast.CompilationUnit; //导入方法依赖的package包/类
@Before
public void setup(){
  CompilationUnit cu = new CompilationUnit();
  // set the package
  cu.setPackage(new PackageDeclaration(ASTHelper.createNameExpr("java.parser.test")));

  // create the type declaration
  ClassOrInterfaceDeclaration type = new ClassOrInterfaceDeclaration(ModifierSet.PUBLIC, false, "GeneratedClass");
  ASTHelper.addTypeDeclaration(cu, type);

  // create a method
  MethodDeclaration method = new MethodDeclaration(ModifierSet.PUBLIC, ASTHelper.VOID_TYPE, "main");
  method.setModifiers(ModifierSet.addModifier(method.getModifiers(), ModifierSet.STATIC));
  ASTHelper.addMember(type, method);

  // add a parameter to the method
  Parameter param = ASTHelper.createParameter(ASTHelper.createReferenceType("String", 0), "args");
  param.setVarArgs(true);
  ASTHelper.addParameter(method, param);

  // add a body to the method
  BlockStmt block = new BlockStmt();
  method.setBody(block);

  // add a statement do the method body
  NameExpr clazz = new NameExpr("System");
  FieldAccessExpr field = new FieldAccessExpr(clazz, "out");
  MethodCallExpr call = new MethodCallExpr(field, "println");
  ASTHelper.addArgument(call, new StringLiteralExpr("Hello World!"));
  ASTHelper.addStmt(block, call);

  unit = cu;
}
 
开发者ID:beihaifeiwu,项目名称:dolphin,代码行数:34,代码来源:JavaparserTest.java

示例7: execute

import com.github.javaparser.ast.CompilationUnit; //导入方法依赖的package包/类
@Override
public void execute(PathResolver pathResolver) throws Exception {
    CompilationUnit cu = new CompilationUnit();
    cu.setPackage(new PackageDeclaration(ASTHelper.createNameExpr("db.migration")));
    List<ImportDeclaration> imports = new ArrayList<>();
    imports.add(new ImportDeclaration(ASTHelper.createNameExpr("org.flywaydb.core.api.migration.jdbc.JdbcMigration"), false, false));
    imports.add(new ImportDeclaration(ASTHelper.createNameExpr("java.sql.Connection"), false, false));
    imports.add(new ImportDeclaration(ASTHelper.createNameExpr("java.sql.Statement"), false, false));
    cu.setImports(imports);

    String version = LocalDateTime.now().format(VERSION_FMT);
    ClassOrInterfaceDeclaration migrationClass = new ClassOrInterfaceDeclaration(
            ModifierSet.PUBLIC, false, "V" + version + "__Create"
            + CaseConverter.pascalCase(tableName));
    ASTHelper.addTypeDeclaration(cu, migrationClass);

    List<ClassOrInterfaceType> implementList = new ArrayList<>();
    implementList.add(new ClassOrInterfaceType("JdbcMigration"));
    migrationClass.setImplements(implementList);
    MethodDeclaration migrateMethod = new MethodDeclaration(ModifierSet.PUBLIC, ASTHelper.VOID_TYPE, "migrate");
    ASTHelper.addMember(migrationClass, migrateMethod);
    ASTHelper.addParameter(migrateMethod, ASTHelper.createParameter(
            ASTHelper.createReferenceType("Connection", 0), "connection"
    ));
    migrateMethod.setThrows(Collections.singletonList(ASTHelper.createNameExpr("Exception")));

    List<AnnotationExpr> annotations = new ArrayList<>();
    annotations.add(new MarkerAnnotationExpr(ASTHelper.createNameExpr("Override")));
    migrateMethod.setAnnotations(annotations);

    BlockStmt block = new BlockStmt();
    migrateMethod.setBody(block);

    TryStmt tryStmt = new TryStmt(createStatementExecuteBlock(), null, null);
    tryStmt.setResources(Collections.singletonList(createAssignStatement()));
    ASTHelper.addStmt(block, tryStmt);

    try (Writer writer = new OutputStreamWriter(
            pathResolver.destinationAsStream(
                    "src/main/java/db/migration/" + migrationClass.getName() + ".java"))) {
        writer.write(cu.toString());
    }
}
 
开发者ID:kawasima,项目名称:enkan,代码行数:44,代码来源:FlywayTask.java

示例8: whenThePackageDeclarationIsSetTo

import com.github.javaparser.ast.CompilationUnit; //导入方法依赖的package包/类
@When("the package declaration is set to \"$packageName\"")
public void whenThePackageDeclarationIsSetTo(String packageName) {
    CompilationUnit compilationUnit = (CompilationUnit) state.get("cu1");
    compilationUnit.setPackage(new PackageDeclaration(new NameExpr(packageName)));
    state.put("cu1", compilationUnit);
}
 
开发者ID:plum-umd,项目名称:java-sketch,代码行数:7,代码来源:ManipulationSteps.java

示例9: doMerge

import com.github.javaparser.ast.CompilationUnit; //导入方法依赖的package包/类
@Override
public CompilationUnit doMerge(CompilationUnit first, CompilationUnit second) {

  CompilationUnit unit = new CompilationUnit();

  unit.setPackage(mergeSingle(first.getPackage(), second.getPackage()));

  unit.setImports(mergeCollections(first.getImports(), second.getImports()));

  unit.setTypes(mergeCollections(first.getTypes(), second.getTypes()));

  return unit;
}
 
开发者ID:beihaifeiwu,项目名称:dolphin,代码行数:14,代码来源:CompilationUnitMerger.java


注:本文中的com.github.javaparser.ast.CompilationUnit.setPackage方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。