@Test
  public void testFieldId() throws SecurityException, NoSuchFieldException {
    TestAnnotatedTypeBuilder<Chair> builder = new TestAnnotatedTypeBuilder<Chair>(Chair.class);
    builder.addToField(Chair.class.getField("legs"), new ProducesLiteral());
    AnnotatedType<Chair> chair3 = builder.create();
    AnnotatedField<? super Chair> field = chair3.getFields().iterator().next();
    String id = AnnotatedTypes.createFieldId(field);
    Assert.assertEquals(
        "org.jboss.weld.tests.unit.util.Chair.legs[@javax.enterprise.inject.Produces()]",
        id,
        "wrong id for field :" + id);

    builder = new TestAnnotatedTypeBuilder<Chair>(Chair.class);
    chair3 = builder.create();
    field = chair3.getFields().iterator().next();
    id = AnnotatedTypes.createFieldId(field);
    Assert.assertEquals(
        "org.jboss.weld.tests.unit.util.Chair.legs", id, "wrong id for field :" + id);

    builder = new TestAnnotatedTypeBuilder<Chair>(Chair.class);
    builder.addToField(Chair.class.getField("legs"), new ComfyChairLiteral());
    chair3 = builder.create();
    field = chair3.getFields().iterator().next();
    id = AnnotatedTypes.createFieldId(field);
    Assert.assertEquals(
        "org.jboss.weld.tests.unit.util.Chair.legs[@org.jboss.weld.tests.unit.util.ComfyChair(softness=1)]",
        id,
        "wrong id for field :" + id);
  }
Example #2
0
  /** Compares two annotated types and returns true if they are the same */
  public static boolean compareAnnotatedTypes(AnnotatedType<?> t1, AnnotatedType<?> t2) {
    if (!t1.getJavaClass().equals(t2.getJavaClass())) {
      return false;
    }
    if (!compareAnnotated(t1, t2)) {
      return false;
    }

    if (t1.getFields().size() != t2.getFields().size()) {
      return false;
    }
    Map<Field, AnnotatedField<?>> fields = new HashMap<Field, AnnotatedField<?>>();
    for (AnnotatedField<?> f : t2.getFields()) {
      fields.put(f.getJavaMember(), f);
    }
    for (AnnotatedField<?> f : t1.getFields()) {
      if (fields.containsKey(f.getJavaMember())) {
        if (!compareAnnotatedField(f, fields.get(f.getJavaMember()))) {
          return false;
        }
      } else {
        return false;
      }
    }

    if (t1.getMethods().size() != t2.getMethods().size()) {
      return false;
    }
    Map<Method, AnnotatedMethod<?>> methods = new HashMap<Method, AnnotatedMethod<?>>();
    for (AnnotatedMethod<?> f : t2.getMethods()) {
      methods.put(f.getJavaMember(), f);
    }
    for (AnnotatedMethod<?> f : t1.getMethods()) {
      if (methods.containsKey(f.getJavaMember())) {
        if (!compareAnnotatedCallable(f, methods.get(f.getJavaMember()))) {
          return false;
        }
      } else {
        return false;
      }
    }
    if (t1.getConstructors().size() != t2.getConstructors().size()) {
      return false;
    }
    Map<Constructor<?>, AnnotatedConstructor<?>> constructors =
        new HashMap<Constructor<?>, AnnotatedConstructor<?>>();
    for (AnnotatedConstructor<?> f : t2.getConstructors()) {
      constructors.put(f.getJavaMember(), f);
    }
    for (AnnotatedConstructor<?> f : t1.getConstructors()) {
      if (constructors.containsKey(f.getJavaMember())) {
        if (!compareAnnotatedCallable(f, constructors.get(f.getJavaMember()))) {
          return false;
        }
      } else {
        return false;
      }
    }
    return true;
  }
  private void introspectInjectField(
      AnnotatedType<X> type, ArrayList<ConfigProgram> injectProgramList) {
    for (AnnotatedField<?> field : type.getFields()) {
      if (field.getAnnotations().size() == 0) continue;

      if (field.isAnnotationPresent(Inject.class)) {
        // boolean isOptional = isQualifierOptional(field);

        InjectionPoint ij = new InjectionPointImpl(getBeanManager(), this, field);

        _injectionPointSet.add(ij);

        if (field.isAnnotationPresent(Delegate.class)) {
          // ioc/0i60
          /*
          if (! type.isAnnotationPresent(javax.decorator.Decorator.class)) {
            throw new IllegalStateException(L.l("'{0}' may not inject with @Delegate because it is not a @Decorator",
                                                type.getJavaClass()));
          }
             */
        } else {
          injectProgramList.add(new FieldInjectProgram(field.getJavaMember(), ij));
        }
      } else {
        InjectionPointHandler handler = getBeanManager().getInjectionPointHandler(field);

        if (handler != null) {
          ConfigProgram program = new FieldHandlerProgram(field, handler);

          injectProgramList.add(program);
        }
      }
    }
  }
