/**
 * Copyright (c) 2011 RedEngine Ltd, http://www.redengine.co.nz. All rights reserved.
 *
 * This program is licensed to you under the Apache License Version 2.0,
 * and you may not use this file except in compliance with the Apache License Version 2.0.
 * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the Apache License Version 2.0 is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
 */
package net.stickycode.configured.guice3;

import java.lang.reflect.Field;

import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.inject.TypeLiteral;
import com.google.inject.spi.TypeEncounter;
import com.google.inject.spi.TypeListener;

import net.stickycode.stereotype.Configured;
import net.stickycode.stereotype.StickyComponent;
import net.stickycode.stereotype.StickyFramework;

@StickyComponent
@StickyFramework
public class ConfiguredTypeListener
    implements TypeListener {
  private Logger log = LoggerFactory.getLogger(getClass());

  @Inject
  private ConfiguredInjector membersInjector;

  @Override
  public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) {
    if (typeIsConfigured(type)) {
      if (membersInjector == null)
        throw new AssertionError("On hearing " + type + " found that " + getClass().getSimpleName() + " was not injected with a " + ConfiguredInjector.class.getSimpleName());

      encounter.register(membersInjector);
      log.info("encountering {} registering injector {}", type, membersInjector);
    }
  }

  private <I> boolean typeIsConfigured(TypeLiteral<I> type) {
    for (Field field : type.getRawType().getDeclaredFields())
      if (field.isAnnotationPresent(Configured.class))
        return true;

    return false;
  }
}

@@ -788,17 +788,16 @@
 .Field;%0A
-%0A
 import j
@@ -803,210 +803,154 @@
 java
-x.inject.Inject;%0A%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0Aimport com.google.inject.TypeLiteral;%0Aimport com.google.inject.spi.TypeEncounter;%0Aimport com.google.inject.spi.TypeListener
+.lang.reflect.Method;%0A%0Aimport javax.inject.Inject;%0A%0Aimport net.stickycode.stereotype.Configured;%0Aimport net.stickycode.stereotype.PostConfigured
 ;%0A
-%0A
 impo
@@ -970,32 +970,35 @@
 code.stereotype.
+Pre
 Configured;%0Aimpo
@@ -1094,16 +1094,200 @@
 ework;%0A%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0Aimport com.google.inject.TypeLiteral;%0Aimport com.google.inject.spi.TypeEncounter;%0Aimport com.google.inject.spi.TypeListener;%0A%0A
 @StickyC
@@ -2162,16 +2162,317 @@
 rn true;
+%0A    %0A    for (Method method : type.getRawType().getDeclaredMethods())%0A      if (method.isAnnotationPresent(PostConfigured.class))%0A        return true;%0A    %0A    for (Method method : type.getRawType().getDeclaredMethods())%0A      if (method.isAnnotationPresent(PreConfigured.class))%0A        return true;
 %0A%0A    re

make sure that @PostConfigured and @PreConfigured get invoked even if there are no @Configured fields