本文整理汇总了Java中com.github.javaparser.ast.PackageDeclaration类的典型用法代码示例。如果您正苦于以下问题:Java PackageDeclaration类的具体用法?Java PackageDeclaration怎么用?Java PackageDeclaration使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
PackageDeclaration类属于com.github.javaparser.ast包,在下文中一共展示了PackageDeclaration类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testGetClassNameFromMethod
import com.github.javaparser.ast.PackageDeclaration; //导入依赖的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: extractClassName
import com.github.javaparser.ast.PackageDeclaration; //导入依赖的package包/类
@Nullable
private static String extractClassName( String code, PrintStream err ) {
InputStream inputStream = new ByteArrayInputStream( code.getBytes( StandardCharsets.UTF_8 ) );
try {
CompilationUnit compilationUnit = JavaParser.parse( inputStream );
List<TypeDeclaration> types = compilationUnit.getTypes();
if ( types.size() == 1 ) {
String simpleType = types.get( 0 ).getName();
return Optional.ofNullable( compilationUnit.getPackage() )
.map( PackageDeclaration::getPackageName )
.map( it -> it + "." + simpleType )
.orElse( simpleType );
} else if ( types.size() == 0 ) {
err.println( "No class definition found" );
} else {
err.println( "Too many class definitions found. Only one class can be defined at a time." );
}
} catch ( ParseException e ) {
// ignore error, let the compiler provide an error message
return "Err";
}
return null;
}
示例3: visit
import com.github.javaparser.ast.PackageDeclaration; //导入依赖的package包/类
@Override
public JCTree visit(final PackageDeclaration n, final Object arg) {
//ARG0: JCExpression
// It returns a full qualified name
JCExpression arg0 = (JCExpression) n.getName().accept(this, arg);
/* TODO - Not supporting annotations
if (n.getAnnotations() != null) {
for (final AnnotationExpr a : n.getAnnotations()) {
JCTree result = a.accept(this, arg);
}
}
*/
if (arg0 instanceof JCIdent) {
return new AJCIdent((JCIdent) arg0, ((n.getComment() != null) ? n.getComment().getContent() : null));
}
return new AJCFieldAccess((JCFieldAccess) arg0, ((n.getComment() != null) ? n.getComment().getContent() : null));
}
示例4: mergeContent
import com.github.javaparser.ast.PackageDeclaration; //导入依赖的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();
}
示例5: visit
import com.github.javaparser.ast.PackageDeclaration; //导入依赖的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;
}
示例6: solve
import com.github.javaparser.ast.PackageDeclaration; //导入依赖的package包/类
private void solve(Node node) {
if (node instanceof ClassOrInterfaceDeclaration) {
solveTypeDecl((ClassOrInterfaceDeclaration) node);
} else if (node instanceof Expression) {
if ((getParentNode(node) instanceof ImportDeclaration) || (getParentNode(node) instanceof Expression)
|| (getParentNode(node) instanceof MethodDeclaration)
|| (getParentNode(node) instanceof PackageDeclaration)) {
// skip
} else if ((getParentNode(node) instanceof Statement) || (getParentNode(node) instanceof VariableDeclarator)) {
try {
Type ref = JavaParserFacade.get(typeSolver).getType(node);
out.println(" Line " + node.getRange().get().begin.line + ") " + node + " ==> " + ref.describe());
ok++;
} catch (UnsupportedOperationException upe) {
unsupported++;
err.println(upe.getMessage());
throw upe;
} catch (RuntimeException re) {
ko++;
err.println(re.getMessage());
throw re;
}
}
}
}
示例7: solve
import com.github.javaparser.ast.PackageDeclaration; //导入依赖的package包/类
private void solve(Node node) {
if (node instanceof ClassOrInterfaceDeclaration) {
solveTypeDecl((ClassOrInterfaceDeclaration) node);
} else if (node instanceof Expression) {
if ((getParentNode(node) instanceof ImportDeclaration) || (getParentNode(node) instanceof Expression)
|| (getParentNode(node) instanceof MethodDeclaration)
|| (getParentNode(node) instanceof PackageDeclaration)) {
// skip
} else if ((getParentNode(node) instanceof Statement) || (getParentNode(node) instanceof VariableDeclarator)) {
try {
ResolvedType ref = JavaParserFacade.get(typeSolver).getType(node);
out.println(" Line " + node.getRange().get().begin.line + ") " + node + " ==> " + ref.describe());
ok++;
} catch (UnsupportedOperationException upe) {
unsupported++;
err.println(upe.getMessage());
throw upe;
} catch (RuntimeException re) {
ko++;
err.println(re.getMessage());
throw re;
}
}
}
}
示例8: visit
import com.github.javaparser.ast.PackageDeclaration; //导入依赖的package包/类
@Override
public void visit(MethodDeclaration n, Optional<PackageDeclaration> clazzPackage) {
/* CHECK METHOD-LEVEL ANNOTATIONS FOR URL AND HTTP METHOD */
// We found a new method to look at
logger.debug("Method Name: " + n.getName());
String methodClazzName = getClassNameFromMethod(n);
// Get all annotations on method
NodeList<AnnotationExpr> nodeList = n.getAnnotations();
for (AnnotationExpr annotation : nodeList) {
// Found an annotation on the method
logger.debug("Found annotation: " + annotation.getNameAsString());
if (annotation.getNameAsString().equals("RequestMapping")) {
String packageName = "";
if (clazzPackage.isPresent()) {
packageName = clazzPackage.get().getNameAsString();
}
Endpoint newEndpoint = handleRequestMappingFound(annotation, packageName, methodClazzName);
// Check method parameters since we have a RequestMapping
newEndpoint.setParams(handleMethodParameters(n.getParameters()));
SpringAPIIdentifier.addEndpoint(newEndpoint);
}
}
super.visit(n, clazzPackage);
}
示例9: execute
import com.github.javaparser.ast.PackageDeclaration; //导入依赖的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());
}
}
示例10: execute
import com.github.javaparser.ast.PackageDeclaration; //导入依赖的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());
}
}
示例11: setFromFile
import com.github.javaparser.ast.PackageDeclaration; //导入依赖的package包/类
/********************************
* 작성일 : 2016. 7. 4. 작성자 : KYJ
*
*
* @param voFile
* @throws IOException
* @throws FileNotFoundException
********************************/
public void setFromFile(File voFile) {
if (voFile == null || !voFile.exists())
return;
if (voFile.isFile()) {
CompilationUnit cu;
try (FileInputStream in = new FileInputStream(voFile)) {
// parse the file
cu = JavaParser.parse(in);
PackageDeclaration packageDeclaration = cu.getPackage();
txtPackageName.setText(packageDeclaration.getName().toString());
txtClassName.setText(voFile.getName().substring(0, voFile.getName().indexOf('.')));
txtLocation.setText(voFile.getAbsolutePath());
MethodVisitor methodVisitor = new MethodVisitor();
methodVisitor.visit(cu, null);
List<TableModelDVO> valideFieldMeta = methodVisitor.getValideFieldMeta(t -> {
TableModelDVO tableModelDVO = new TableModelDVO();
tableModelDVO.setName(t.getName());
tableModelDVO.setType(t.getFieldType().getSimpleName());
return tableModelDVO;
});
this.tbVoEditor.getItems().addAll(valideFieldMeta);
} catch (IOException | ParseException e) {
LOGGER.error(ValueUtil.toString(e));
}
} else {
txtLocation.setText(voFile.getAbsolutePath());
}
}
示例12: main
import com.github.javaparser.ast.PackageDeclaration; //导入依赖的package包/类
public static void main(String[] args) throws ParseException, IOException {
String fileName = "C:\\Users\\KYJ\\JAVA_FX\\gagoyleWorkspace\\VisualFxVoEditor\\src\\main\\java\\com\\kyj\\fx\\voeditor\\visual\\main\\model\\vo\\ClassPathEntry.java";
FileInputStream in = new FileInputStream(fileName);
CompilationUnit cu;
try {
// parse the file
cu = JavaParser.parse(in);
} finally {
in.close();
}
PackageDeclaration packageDeclaration = cu.getPackage();
// System.out.println(packageDeclaration.getName().toString());
// System.out.println();
// System.out.println(String.format("package name : %s",
// packageDeclaration.getName().getName()));
ClassMeta classMeta = new ClassMeta("");
classMeta.setPackageName(packageDeclaration.getName().toString());
ArrayList<FieldMeta> fields = new ArrayList<FieldMeta>();
VoEditor voEditor = new VoEditor(classMeta, fields);
List<Node> childrenNodes = cu.getChildrenNodes();
for (Node n : childrenNodes) {
}
new MethodVisitor().visit(cu, null);
}
示例13: createCU
import com.github.javaparser.ast.PackageDeclaration; //导入依赖的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;
}
示例14: create
import com.github.javaparser.ast.PackageDeclaration; //导入依赖的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);
}
示例15: createEnum
import com.github.javaparser.ast.PackageDeclaration; //导入依赖的package包/类
static ClassWrapper createEnum(AbstractGenerator g, String packageName, String className) {
CompilationUnit cu = new CompilationUnit();
cu.setPackageDeclaration(new PackageDeclaration(Name.parse(packageName)));
// create the type declaration
EnumDeclaration type = cu.addEnum(className);
ClassWrapper cw = new ClassWrapper(g, packageName, className, cu, type);
cw.setType(ClassWrapperType.enumClass);
return cw;
}