private SessionType determineSessionType(
     final String ejbClass, final CompositeIndex compositeIndex) {
   if (ejbClass == null) {
     return null;
   }
   final ClassInfo info = compositeIndex.getClassByName(DotName.createSimple(ejbClass));
   if (info == null) {
     return null;
   }
   if (info.annotations().get(STATEFUL_ANNOTATION) != null) {
     return SessionType.Stateful;
   } else if (info.annotations().get(STATELESS_ANNOTATION) != null) {
     return SessionType.Stateless;
   } else if (info.annotations().get(SINGLETON_ANNOTATION) != null) {
     return SessionType.Singleton;
   }
   return null;
 }
 public Map<DotName, List<AnnotationInstance>> getIndexedAnnotations(DotName name) {
   Map<DotName, List<AnnotationInstance>> map = indexedClassInfoAnnotationsMap.get(name);
   if (map == null) {
     ClassInfo ci = index.getClassByName(name);
     if (ci == null || ci.annotations() == null) {
       map = Collections.emptyMap();
     } else {
       map = new HashMap<DotName, List<AnnotationInstance>>(ci.annotations());
       // here we ignore global annotations
       for (DotName globalAnnotationName : DefaultConfigurationHelper.GLOBAL_ANNOTATIONS) {
         if (map.containsKey(globalAnnotationName)) {
           map.put(globalAnnotationName, Collections.<AnnotationInstance>emptyList());
         }
       }
     }
     indexedClassInfoAnnotationsMap.put(name, map);
   }
   return map;
 }
Beispiel #3
0
  private static AnnotationInstance getAnnotation(
      Index index, String moduleName, DotName annotationName) {
    final ClassInfo moduleClass = getModuleInfo(index, moduleName);
    if (moduleClass == null) return null;

    List<AnnotationInstance> annotations = moduleClass.annotations().get(annotationName);
    if (annotations == null || annotations.isEmpty()) return null;

    return annotations.get(0);
  }
Beispiel #4
0
 @Test
 public void testEntityMetadataComplete() {
   Index index = getMockedIndex("entity-metadata-complete.xml");
   DotName authorName = DotName.createSimple(Author.class.getName());
   ClassInfo authorClassInfo = index.getClassByName(authorName);
   assertHasAnnotation(index, authorName, JPADotNames.ENTITY);
   assertHasAnnotation(index, authorName, JPADotNames.ID_CLASS);
   assertEquals(2, authorClassInfo.annotations().size());
   DotName bookName = DotName.createSimple(Book.class.getName());
   assertHasAnnotation(index, bookName, JPADotNames.ENTITY);
 }
Beispiel #5
0
 private AnnotationInstance getClassAnnotation(ClassInfo cls, DotName annoName) {
   List<AnnotationInstance> annos = cls.annotations().get(annoName);
   if (annos != null) {
     // Just return the first one we can find on the class itself
     for (AnnotationInstance anno : annos) {
       if (anno.target() == cls) {
         return anno;
       }
     }
   }
   return null;
 }
 private boolean isAnnotationDeclared(ClassInfo classInfo, DotName requiredAnnotationName) {
   Map<DotName, List<AnnotationInstance>> annotationsMap = classInfo.annotations();
   List<AnnotationInstance> annotations = annotationsMap.get(requiredAnnotationName);
   if (annotations != null) {
     for (AnnotationInstance annotationInstance : annotations) {
       if (annotationInstance.target().equals(classInfo)) {
         return true;
       }
     }
   }
   return false;
 }
