private void processPersistenceAnnotations(
      final DeploymentUnit deploymentUnit,
      final EEModuleDescription eeModuleDescription,
      List<AnnotationInstance> persistenceContexts,
      final EEApplicationClasses applicationClasses)
      throws DeploymentUnitProcessingException {

    for (AnnotationInstance annotation : persistenceContexts) {
      ClassInfo declaringClass = null;
      final AnnotationTarget annotationTarget = annotation.target();
      if (annotationTarget instanceof FieldInfo) {
        FieldInfo fieldInfo = (FieldInfo) annotationTarget;
        declaringClass = fieldInfo.declaringClass();
        EEModuleClassDescription eeModuleClassDescription =
            eeModuleDescription.addOrGetLocalClassDescription(declaringClass.name().toString());
        this.processField(deploymentUnit, annotation, fieldInfo, eeModuleClassDescription);
      } else if (annotationTarget instanceof MethodInfo) {
        MethodInfo methodInfo = (MethodInfo) annotationTarget;
        declaringClass = methodInfo.declaringClass();
        EEModuleClassDescription eeModuleClassDescription =
            eeModuleDescription.addOrGetLocalClassDescription(declaringClass.name().toString());
        this.processMethod(deploymentUnit, annotation, methodInfo, eeModuleClassDescription);
      } else if (annotationTarget instanceof ClassInfo) {
        declaringClass = (ClassInfo) annotationTarget;
        EEModuleClassDescription eeModuleClassDescription =
            eeModuleDescription.addOrGetLocalClassDescription(declaringClass.name().toString());
        this.processClass(deploymentUnit, annotation, eeModuleClassDescription);
      }
    }
  }
  private BindingDescription processField(
      final DeploymentUnit deploymentUnit,
      final AnnotationInstance annotation,
      final FieldInfo fieldInfo,
      final AbstractComponentDescription componentDescription,
      final DeploymentPhaseContext phaseContext)
      throws DeploymentUnitProcessingException {

    final String fieldName = fieldInfo.name();
    final AnnotationValue declaredNameValue = annotation.value("name");
    final String declaredName = declaredNameValue != null ? declaredNameValue.asString() : null;
    final String localContextName;
    if (declaredName == null || declaredName.isEmpty()) {
      localContextName = "java:comp/env/persistence" + "/" + fieldName;
    } else {
      localContextName = declaredName;
    }

    // final AnnotationValue declaredTypeValue = annotation.value("type");
    final DotName declaredType = fieldInfo.type().name();
    final DotName injectionType =
        declaredType == null || declaredType.toString().equals(Object.class.getName())
            ? fieldInfo.type().name()
            : declaredType;

    BindingDescription bindingDescription = new BindingDescription();
    bindingDescription.setDependency(true);
    bindingDescription.setBindingName(localContextName);
    final String injectionTypeName = injectionType.toString();
    bindingDescription.setBindingType(injectionTypeName);

    ServiceName injectorName =
        getInjectorServiceName(
            deploymentUnit,
            annotation,
            componentDescription,
            phaseContext,
            fieldName,
            injectionTypeName);
    bindingDescription.setReferenceSourceDescription(
        new ServiceBindingSourceDescription(injectorName));

    // setup the injection target
    final InjectionTargetDescription targetDescription = new InjectionTargetDescription();
    targetDescription.setName(fieldName);
    targetDescription.setClassName(fieldInfo.declaringClass().name().toString());
    targetDescription.setType(InjectionTargetDescription.Type.FIELD);
    targetDescription.setValueClassName(injectionTypeName);
    bindingDescription.getInjectionTargetDescriptions().add(targetDescription);

    return bindingDescription;
  }
