本文整理了Java中com.github.javaparser.ast.CompilationUnit.getPackageDeclaration()
方法的一些代码示例,展示了CompilationUnit.getPackageDeclaration()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。CompilationUnit.getPackageDeclaration()
方法的具体详情如下:
包路径:com.github.javaparser.ast.CompilationUnit
类名称:CompilationUnit
方法名:getPackageDeclaration
暂无
代码示例来源:origin: com.github.javaparser/javaparser-symbol-solver-core
static String getPackageName(Node container) {
if (container instanceof CompilationUnit) {
Optional<PackageDeclaration> p = ((CompilationUnit) container).getPackageDeclaration();
if (p.isPresent()) {
return p.get().getName().toString();
}
} else if (container != null) {
return getPackageName(container.getParentNode().orElse(null));
}
return "";
}
代码示例来源:origin: benas/jql
public void index(com.github.javaparser.ast.CompilationUnit compilationUnit, String fileName) {
String packageName = compilationUnit.getPackageDeclaration().map(NodeWithName::getNameAsString).orElse("");
io.github.benas.jql.model.CompilationUnit cu = new io.github.benas.jql.model.CompilationUnit(fileName, packageName);
int cuId = compilationUnitDao.save(cu);
List<TypeDeclaration<?>> types = compilationUnit.getTypes();
for (TypeDeclaration<?> type : types) {
typeIndexer.index(type, cuId);
}
}
代码示例来源:origin: javaparser/javasymbolsolver
static String getPackageName(Node container) {
if (container instanceof CompilationUnit) {
Optional<PackageDeclaration> p = ((CompilationUnit) container).getPackageDeclaration();
if (p.isPresent()) {
return p.get().getName().toString();
}
} else if (container != null) {
return getPackageName(getParentNode(container));
}
return "";
}
代码示例来源:origin: com.github.javaparser/java-symbol-solver-core
static String getPackageName(Node container) {
if (container instanceof CompilationUnit) {
Optional<PackageDeclaration> p = ((CompilationUnit) container).getPackageDeclaration();
if (p.isPresent()) {
return p.get().getName().toString();
}
} else if (container != null) {
return getPackageName(getParentNode(container));
}
return "";
}
代码示例来源:origin: com.paypal.butterfly/butterfly-utilities
@SuppressWarnings("PMD.SimplifyBooleanReturns")
private boolean isImported(CompilationUnit compilationUnit, String typeSimpleName) {
if (StringUtils.isBlank(typeSimpleName) || typeSimpleName.contains(".")) {
throw new IllegalArgumentException("Invalid type simple name");
}
// If the specified type is part of the JDK,
// then it won't need need an explicit import statement
if (specifiedTypePackageName.startsWith("java.lang")) {
return true;
}
// Check if the compilation unit has an explicit import declaration whose
// type name matches the specified type simple name
String importClass;
for (ImportDeclaration importDeclaration : compilationUnit.getImports()) {
importClass = importDeclaration.getName().getIdentifier();
if (importClass.equals(typeSimpleName)) {
return true;
}
}
// Check if the annotation is declared
// at the same package where the class is
if (compilationUnit.getPackageDeclaration().get().getNameAsString().equals(specifiedTypePackageName)) {
return true;
}
return false;
}
代码示例来源:origin: benas/jql
public void calculate(ClassOrInterfaceDeclaration classOrInterfaceDeclaration, CompilationUnit compilationUnit) {
List<AnnotationExpr> annotations = classOrInterfaceDeclaration.getAnnotations();
for (AnnotationExpr annotation : annotations) {
String annotationName = annotation.getNameAsString();
String annotationPackageName = annotation
.findCompilationUnit()
.flatMap(CompilationUnit::getPackageDeclaration)
.flatMap(pkg -> Optional.of(pkg.getNameAsString())).orElse("???");
if (typeDao.exist(annotationName, annotationPackageName)) { // JDK annotations are not indexed
int annotationId = typeDao.getId(annotationName, annotationPackageName);
int typeId = typeDao.getId(classOrInterfaceDeclaration.getNameAsString(), compilationUnit.getPackageDeclaration().get().getNameAsString());
annotatedWithDao.save(new AnnotatedWith(typeId, annotationId));
}
}
}
}
代码示例来源:origin: benas/jql
public void calculate(ClassOrInterfaceDeclaration classOrInterfaceDeclaration, CompilationUnit compilationUnit) {
List<ClassOrInterfaceType> extendedTypes = classOrInterfaceDeclaration.getExtendedTypes();
for (ClassOrInterfaceType extendedType : extendedTypes) {
String extendedTypeName = extendedType.getNameAsString();
String extendedTypePackageName = extendedType
.findCompilationUnit()
.flatMap(CompilationUnit::getPackageDeclaration)
.flatMap(pkg -> Optional.of(pkg.getNameAsString())).orElse("???");
if (typeDao.exist(extendedTypeName, extendedTypePackageName)) { // JDK interfaces are not indexed
int extendedInterfaceId = typeDao.getId(extendedTypeName, extendedTypePackageName);
int interfaceId = typeDao.getId(classOrInterfaceDeclaration.getNameAsString(), compilationUnit.getPackageDeclaration().get().getNameAsString());
extendsDao.save(new Extends(interfaceId, extendedInterfaceId));
}
}
}
}
代码示例来源:origin: com.paypal.butterfly/butterfly-utilities
@Override
@edu.umd.cs.findbugs.annotations.SuppressFBWarnings (value="NP_ALWAYS_NULL_EXCEPTION")
protected TUExecutionResult execution(File transformedAppFolder, TransformationContext transformationContext) {
File javaClassFile = getAbsoluteFile(transformedAppFolder, transformationContext);
FileInputStream fileInputStream = null;
TUExecutionResult result = null;
// TODO
// Add a validation here simply checking if the file name ends with .java
try {
fileInputStream = new FileInputStream(javaClassFile);
CompilationUnit compilationUnit = JavaParser.parse(fileInputStream);
Optional<PackageDeclaration> packageDeclaration = compilationUnit.getPackageDeclaration();
result = TUExecutionResult.value(this, packageDeclaration.get().getNameAsString());
} catch (Exception e) {
TransformationUtilityException tue = new TransformationUtilityException("An error happened when trying to read and parse the specified Java file " + getRelativePath(transformedAppFolder, javaClassFile), e);
result = TUExecutionResult.error(this, tue);
} finally {
try {
if (fileInputStream != null) {
fileInputStream.close();
}
} catch (IOException e) {
result.addWarning(e);
}
}
return result;
}
代码示例来源:origin: gradle.plugin.com.github.honourednihilist/gradle-kafka-deserializers
info.setCompilationUnit(JavaParser.parse(file));
info.setPackageName(info.getCompilationUnit()
.getPackageDeclaration()
.map(PackageDeclaration::getNameAsString)
.orElse(null));
代码示例来源:origin: benas/jql
public void calculate(ClassOrInterfaceDeclaration classOrInterfaceDeclaration, CompilationUnit compilationUnit) {
List<ClassOrInterfaceType> implementedInterfaces = classOrInterfaceDeclaration.getImplementedTypes();
for (ClassOrInterfaceType implementedInterface : implementedInterfaces) {
String implementedInterfaceName = implementedInterface.getNameAsString();
String implementedInterfacePackageName = implementedInterface
.findCompilationUnit()
.flatMap(CompilationUnit::getPackageDeclaration)
.flatMap(pkg -> Optional.of(pkg.getNameAsString())).orElse("???");
if (typeDao.exist(implementedInterfaceName, implementedInterfacePackageName)) { // JDK interfaces are not indexed
int interfaceId = typeDao.getId(implementedInterfaceName, implementedInterfacePackageName);
String cuPackageName = compilationUnit.getPackageDeclaration().get().getNameAsString();
int nbClasses = typeDao.count(classOrInterfaceDeclaration.getNameAsString(), cuPackageName);
if (nbClasses > 1) {
System.err.println("More than one class having the same name '" + classOrInterfaceDeclaration.getName() + "' and package '" + cuPackageName + "'");
} else {
int classId = typeDao.getId(classOrInterfaceDeclaration.getNameAsString(), cuPackageName);
implementsDao.save(new Implements(classId, interfaceId));
}
}
}
}
}
代码示例来源:origin: gradle.plugin.org.javamodularity/moduleplugin
for (var sourceFile : testSourceSet.getJava()) {
try {
Optional<PackageDeclaration> optionalPackageDeclaration = JavaParser.parse(sourceFile).getPackageDeclaration();
if (optionalPackageDeclaration.isPresent()) {
PackageDeclaration packageDeclaration = optionalPackageDeclaration.get();
代码示例来源:origin: treeleafj/xDoc
final Class<?> moduleType = Class.forName(cu.getPackageDeclaration().get().getNameAsString() + "." + typeDeclaration.getNameAsString());
IgnoreApi ignoreApi = moduleType.getAnnotation(IgnoreApi.class);
if (ignoreApi == null) {
final Class<?> moduleType = Class.forName(cu.getPackageDeclaration().get().getNameAsString() + "." + typeDeclaration.getNameAsString());
代码示例来源:origin: javaparser/javasymbolsolver
String importString = importDecl.getNameAsString();
if (this.wrappedNode.getPackageDeclaration().isPresent()
&& this.wrappedNode.getPackageDeclaration().get().getName().getIdentifier().equals(packageName(importString))
&& this.wrappedNode.getTypes().stream().anyMatch(it -> it.getName().getIdentifier().equals(toSimpleName(importString)))) {
代码示例来源:origin: com.github.javaparser/java-symbol-solver-core
String importString = importDecl.getNameAsString();
if (this.wrappedNode.getPackageDeclaration().isPresent()
&& this.wrappedNode.getPackageDeclaration().get().getName().getIdentifier().equals(packageName(importString))
&& this.wrappedNode.getTypes().stream().anyMatch(it -> it.getName().getIdentifier().equals(toSimpleName(importString)))) {
代码示例来源:origin: com.github.javaparser/javaparser-symbol-solver-core
String importString = importDecl.getNameAsString();
if (this.wrappedNode.getPackageDeclaration().isPresent()
&& this.wrappedNode.getPackageDeclaration().get().getName().getIdentifier().equals(packageName(importString))
&& this.wrappedNode.getTypes().stream().anyMatch(it -> it.getName().getIdentifier().equals(toSimpleName(importString)))) {
代码示例来源:origin: treeleafj/xDoc
@Override
public DocTag converter(String comment) {
DocTag docTag = super.converter(comment);
String path = ClassMapperUtils.getPath((String) docTag.getValues());
if (StringUtils.isBlank(path)) {
return null;
}
Class<?> returnClassz;
CompilationUnit cu;
try (FileInputStream in = new FileInputStream(path)) {
cu = JavaParser.parse(in);
if (cu.getTypes().size() <= 0) {
return null;
}
returnClassz = Class.forName(cu.getPackageDeclaration().get().getNameAsString() + "." + cu.getTypes().get(0).getNameAsString());
} catch (Exception e) {
log.warn("读取java原文件失败:{}", path, e.getMessage());
return null;
}
String text = cu.getComment().isPresent() ? CommentUtils.parseCommentText(cu.getComment().get().getContent()) : "";
Map<String, String> commentMap = this.analysisFieldComments(returnClassz);
List<FieldInfo> fields = this.analysisFields(returnClassz, commentMap);
ObjectInfo objectInfo = new ObjectInfo();
objectInfo.setType(returnClassz);
objectInfo.setFieldInfos(fields);
objectInfo.setComment(text);
return new SeeTagImpl(docTag.getTagName(), objectInfo);
}
代码示例来源:origin: com.github.davidmoten/state-machine-generator
PrintStream s = new PrintStream(bytes);
String newPkg = cu.getPackageDeclaration() //
代码示例来源:origin: davidmoten/state-machine
PrintStream s = new PrintStream(bytes);
String newPkg = cu.getPackageDeclaration() //
代码示例来源:origin: com.github.javaparser/java-symbol-solver-core
if (this.wrappedNode.getPackageDeclaration().isPresent()) {
String qName = this.wrappedNode.getPackageDeclaration().get().getName().toString() + "." + name;
SymbolReference<ResolvedReferenceTypeDeclaration> ref = typeSolver.tryToSolveType(qName);
if (ref.isSolved()) {
代码示例来源:origin: javaparser/javasymbolsolver
if (this.wrappedNode.getPackageDeclaration().isPresent()) {
String qName = this.wrappedNode.getPackageDeclaration().get().getName().toString() + "." + name;
SymbolReference<ResolvedReferenceTypeDeclaration> ref = typeSolver.tryToSolveType(qName);
if (ref.isSolved()) {
内容来源于网络,如有侵权,请联系作者删除!