Пример #1
0
 /**
  * Returns the attribute names to use for the supplied object class. This method is invoked
  * recursively if superior classes are included.
  *
  * @param objectClass to retrieve names from
  * @param processed object classes that have already been processed
  * @return set of all attribute names used for bean generation
  */
 private Set<String> getAttributeNames(
     final ObjectClass objectClass, final Set<ObjectClass> processed) {
   final Set<String> attributeNames = new HashSet<>();
   if (objectClass != null) {
     if (objectClass.getRequiredAttributes() != null) {
       attributeNames.addAll(Arrays.asList(objectClass.getRequiredAttributes()));
     }
     if (useOptionalAttributes && objectClass.getOptionalAttributes() != null) {
       attributeNames.addAll(Arrays.asList(objectClass.getOptionalAttributes()));
     }
     processed.add(objectClass);
     if (includeSuperiorClasses && objectClass.getSuperiorClasses() != null) {
       for (String oc : objectClass.getSuperiorClasses()) {
         final ObjectClass superiorOc = schema.getObjectClass(oc);
         if (!processed.contains(superiorOc)) {
           attributeNames.addAll(getAttributeNames(superiorOc, processed));
         }
       }
     }
   }
   return attributeNames;
 }
Пример #2
0
  /**
   * Generates a class for each configured object class. See {@link #objectClasses}. {@link
   * #write(String)} must be invoked to write the classes to disk.
   */
  public void generate() {
    for (String objectClass : objectClasses) {
      final JDefinedClass definedClass = createClass(packageName, objectClass);
      final JDocComment jDocComment = definedClass.javadoc();
      jDocComment.add(String.format("Ldaptive generated bean for objectClass '%s'", objectClass));

      final ObjectClass oc = schema.getObjectClass(objectClass);
      final Set<String> attributeNames = getAttributeNames(oc);
      if (useOperationalAttributes) {
        for (AttributeType type : schema.getAttributeTypes()) {
          if (AttributeUsage.DIRECTORY_OPERATION.equals(type.getUsage())) {
            attributeNames.add(type.getName());
          }
        }
      }

      final Map<String, AttributeType> mutators = new TreeMap<>();
      for (String name : attributeNames) {
        final AttributeType type = schema.getAttributeType(name);
        if (!isNameExcluded(type)) {
          if (nameMappings.containsKey(type.getName())) {
            mutators.put(nameMappings.get(type.getName()), type);
          } else {
            mutators.put(formatAttributeName(type.getName()), type);
          }
        }
      }

      // add entry annotation
      final JAnnotationUse entryAnnotation =
          definedClass.annotate(codeModel.ref(org.ldaptive.beans.Entry.class));
      entryAnnotation.param("dn", "dn");

      final JAnnotationArrayMember attrArray = entryAnnotation.paramArray("attributes");

      // add mutator for the DN
      createMutators(definedClass, "dn", String.class, false);

      // add mutators for each attribute
      for (Map.Entry<String, AttributeType> mutator : mutators.entrySet()) {
        final Class<?> syntaxType =
            getSyntaxType(
                mutator.getValue(), schema.getSyntax(mutator.getValue().getSyntaxOID(false)));
        if (mutator.getValue().isSingleValued()) {
          createMutators(definedClass, mutator.getKey(), syntaxType, false);
        } else {
          createMutators(definedClass, mutator.getKey(), syntaxType, true);
        }

        // add attribute annotation
        final JAnnotationUse attrAnnotation =
            attrArray.annotate(org.ldaptive.beans.Attribute.class);
        attrAnnotation.param("name", mutator.getValue().getName());
        if (!mutator.getKey().equals(mutator.getValue().getName())) {
          attrAnnotation.param("property", mutator.getKey());
        }
        if (byte[].class.equals(syntaxType)) {
          attrAnnotation.param("binary", true);
        }
      }

      // create additional methods
      createHashCode(definedClass);
      createEquals(definedClass);
      createToString(definedClass);
    }
  }