/** * Make the imports for a given type. * */ private Collection<CtImport> computeImports(CtType<?> type) { context.currentTopLevel = type; importsContext.computeImports(context.currentTopLevel); return importsContext.getAllImports(); }
@Override public String toString() { DefaultJavaPrettyPrinter printer = new DefaultJavaPrettyPrinter(getFactory().getEnvironment()); String errorMessage = ""; try { // we do not want to compute imports of for CtImport and CtReference // as it may change the print of a reference if (!(this instanceof CtImport) && !(this instanceof CtReference)) { printer.getImportsContext().computeImports(this); } printer.scan(this); } catch (ParentNotInitializedException ignore) { LOGGER.error(ERROR_MESSAGE_TO_STRING, ignore); errorMessage = ERROR_MESSAGE_TO_STRING; } // in line-preservation mode, newlines are added at the beginning to matches the lines // removing them from the toString() representation return printer.toString().replaceFirst("^\\s+", "") + errorMessage; }
@Override public String printPackageInfo(CtPackage pack) { reset(); elementPrinterHelper.writeComment(pack); // we need to compute imports only for annotations // we don't want to get all imports coming from content of package for (CtAnnotation annotation : pack.getAnnotations()) { this.importsContext.computeImports(annotation); } elementPrinterHelper.writeAnnotations(pack); if (!pack.isUnnamedPackage()) { elementPrinterHelper.writePackageLine(pack.getQualifiedName()); } elementPrinterHelper.writeImports(this.importsContext.getAllImports()); return printer.getPrinterHelper().toString(); }
/** * Make the imports for all elements. */ public void computeImports(CtElement element) { if (env.isAutoImports()) { importsContext.computeImports(element); } }
/** * Make the imports for a given type. */ public Collection<CtTypeReference<?>> computeImports(CtType<?> type) { if (env.isAutoImports()) { context.currentTopLevel = type; return importsContext.computeImports(context.currentTopLevel); } return Collections.emptyList(); }
@Deprecated public static Set<CtType> getImport(CtType type) { if (!AmplificationHelper.importByClass.containsKey(type)) { ImportScanner importScanner = new ImportScannerImpl(); try { importScanner.computeImports(type); Set<CtType> set = importScanner.getAllImports() .stream() .map(CtImport::getReference) .filter(Objects::nonNull) .filter(ctElement -> ctElement instanceof CtType) .map(ctElement -> (CtType) ctElement) .collect(Collectors.toSet()); AmplificationHelper.importByClass.put(type, set); } catch (Exception e) { AmplificationHelper.importByClass.put(type, new HashSet<>(0)); } } return AmplificationHelper.importByClass.get(type); }
public void analyzeLib() { String sourcefile=this.faultyClass.getPosition().getFile().getAbsolutePath(); sourcefile = sourcefile.substring(0,sourcefile.lastIndexOf("/")); parseDirSingle(new File(sourcefile)); ImportScanner importContext = new ImportScannerImpl(); importContext.computeImports(this.faultyClass); Collection<CtImport> imports = importContext.getAllImports(); for (CtImport certainimport : imports) { String importedlibname=certainimport.getReference().toString(); if(importedlibname.indexOf(".")==-1) continue; if(certainimport.getImportKind()==CtImportKind.FIELD) { log.info("do not need to consider this case"); importedlibname=""; } else if(certainimport.getImportKind()==CtImportKind.TYPE) { importedlibname=importedlibname; } else if(certainimport.getImportKind()==CtImportKind.METHOD ||certainimport.getImportKind()==CtImportKind.ALL_TYPES) { importedlibname=importedlibname.substring(0,importedlibname.lastIndexOf(".")); } else if (certainimport.getImportKind()==CtImportKind.ALL_STATIC_MEMBERS) { log.info("temporily ignore this case"); importedlibname=""; } parseImportFile(importedlibname); } }