public function build()
 {
     $annotationReader = $this->annotationReader;
     if (null === $annotationReader) {
         $annotationReader = new AnnotationReader();
         if (null !== $this->cacheDir) {
             $this->createDir($this->cacheDir . '/annotations');
             $annotationsCache = new FilesystemCache($this->cacheDir . '/annotations');
             $annotationReader = new CachedReader($annotationReader, $annotationsCache, $this->debug);
         }
     }
     $metadataDriver = $this->driverFactory->createDriver($this->metadataDirs, $annotationReader);
     $metadataFactory = new MetadataFactory($metadataDriver, null, $this->debug);
     $metadataFactory->setIncludeInterfaces($this->includeInterfaceMetadata);
     if (null !== $this->cacheDir) {
         $this->createDir($this->cacheDir . '/metadata');
         $metadataFactory->setCache(new FileCache($this->cacheDir . '/metadata'));
     }
     if (!$this->handlersConfigured) {
         $this->addDefaultHandlers();
     }
     if (!$this->listenersConfigured) {
         $this->addDefaultListeners();
     }
     if (!$this->visitorsAdded) {
         $this->addDefaultSerializationVisitors();
         $this->addDefaultDeserializationVisitors();
     }
     $serializer = new Serializer($metadataFactory, $this->handlerRegistry, $this->objectConstructor ?: new UnserializeObjectConstructor(), $this->serializationVisitors, $this->deserializationVisitors, $this->eventDispatcher);
     if (null !== $this->serializationContextFactory) {
         $serializer->setSerializationContextFactory($this->serializationContextFactory);
     }
     if (null !== $this->deserializationContextFactory) {
         $serializer->setDeserializationContextFactory($this->deserializationContextFactory);
     }
     return $serializer;
 }