Example #4
0
 /**
  * Generates a unique signature for an annotated type. Members without annotations are omitted to
  * reduce the length of the signature
  *
  * @param <X>
  * @param annotatedType
  * @return
  */
 public static <X> String createTypeId(AnnotatedType<X> annotatedType) {
   return createTypeId(
       annotatedType.getJavaClass(),
       annotatedType.getAnnotations(),
       annotatedType.getMethods(),
       annotatedType.getFields(),
       annotatedType.getConstructors());
 }
Example #5
0
 /**
  * Search the annotatedType for the field, returning the {@link AnnotatedField}
  *
  * @param annotatedType The annotatedType to search
  * @param field the field to search for
  * @return The {@link AnnotatedField} found, or null if no field is found
  */
 public static <X> AnnotatedField<? super X> getField(
     AnnotatedType<X> annotatedType, Field field) {
   for (AnnotatedField<? super X> annotatedField : annotatedType.getFields()) {
     if (annotatedField.getDeclaringType().getJavaClass().equals(field.getDeclaringClass())
         && annotatedField.getJavaMember().getName().equals(field.getName())) {
       return annotatedField;
     }
   }
   return null;
 }
 @Test
 public void testWatchAlternatives_stereotypedField_enable() throws Exception {
   // prepare, global alternative
   when(at.isAnnotationPresent(Priority.class)).thenReturn(true);
   final Set<Annotated> annotated = asAnnotatedSet(StereotypedField.class.getDeclaredFields());
   when(at.getFields()).thenReturn(annotated);
   when(at.getJavaClass()).thenReturn(StereotypedField.class);
   // act
   subject.watchAlternatives(pat);
   // assert, not vetoed
   verify(pat, times(0)).veto();
 }
Example #7
0
 @SuppressWarnings("unchecked")
 private <X, A extends AnnotatedMember<? super X>> A getAnnotatedMember(
     Class<X> javaClass, String memberName) {
   AnnotatedType<X> type = getCurrentManager().createAnnotatedType(javaClass);
   for (AnnotatedField<? super X> field : type.getFields()) {
     if (field.getJavaMember().getName().equals(memberName)) {
       return (A) field;
     }
   }
   for (AnnotatedMethod<? super X> method : type.getMethods()) {
     if (method.getJavaMember().getName().equals(memberName)) {
       return (A) method;
     }
   }
   throw new IllegalArgumentException("Member " + memberName + " not found on " + javaClass);
 }
 private UnbackedAnnotatedType(
     AnnotatedType<X> source, AnnotatedTypeIdentifier identifier, SharedObjectCache cache) {
   super(source.getBaseType(), source.getTypeClosure(), source.getAnnotations());
   this.javaClass = source.getJavaClass();
   ImmutableSet.Builder<AnnotatedConstructor<X>> constructors = ImmutableSet.builder();
   for (AnnotatedConstructor<X> constructor : source.getConstructors()) {
     constructors.add(UnbackedAnnotatedConstructor.of(constructor, this, cache));
   }
   this.constructors = constructors.build();
   ImmutableSet.Builder<AnnotatedMethod<? super X>> methods = ImmutableSet.builder();
   for (AnnotatedMethod<? super X> originalMethod : source.getMethods()) {
     methods.add(UnbackedAnnotatedMethod.of(originalMethod, this, cache));
   }
   this.methods = methods.build();
   ImmutableSet.Builder<AnnotatedField<? super X>> fields = ImmutableSet.builder();
   for (AnnotatedField<? super X> originalField : source.getFields()) {
     fields.add(UnbackedAnnotatedField.of(originalField, this, cache));
   }
   this.fields = fields.build();
   this.identifier = identifier;
 }