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 ""; }
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); } }
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 ""; }
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 ""; }
@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; }
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)); } } } }
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)); } } } }
@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; }
info.setCompilationUnit(JavaParser.parse(file)); info.setPackageName(info.getCompilationUnit() .getPackageDeclaration() .map(PackageDeclaration::getNameAsString) .orElse(null));
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)); } } } } }
for (var sourceFile : testSourceSet.getJava()) { try { Optional<PackageDeclaration> optionalPackageDeclaration = JavaParser.parse(sourceFile).getPackageDeclaration(); if (optionalPackageDeclaration.isPresent()) { PackageDeclaration packageDeclaration = optionalPackageDeclaration.get();
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());
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)))) {
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)))) {
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)))) {
@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); }
PrintStream s = new PrintStream(bytes); String newPkg = cu.getPackageDeclaration() //
PrintStream s = new PrintStream(bytes); String newPkg = cu.getPackageDeclaration() //
if (this.wrappedNode.getPackageDeclaration().isPresent()) { String qName = this.wrappedNode.getPackageDeclaration().get().getName().toString() + "." + name; SymbolReference<ResolvedReferenceTypeDeclaration> ref = typeSolver.tryToSolveType(qName); if (ref.isSolved()) {
if (this.wrappedNode.getPackageDeclaration().isPresent()) { String qName = this.wrappedNode.getPackageDeclaration().get().getName().toString() + "." + name; SymbolReference<ResolvedReferenceTypeDeclaration> ref = typeSolver.tryToSolveType(qName); if (ref.isSolved()) {