예제 #3
0
  public static AnnotationTarget getTarget(
      ServiceRegistry serviceRegistry, ClassInfo classInfo, String name, TargetType type) {
    Class clazz =
        serviceRegistry.getService(ClassLoaderService.class).classForName(classInfo.toString());
    switch (type) {
      case FIELD:
        Field field = getField(clazz, name);
        if (field == null) {
          throw new HibernateException(
              "Unable to load field " + name + " of class " + clazz.getName());
        }

        return FieldInfo.create(
            classInfo, name, getType(field.getType()), (short) (field.getModifiers()));
      case METHOD:
        Method method = getMethod(clazz, name);
        if (method == null) {
          throw new HibernateException(
              "Unable to load method " + name + " of class " + clazz.getName());
        }
        return getMethodInfo(classInfo, method);
      case PROPERTY:
        method = getterMethod(clazz, name);
        if (method == null) {
          throw new HibernateException(
              "Unable to load property " + name + " of class " + clazz.getName());
        }
        return getMethodInfo(classInfo, method);
    }
    throw new HibernateException("");
  }
 @Override
 public Collection<Annotation> getAnnotation(Class<?> annotationClass) {
   List<AnnotationInstance> instances =
       backingRepository.getAnnotations(DotName.createSimple(annotationClass.getName()));
   ArrayList<Annotation> annotations = new ArrayList<Annotation>(instances.size());
   for (AnnotationInstance instance : instances) {
     AnnotationTarget target = instance.target();
     Annotation annotation = null;
     if (target instanceof MethodInfo) {
       MethodInfo m = (MethodInfo) target;
       List<String> parameterTypes = new ArrayList<String>(m.args().length);
       for (Type type : m.args()) {
         parameterTypes.add(type.toString());
       }
       String declaringClass = m.declaringClass().name().toString();
       annotation =
           new AnnotationImpl(
               declaringClass, cl, parameterTypes, m.name(), true, false, annotationClass);
     }
     if (target instanceof FieldInfo) {
       FieldInfo f = (FieldInfo) target;
       String declaringClass = f.declaringClass().name().toString();
       annotation =
           new AnnotationImpl(declaringClass, cl, null, f.name(), false, true, annotationClass);
     }
     if (target instanceof ClassInfo) {
       ClassInfo c = (ClassInfo) target;
       annotation =
           new AnnotationImpl(c.name().toString(), cl, null, null, false, false, annotationClass);
     }
     if (annotation != null) {
       annotations.add(annotation);
     }
   }
   annotations.trimToSize();
   if (annotations.size() == 0) {
     return null;
   } else {
     return Collections.unmodifiableList(annotations);
   }
 }
 private void processField(
     final DeploymentUnit deploymentUnit,
     final EJBResourceWrapper annotation,
     final FieldInfo fieldInfo,
     final EEModuleDescription eeModuleDescription) {
   final String fieldName = fieldInfo.name();
   final String fieldType = fieldInfo.type().name().toString();
   final InjectionTarget targetDescription =
       new FieldInjectionTarget(
           fieldInfo.declaringClass().name().toString(), fieldName, fieldType);
   final String localContextName =
       isEmpty(annotation.name())
           ? fieldInfo.declaringClass().name().toString() + "/" + fieldInfo.name()
           : annotation.name();
   final String beanInterfaceType =
       isEmpty(annotation.beanInterface())
               || annotation.beanInterface().equals(Object.class.getName())
           ? fieldType
           : annotation.beanInterface();
   process(
       deploymentUnit,
       beanInterfaceType,
       annotation.beanName(),
       annotation.lookup(),
       fieldInfo.declaringClass(),
       targetDescription,
       localContextName,
       eeModuleDescription);
 }