Beispiel #7
0
 public static boolean isJaxwsEndpoint(final ClassInfo clazz, final CompositeIndex index) {
   // assert JAXWS endpoint class flags
   final short flags = clazz.flags();
   if (Modifier.isInterface(flags)) return false;
   if (Modifier.isAbstract(flags)) return false;
   if (!Modifier.isPublic(flags)) return false;
   if (isJaxwsService(clazz, index)) return false;
   final boolean hasWebServiceAnnotation = clazz.annotations().containsKey(WEB_SERVICE_ANNOTATION);
   final boolean hasWebServiceProviderAnnotation =
       clazz.annotations().containsKey(WEB_SERVICE_PROVIDER_ANNOTATION);
   if (!hasWebServiceAnnotation && !hasWebServiceProviderAnnotation) {
     return false;
   }
   if (hasWebServiceAnnotation && hasWebServiceProviderAnnotation) {
     ROOT_LOGGER.mutuallyExclusiveAnnotations(clazz.name().toString());
     return false;
   }
   if (Modifier.isFinal(flags)) {
     ROOT_LOGGER.finalEndpointClassDetected(clazz.name().toString());
     return false;
   }
   return true;
 }
  private boolean containsAnnotation(
      ClassInfo classInfo,
      DotName requiredAnnotationName,
      Class<? extends Annotation> requiredAnnotation) {
    // Type and members
    if (classInfo.annotations().containsKey(requiredAnnotationName)) {
      return true;
    }
    // Meta-annotations
    for (DotName annotation : classInfo.annotations().keySet()) {
      try {
        if (annotationClassAnnotationsCache
            .get(annotation)
            .contains(requiredAnnotationName.toString())) {
          return true;
        }
      } catch (ExecutionException e) {
        throw new RuntimeException(e);
      }
    }
    // Superclass
    final DotName superName = classInfo.superName();

    if (superName != null && !OBJECT_NAME.equals(superName)) {
      final ClassInfo superClassInfo = index.getClassByName(superName);
      if (superClassInfo == null) {
        // we are accessing a class that is outside of the jandex index
        // fallback to using reflection
        return SEReflections.containsAnnotation(
            loadClass(superName.toString()), requiredAnnotation);
      }
      if (containsAnnotation(superClassInfo, requiredAnnotationName, requiredAnnotation)) {
        return true;
      }
    }
    return false;
  }
 /**
  * Build new {@link Index} with mocked annotations from orm.xml. This method should be only called
  * once per {@org.hibernate.metamodel.source.annotations.xml.mocker.IndexBuilder IndexBuilder}
  * instance.
  *
  * @param globalDefaults Global defaults from <persistence-unit-metadata>, or null.
  * @return Index.
  */
 Index build(EntityMappingsMocker.Default globalDefaults) {
   // merge annotations that not overrided by xml into the new Index
   for (ClassInfo ci : index.getKnownClasses()) {
     DotName name = ci.name();
     if (indexedClassInfoAnnotationsMap.containsKey(name)) {
       // this class has been overrided by orm.xml
       continue;
     }
     if (ci.annotations() != null && !ci.annotations().isEmpty()) {
       Map<DotName, List<AnnotationInstance>> tmp =
           new HashMap<DotName, List<AnnotationInstance>>(ci.annotations());
       DefaultConfigurationHelper.INSTANCE.applyDefaults(tmp, globalDefaults);
       mergeAnnotationMap(tmp, annotations);
       classes.put(name, ci);
       if (ci.superName() != null) {
         addSubClasses(ci.superName(), ci);
       }
       if (ci.interfaces() != null && ci.interfaces().length > 0) {
         addImplementors(ci.interfaces(), ci);
       }
     }
   }
   return Index.create(annotations, subclasses, implementors, classes);
 }
 private boolean hasInjectConstructor() {
   List<AnnotationInstance> annotationInstances = classInfo.annotations().get(DOT_NAME_INJECT);
   if (annotationInstances != null) {
     for (AnnotationInstance instance : annotationInstances) {
       AnnotationTarget target = instance.target();
       if (target instanceof MethodInfo) {
         MethodInfo methodInfo = (MethodInfo) target;
         if (methodInfo.name().equals(CONSTRUCTOR_METHOD_NAME)) {
           return true;
         }
       }
     }
   }
   return false;
 }
Beispiel #11
0
 @Test
 public void testPersistenceUnitMetadataMetadataComplete() {
   JaxbEntity author = new JaxbEntity();
   author.setClazz(Author.class.getName());
   IndexBuilder indexBuilder = getIndexBuilder();
   EntityMappingsMocker.Default defaults = new EntityMappingsMocker.Default();
   defaults.setMetadataComplete(true);
   EntityMocker entityMocker = new EntityMocker(indexBuilder, author, defaults);
   entityMocker.preProcess();
   entityMocker.process();
   Index index = indexBuilder.build(new EntityMappingsMocker.Default());
   DotName className = DotName.createSimple(Author.class.getName());
   ClassInfo classInfo = index.getClassByName(className);
   assertEquals(1, classInfo.annotations().size());
   assertHasAnnotation(index, className, JPADotNames.ENTITY);
 }
Beispiel #12
0
  private static boolean isWebserviceEndpoint(
      final ServletMetaData servletMD, final List<Index> annotationIndexes) {
    final String endpointClassName = ASHelper.getEndpointName(servletMD);
    if (isJSP(endpointClassName)) return false;

    final DotName endpointDN = DotName.createSimple(endpointClassName);
    ClassInfo endpointClassInfo = null;
    for (final Index index : annotationIndexes) {
      endpointClassInfo = index.getClassByName(endpointDN);
      if (endpointClassInfo != null) {
        if (endpointClassInfo.annotations().containsKey(WEB_SERVICE_ANNOTATION)) return true;
        if (endpointClassInfo.annotations().containsKey(WEB_SERVICE_PROVIDER_ANNOTATION))
          return true;
      }
    }

    return false;
  }
  private List<BindingDescription> getConfigurations(
      final DeploymentUnit deploymentUnit,
      final ClassInfo classInfo,
      final AbstractComponentDescription componentDescription,
      final DeploymentPhaseContext phaseContext)
      throws DeploymentUnitProcessingException {

    final List<BindingDescription> configurations = new ArrayList<BindingDescription>();
    boolean isJPADeploymentMarker = false;
    final Map<DotName, List<AnnotationInstance>> classAnnotations = classInfo.annotations();
    if (classAnnotations != null) {
      List<AnnotationInstance> resourceAnnotations =
          classAnnotations.get(PERSISTENCE_UNIT_ANNOTATION_NAME);
      if (resourceAnnotations != null && resourceAnnotations.size() > 0) {
        isJPADeploymentMarker = true;
        for (AnnotationInstance annotation : resourceAnnotations) {
          configurations.add(
              getConfiguration(deploymentUnit, annotation, componentDescription, phaseContext));
        }
      }
      resourceAnnotations = classAnnotations.get(PERSISTENCE_CONTEXT_ANNOTATION_NAME);
      if (resourceAnnotations != null && resourceAnnotations.size() > 0) {
        isJPADeploymentMarker = true;
        for (AnnotationInstance annotation : resourceAnnotations) {
          configurations.add(
              getConfiguration(deploymentUnit, annotation, componentDescription, phaseContext));
        }
      }
    }

    if (isJPADeploymentMarker) {
      JPADeploymentMarker.mark(deploymentUnit);
    }

    return configurations;
  }
  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;
  }