/** * 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 void calculate(CtCompilationUnit sourceCompilationUnit, List<CtType<?>> types) { // reset the importsContext to avoid errors with multiple CU reset(); this.sourceCompilationUnit = sourceCompilationUnit; this.imports = new HashSet<>(); if (sourceCompilationUnit != null) { this.importsContext.initWithImports(sourceCompilationUnit.getImports()); } for (CtType<?> t : types) { imports.addAll(computeImports(t)); } this.writeHeader(types, imports); printTypes(types); }
@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; }
private boolean printQualified(CtTypeReference<?> ref) { if (importsContext.isImported(ref) // If my.pkg.Something is imported || (this.env.isAutoImports() && ref.getPackage() != null && "java.lang".equals(ref.getPackage().getSimpleName())) // or that we are in java.lang ) { for (CacheBasedConflictFinder typeContext : context.currentThis) { //A) we are in the context of a class which is also called "Something", if (typeContext.getSimpleName().equals(ref.getSimpleName()) && !Objects.equals(typeContext.getPackage(), ref.getPackage())) { return true; } //B) we are in the context of a class which defines field which is also called "Something", // we should still use qualified version my.pkg.Something if (typeContext.hasFieldConflict(ref.getSimpleName()) || typeContext.hasNestedTypeConflict(ref.getSimpleName()) // fix of #2369 ) { return true; } } return false; } else { return true; } }
/** * 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(); }
public void visitCtTypeParameterReference(CtTypeParameterReference ref) { printTypeAnnotations(ref); if (importsContext.isImported(ref)) { write(ref.getSimpleName()); } else { write(ref.getQualifiedName()); } if ((!context.isInvocation || "?".equals(ref.getSimpleName())) && !(ref.getBounds() == null) && !ref.getBounds().isEmpty() && !((ref.getBounds().size() == 1) && "java.lang.Object" .equals(ref.getBounds().get(0).getQualifiedName()))) { if (ref.isUpper()) { write(" extends "); } else { write(" super "); } for (CtTypeReference<?> b : ref.getBounds()) { scan(b); write(" & "); } removeLastChar(); } }
@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); } }
public void visitCtTypeReferenceWithoutGenerics(CtTypeReference<?> ref) { if (ref.isPrimitive()) { write(ref.getSimpleName()); return; } if (importsContext.isImported(ref) && ref.getPackage() != null) { printTypeAnnotations(ref); write(ref.getSimpleName()); } else { if (ref.getDeclaringType() != null) { if (!context.currentThis.contains(ref.getDeclaringType()) || ref.getModifiers().contains(ModifierKind.STATIC) || hasDeclaringTypeWithGenerics(ref)) { if (!context.ignoreEnclosingClass) { boolean ign = context.ignoreGenerics; context.ignoreGenerics = true; scan(ref.getDeclaringType()); write("."); context.ignoreGenerics = ign; } } write(ref.getSimpleName()); } else { write(ref.getQualifiedName()); } } }
@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 <T> void visitCtTypeReference(CtTypeReference<T> ref) { if (ref.isPrimitive()) { write(ref.getSimpleName()); return; } if (importsContext.isImported(ref) && ref.getPackage() != null) { printTypeAnnotations(ref); write(ref.getSimpleName()); } else { if (ref.getDeclaringType() != null) { if (!context.currentThis.contains(ref.getDeclaringType()) || ref.getModifiers().contains(ModifierKind.STATIC) || hasDeclaringTypeWithGenerics(ref)) { if (!context.ignoreEnclosingClass) { boolean ign = context.ignoreGenerics; context.ignoreGenerics = false; scan(ref.getDeclaringType()); write("."); context.ignoreGenerics = ign; } } write(ref.getSimpleName()); } else { write(ref.getQualifiedName()); } } if (!context.ignoreGenerics) { writeActualTypeArguments(ref); } }
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); } }