@Override protected boolean addClassImport(CtTypeReference<?> ref) { boolean shouldTypeBeImported = this.shouldTypeBeImported(ref); if (shouldTypeBeImported) { return super.addClassImport(ref); } else { return false; } }
@Override public <T> void visitCtInterface(CtInterface<T> intrface) { addClassImport(intrface.getReference()); for (CtTypeMember t : intrface.getTypeMembers()) { if (!(t instanceof CtType)) { continue; } addClassImport(((CtType) t).getReference()); } super.visitCtInterface(intrface); }
@Override public <T> void visitCtClass(CtClass<T> ctClass) { addClassImport(ctClass.getReference()); for (CtTypeMember t : ctClass.getTypeMembers()) { if (!(t instanceof CtType)) { continue; } addClassImport(((CtType) t).getReference()); } super.visitCtClass(ctClass); }
@Override public <T> void visitCtCatchVariable(CtCatchVariable<T> catchVariable) { for (CtTypeReference<?> type : catchVariable.getMultiTypes()) { addClassImport(type); } super.visitCtCatchVariable(catchVariable); }
@Override public <A extends Annotation> void visitCtAnnotationType( CtAnnotationType<A> annotationType) { addClassImport(annotationType.getReference()); super.visitCtAnnotationType(annotationType); }
@Override public <T extends Enum<?>> void visitCtEnum(CtEnum<T> ctEnum) { addClassImport(ctEnum.getReference()); super.visitCtEnum(ctEnum); }
/** * 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 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); } }
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; }