@Override public <T> void visitCtFieldReference(CtFieldReference<T> reference) { enter(reference); scan(reference.getDeclaringType()); if (reference.isStatic()) { addFieldImport(reference); } else { scan(reference.getDeclaringType()); } exit(reference); }
/** * This method is used to check if the declaring type has been already imported, or if it is local * In both case we do not want to import it, even in FQN mode. * @param declaringType * @return true if it is local or imported */ private boolean declaringTypeIsLocalOrImported(CtTypeReference declaringType) { if (declaringType != null) { boolean isInCollision = isTypeInCollision(declaringType, false); if (!isInCollision) { boolean importSuccess = addClassImport(declaringType); if (importSuccess) { return true; } } boolean importedInClassImports = isImportedInClassImports(declaringType); boolean inJavaLang = classNamePresentInJavaLang(declaringType); if (importedInClassImports || inJavaLang) { return true; } while (declaringType != null) { if (declaringType.equals(targetType)) { return true; } declaringType = declaringType.getDeclaringType(); } } return false; }
@Override public <T> void visitCtExecutableReference(CtExecutableReference<T> reference) { enter(reference); if (reference.isStatic()) { addMethodImport(reference); } else if (reference.isConstructor()) { scan(reference.getDeclaringType()); } scan(reference.getActualTypeArguments()); exit(reference); }
private void reset() { printer.reset(); context = new PrintingContext(); if (env.isAutoImports()) { this.importsContext = new ImportScannerImpl(); } else { this.importsContext = new MinimalImportScanner(); } }
protected boolean addMethodImport(CtExecutableReference ref) { // static import is not supported below java 1.5 if (ref.getFactory().getEnvironment().getComplianceLevel() < 5) { return false; } if (this.isImportedInMethodImports(ref)) { return true; } // if the whole class is imported: no need to import the method. if (declaringTypeIsLocalOrImported(ref.getDeclaringType())) { return false; } if (this.isInCollisionWithLocalMethod(ref)) { return false; } methodImports.put(ref.getSimpleName(), ref); // if we are in the same package than target type, we also import class to avoid FQN in FQN mode. if (ref.getDeclaringType() != null) { if (ref.getDeclaringType().getPackage() != null) { if (ref.getDeclaringType().getPackage().equals(this.targetType.getPackage())) { addClassImport(ref.getDeclaringType()); } } } return true; }
return isImportedInClassImports(ref); if (this.isThereAnotherClassWithSameNameInAnotherPackage(ref)) { return false; if (reference != null && isImported(reference)) {
@Override public <T> void visitCtFieldReference(CtFieldReference<T> reference) { enterReference(reference); scan(reference.getDeclaringType()); // scan(reference.getType()); exitReference(reference); }
@Override public Collection<CtTypeReference<?>> computeImports(CtType<?> simpleType) { imports.clear(); addImport(simpleType.getReference()); scan(simpleType); return getImports(simpleType); }
@Override public <T> void visitCtExecutableReference( CtExecutableReference<T> reference) { enterReference(reference); if (reference.getDeclaringType() != null && reference.getDeclaringType().getDeclaringType() == null) { addImport(reference.getDeclaringType()); } scanReferences(reference.getActualTypeArguments()); exitReference(reference); }
@Override public void visitCtInvocation(CtInvocation invocation) { this.scan(invocation.getTypeCasts()); this.scan(invocation.getExecutable()); if (!this.isImportedInMethodImports(invocation.getExecutable())) { this.scan(invocation.getTarget()); } this.scan(invocation.getArguments()); }
@Override public <T> void visitCtTypeReference(CtTypeReference<T> reference) { if (!(reference instanceof CtArrayTypeReference)) { CtTypeReference typeReference; if (reference.getDeclaringType() == null) { typeReference = reference; } else { typeReference = reference.getAccessType(); } if (!typeReference.equals(reference)) { if (this.isAlreadyInUsedImport(reference)) { super.visitCtTypeReference(reference); return; } } if (!this.isTypeInCollision(typeReference, false)) { this.addClassImport(typeReference); } } super.visitCtTypeReference(reference); }
@Override public void computeImports(CtElement element) { //look for top declaring type of that simpleType if (element instanceof CtType) { CtType simpleType = (CtType) element; targetType = simpleType.getReference().getTopLevelType(); addClassImport(simpleType.getReference()); scan(simpleType); } else { CtType<?> type = element.getParent(CtType.class); targetType = type == null ? null : type.getReference().getTopLevelType(); scan(element); } }
@Override protected boolean addClassImport(CtTypeReference<?> ref) { boolean shouldTypeBeImported = this.shouldTypeBeImported(ref); if (shouldTypeBeImported) { return super.addClassImport(ref); } else { return false; } }
@Override public void computeImports(CtElement element) { imports.clear(); scan(element); }
/** * Adds a type to the imports. */ private boolean addImport(CtTypeReference<?> ref) { if (imports.containsKey(ref.getSimpleName())) { return isImported(ref); } imports.put(ref.getSimpleName(), ref); return true; } }
/** * Creates a new code generator visitor. */ public DefaultJavaPrettyPrinter(Environment env) { this.env = env; this.imports = new HashSet<>(); setPrinterTokenWriter(new DefaultTokenWriter(new PrinterHelper(env))); if (env.isAutoImports()) { this.importsContext = new ImportScannerImpl(); } else { this.importsContext = new MinimalImportScanner(); } }