예제 #6
0
파일: MockHelper.java 프로젝트: enenuki/phd
/* 285:    */   
/* 286:    */   static AnnotationTarget getTarget(ServiceRegistry serviceRegistry, ClassInfo classInfo, String name, TargetType type)
/* 287:    */   {
/* 288:322 */     Class clazz = ((ClassLoaderService)serviceRegistry.getService(ClassLoaderService.class)).classForName(classInfo.toString());
/* 289:    */     Method method;
/* 290:323 */     switch (1.$SwitchMap$org$hibernate$metamodel$source$annotations$xml$mocker$MockHelper$TargetType[type.ordinal()])
/* 291:    */     {
/* 292:    */     case 1: 
/* 293:325 */       Field field = getField(clazz, name);
/* 294:326 */       if (field == null) {
/* 295:327 */         throw new HibernateException("Unable to load field " + name + " of class " + clazz.getName());
/* 296:    */       }
/* 297:334 */       return FieldInfo.create(classInfo, name, getType(field.getType()), (short)field.getModifiers());
/* 298:    */     case 2: 
/* 299:338 */       method = getMethod(clazz, name);
/* 300:339 */       if (method == null) {
/* 301:340 */         throw new HibernateException("Unable to load method " + name + " of class " + clazz.getName());
/* 302:    */       }
/* 303:346 */       return getMethodInfo(classInfo, method);
/* 304:    */     case 3: 
  private void processField(
      final DeploymentUnit deploymentUnit,
      final AnnotationInstance annotation,
      final FieldInfo fieldInfo,
      final EEModuleClassDescription eeModuleClassDescription)
      throws DeploymentUnitProcessingException {

    final String fieldName = fieldInfo.name();
    final AnnotationValue declaredNameValue = annotation.value("name");
    final String declaredName = declaredNameValue != null ? declaredNameValue.asString() : null;
    final String localContextName;
    if (declaredName == null || declaredName.isEmpty()) {
      localContextName = fieldInfo.declaringClass().name().toString() + "/" + fieldName;
    } else {
      localContextName = declaredName;
    }

    // final AnnotationValue declaredTypeValue = annotation.value("type");
    final DotName declaredTypeDotName = fieldInfo.type().name();
    final DotName injectionTypeDotName =
        declaredTypeDotName == null || declaredTypeDotName.toString().equals(Object.class.getName())
            ? fieldInfo.type().name()
            : declaredTypeDotName;

    final String injectionType = injectionTypeDotName.toString();
    final InjectionSource bindingSource =
        this.getBindingSource(deploymentUnit, annotation, injectionType, eeModuleClassDescription);
    if (bindingSource != null) {
      final BindingConfiguration bindingConfiguration =
          new BindingConfiguration(localContextName, bindingSource);
      eeModuleClassDescription.getBindingConfigurations().add(bindingConfiguration);

      // setup the injection target
      final InjectionTarget injectionTarget =
          new FieldInjectionTarget(
              fieldInfo.declaringClass().name().toString(),
              fieldName,
              fieldInfo.type().name().toString());
      // source is always local ENC jndi
      final InjectionSource injectionSource = new LookupInjectionSource(localContextName);
      final ResourceInjectionConfiguration injectionConfiguration =
          new ResourceInjectionConfiguration(injectionTarget, injectionSource);
      eeModuleClassDescription.addResourceInjection(injectionConfiguration);
    }
  }
  private JandexPivot pivotAnnotations(DotName typeName) {
    if (jandexIndex == null) {
      return NO_JANDEX_PIVOT;
    }

    final ClassInfo jandexClassInfo = jandexIndex.getClassByName(typeName);
    if (jandexClassInfo == null) {
      return NO_JANDEX_PIVOT;
    }

    final Map<DotName, List<AnnotationInstance>> annotations = jandexClassInfo.annotations();
    final JandexPivot pivot = new JandexPivot();
    for (Map.Entry<DotName, List<AnnotationInstance>> annotationInstances :
        annotations.entrySet()) {
      for (AnnotationInstance annotationInstance : annotationInstances.getValue()) {
        if (MethodParameterInfo.class.isInstance(annotationInstance.target())) {
          continue;
        }

        if (FieldInfo.class.isInstance(annotationInstance.target())) {
          final FieldInfo fieldInfo = (FieldInfo) annotationInstance.target();
          Map<DotName, AnnotationInstance> fieldAnnotations =
              pivot.fieldAnnotations.get(fieldInfo.name());
          if (fieldAnnotations == null) {
            fieldAnnotations = new HashMap<DotName, AnnotationInstance>();
            pivot.fieldAnnotations.put(fieldInfo.name(), fieldAnnotations);
            fieldAnnotations.put(annotationInstance.name(), annotationInstance);
          } else {
            final Object oldEntry =
                fieldAnnotations.put(annotationInstance.name(), annotationInstance);
            if (oldEntry != null) {
              log.debugf(
                  "Encountered duplicate annotation [%s] on field [%s]",
                  annotationInstance.name(), fieldInfo.name());
            }
          }
        } else if (MethodInfo.class.isInstance(annotationInstance.target())) {
          final MethodInfo methodInfo = (MethodInfo) annotationInstance.target();
          final String methodKey = buildBuildKey(methodInfo);
          Map<DotName, AnnotationInstance> methodAnnotations =
              pivot.methodAnnotations.get(methodKey);
          if (methodAnnotations == null) {
            methodAnnotations = new HashMap<DotName, AnnotationInstance>();
            pivot.methodAnnotations.put(methodKey, methodAnnotations);
            methodAnnotations.put(annotationInstance.name(), annotationInstance);
          } else {
            final Object oldEntry =
                methodAnnotations.put(annotationInstance.name(), annotationInstance);
            if (oldEntry != null) {
              log.debugf(
                  "Encountered duplicate annotation [%s] on method [%s -> %s]",
                  annotationInstance.name(), jandexClassInfo.name(), methodKey);
            }
          }
        } else if (ClassInfo.class.isInstance(annotationInstance.target())) {
          // todo : validate its the type we are processing?
          final Object oldEntry =
              pivot.typeAnnotations.put(annotationInstance.name(), annotationInstance);
          if (oldEntry != null) {
            log.debugf(
                "Encountered duplicate annotation [%s] on type [%s]",
                annotationInstance.name(), jandexClassInfo.name());
          }
        }
      }
    }

    return pivot;
  }