Example #1
0
  /**
   * use annotation based class parsing to detect the configurable properties of a <code>
   * Configurable</code>-class
   *
   * @param configurable of type Class
   */
  private static Map<Field, Annotation> parseClass(Class<? extends Configurable> configurable) {
    Field[] classFields = configurable.getFields();

    Map<Field, Annotation> s4props = new HashMap<Field, Annotation>();
    for (Field field : classFields) {
      Annotation[] annotations = field.getAnnotations();

      for (Annotation annotation : annotations) {
        Annotation[] superAnnotations = annotation.annotationType().getAnnotations();

        for (Annotation superAnnotation : superAnnotations) {
          if (superAnnotation instanceof S4Property) {
            int fieldModifiers = field.getModifiers();
            assert Modifier.isStatic(fieldModifiers) : "property fields are assumed to be static";
            assert Modifier.isPublic(fieldModifiers) : "property fields are assumed to be public";
            assert Modifier.isFinal(fieldModifiers) : "property fields are assumed to be final";
            assert field.getType().equals(String.class)
                : "properties fields are assumed to be instances of java.lang.String";

            s4props.put(field, annotation);
          }
        }
      }
    }

    return s4props;
  }
Example #2
0
 /**
  * Get an array of field names from an Object.
  *
  * @return An array of field names, or null if there are no names.
  */
 public static String[] getNames(Object object) {
   if (object == null) {
     return null;
   }
   final Class klass = object.getClass();
   final Field[] fields = klass.getFields();
   final int length = fields.length;
   if (length == 0) {
     return null;
   }
   final String[] names = new String[length];
   for (int i = 0; i < length; i += 1) {
     names[i] = fields[i].getName();
   }
   return names;
 }
Example #3
0
  private MutableTreeNode populateAttributes(CavityDBObject obj) {
    DefaultMutableTreeNode tree = new DefaultMutableTreeNode("attrs");
    Class cls = obj.getClass();
    for (Field f : cls.getFields()) {
      int mod = f.getModifiers();
      if (Modifier.isPublic(mod) && !Modifier.isStatic(mod)) {
        String fieldName = f.getName();
        try {
          Object value = f.get(obj);
          tree.add(
              new DefaultMutableTreeNode(String.format("%s=%s", fieldName, String.valueOf(value))));

        } catch (IllegalAccessException e) {
          // do nothing.
        }
      }
    }
    return tree;
  }
Example #4
0
  private void inspect(Class clazz) throws IntrospectionException {
    // Look for public fields.
    Field[] pubFields = clazz.getFields();

    // Filter out
    // 1. transient fields
    // 2. static fields
    // 3. final fields
    for (int i = 0; i < pubFields.length; i++) {
      int modifiers = pubFields[i].getModifiers();
      if ((modifiers & Modifier.TRANSIENT) != 0) {
        continue;
      }
      if ((modifiers & Modifier.STATIC) != 0) {
        continue;
      }
      if ((modifiers & Modifier.FINAL) != 0) {
        continue;
      }
      this.fields.put(pubFields[i].getName(), pubFields[i]);
      this.getterNames.add(pubFields[i].getName());
      this.setterNames.add(pubFields[i].getName());
    }

    // Introspect for getter and setter methods.
    BeanInfo info = Introspector.getBeanInfo(clazz, Object.class);

    PropertyDescriptor[] props = info.getPropertyDescriptors();
    for (int i = 0; i < props.length; i++) {
      if (props[i].getReadMethod() != null) {
        this.getters.put(props[i].getName(), props[i].getReadMethod());
        this.getterNames.add(props[i].getName());
      }
      if (props[i].getWriteMethod() != null) {
        this.setters.put(props[i].getName(), props[i].getWriteMethod());
        this.setterNames.add(props[i].getName());
      }
    }
  }
  public String getDestinations(String messageBrokerId) {
    StringBuilder result = new StringBuilder();
    MessageBroker broker = MessageBroker.getMessageBroker(messageBrokerId);
    result.append("<remotingDestinations>");
    if (broker != null) {
      Service remotingService = broker.getServiceByType("flex.messaging.services.RemotingService");
      if (remotingService != null) {
        Map destinations = remotingService.getDestinations();
        Iterator destinationsIterator = destinations.keySet().iterator();
        result.append("<destinations>");
        if (destinationsIterator != null) {
          while (destinationsIterator.hasNext()) {
            RemotingDestination destination =
                (RemotingDestination) destinations.get(destinationsIterator.next());
            if (destination != null) {
              result.append("<destination>");
              result.append(
                  (new StringBuilder("<destinationId>"))
                      .append(destination.getId())
                      .append("</destinationId>")
                      .toString());
              result.append(
                  (new StringBuilder("<adapterName>"))
                      .append(destination.getAdapter().getClass().getName())
                      .append("</adapterName>")
                      .toString());
              result.append(
                  (new StringBuilder("<source>"))
                      .append(destination.getSource())
                      .append("</source>")
                      .toString());
              List channelIds = destination.getChannels();
              Iterator channelIdsIterator = channelIds.iterator();
              result.append("<channels>");
              for (;
                  channelIdsIterator.hasNext();
                  result.append(
                      (new StringBuilder("<channel>"))
                          .append((String) channelIdsIterator.next())
                          .append("</channel>")
                          .toString())) ;
              result.append("</channels>");
              SecurityConstraint secConstraint = destination.getSecurityConstraint();
              result.append("<securityConstraint>");
              if (secConstraint != null) {
                result.append(
                    (new StringBuilder("<securityMethod>"))
                        .append(secConstraint.getMethod())
                        .append("</securityMethod>")
                        .toString());
                result.append(
                    (new StringBuilder("<securityRoles>"))
                        .append(secConstraint.getRoles())
                        .append("</securityRoles>")
                        .toString());
              }
              result.append("</securityConstraint>");
              String className = destination.getSource();
              if (className != null)
                try {
                  Class c = Class.forName("com.adams.dt.util.Abstract");
                  Field fields[] = c.getFields();
                  result.append("<fields>");
                  if (fields != null) {
                    for (int i = 0; i < fields.length; i++)
                      result.append(
                          (new StringBuilder("<field>"))
                              .append(fields[i].toString())
                              .append("</field>")
                              .toString());
                  }
                  result.append("</fields>");

                  Method methods[] = c.getMethods();
                  result.append("<methods>");
                  if (methods != null) {
                    for (int i = 0; i < methods.length; i++)
                      if (methods[i] != null && !methodsExclude.contains(methods[i].getName())) {
                        result.append("<method>");
                        result.append(
                            (new StringBuilder("<methodSignature>"))
                                .append(methods[i].toString())
                                .append("</methodSignature>")
                                .toString());
                        result.append(
                            (new StringBuilder("<methodName>"))
                                .append(methods[i].getName())
                                .append("</methodName>")
                                .toString());
                        result.append(
                            (new StringBuilder("<returnType>"))
                                .append(methods[i].getReturnType().getName())
                                .append("</returnType>")
                                .toString());
                        Class paramClasses[] = methods[i].getParameterTypes();
                        result.append("<params>");
                        if (paramClasses != null) {
                          for (int j = 0; j < paramClasses.length; j++)
                            if (paramClasses[j] != null)
                              result.append(
                                  (new StringBuilder("<param>"))
                                      .append(paramClasses[j].getName())
                                      .append("</param>")
                                      .toString());
                        }
                        result.append("</params>");
                        result.append("</method>");
                      }
                  }
                  result.append("</methods>");
                } catch (ClassNotFoundException e) {
                  System.out.println(e.getMessage());
                }
              result.append("</destination>");
            }
          }
          result.append("</destinations>");
        }
      }
    }
    result.append("</remotingDestinations>");
    return result.toString();
  }
  public String getPublicPropertiesForClasses(String classNames[]) {
    StringBuilder result = new StringBuilder();
    String className = null;
    ArrayList publicFields = new ArrayList();
    result.append("<classDefinitions>");
    if (classNames != null && classNames.length > 0) {
      for (int i = 0; i < classNames.length; i++) {
        className = classNames[i];
        if (className != null) {
          result.append("<classDefinition>");
          try {
            Class c = Class.forName(className);
            Field fields[] = c.getFields();
            Field field = null;
            if (fields != null) {
              for (int k = 0; k < fields.length; k++) {
                field = fields[k];
                if (field != null)
                  publicFields.add(
                      (new StringBuilder(String.valueOf(field.getName())))
                          .append(",")
                          .append(field.getType().getName())
                          .toString());
              }
            }
            try {
              BeanInfo b = Introspector.getBeanInfo(c);
              result.append(
                  (new StringBuilder("<classSimpleName>"))
                      .append(c.getSimpleName())
                      .append("</classSimpleName>")
                      .toString());
              result.append(
                  (new StringBuilder("<classFullName>"))
                      .append(c.getName())
                      .append("</classFullName>")
                      .toString());
              Package pack = c.getPackage();
              String packStr = "";
              if (pack != null) packStr = pack.getName();
              result.append(
                  (new StringBuilder("<packageName>"))
                      .append(packStr)
                      .append("</packageName>")
                      .toString());
              PropertyDescriptor pds[] = b.getPropertyDescriptors();
              if (pds != null) {
                for (int propCount = 0; propCount < pds.length; propCount++) {
                  PropertyDescriptor pd = pds[propCount];
                  String propertyName = pd.getName();
                  Method readMethod = pd.getReadMethod();
                  Method writeMethod = pd.getWriteMethod();
                  if (readMethod != null
                      && isPublicAccessor(readMethod.getModifiers())
                      && writeMethod != null
                      && isPublicAccessor(writeMethod.getModifiers()))
                    publicFields.add(
                        (new StringBuilder(String.valueOf(propertyName)))
                            .append(",")
                            .append(pd.getPropertyType().getName())
                            .toString());
                }
              }
            } catch (Exception e) {
              e.printStackTrace();
            }
            if (publicFields != null && publicFields.size() > 0) {
              String temp = null;
              result.append("<publicFields>");
              for (int counter = 0; counter < publicFields.size(); counter++) {
                temp = (String) publicFields.get(counter);
                if (temp != null) {
                  String pubTemp[] = temp.split(",");
                  if (pubTemp.length == 2) {
                    result.append("<publicField>");
                    result.append(
                        (new StringBuilder("<publicFieldName>"))
                            .append(pubTemp[0])
                            .append("</publicFieldName>")
                            .toString());
                    result.append(
                        (new StringBuilder("<publicFieldType>"))
                            .append(pubTemp[1])
                            .append("</publicFieldType>")
                            .toString());
                    result.append("</publicField>");
                  }
                }
              }

              result.append("</publicFields>");
            }
          } catch (ClassNotFoundException e) {
            result.append(
                (new StringBuilder("<classFullName>"))
                    .append(className)
                    .append("</classFullName>")
                    .toString());
            result.append(
                (new StringBuilder("<error>Problem retrieving "))
                    .append(className)
                    .append(" information</error>")
                    .toString());
            System.out.println(e.getMessage());
          }
          result.append("</classDefinition>");
        }
      }
    }
    result.append("</classDefinitions>");
    return result.toString();
  }
  // Helper method for loadDocuments()
  private Map<String, SpringResource> analyzeController(
      Class<?> clazz, Map<String, SpringResource> resourceMap, String description)
      throws ClassNotFoundException {
    String controllerCanonicalName = clazz.getCanonicalName();
    String[] controllerRequestMappingValues = null;

    // Determine if we will use class-level requestmapping or dummy string
    if (clazz.getAnnotation(RequestMapping.class) != null
        && clazz.getAnnotation(RequestMapping.class).value() != null) {
      controllerRequestMappingValues = clazz.getAnnotation(RequestMapping.class).value();
    } else {
      controllerRequestMappingValues = new String[1];
      controllerRequestMappingValues[0] = "";
    }

    // Iterate over all value attributes of the class-level RequestMapping annotation
    for (int i = 0; i < controllerRequestMappingValues.length; i++) {

      // Iterate over all methods inside the controller
      Method[] methods = clazz.getMethods();
      for (Method method : methods) {
        RequestMapping methodRequestMapping = method.getAnnotation(RequestMapping.class);

        // Look for method-level @RequestMapping annotation
        if (methodRequestMapping instanceof RequestMapping) {
          RequestMethod[] requestMappingRequestMethods = methodRequestMapping.method();

          // For each method-level @RequestMapping annotation, iterate over HTTP Verb
          for (RequestMethod requestMappingRequestMethod : requestMappingRequestMethods) {
            String[] methodRequestMappingValues = methodRequestMapping.value();

            // Check for cases where method-level @RequestMapping#value is not set, and use the
            // controllers @RequestMapping
            if (methodRequestMappingValues == null || methodRequestMappingValues.length == 0) {
              // The map key is a concat of the following:
              //   1. The controller package
              //   2. The controller class name
              //   3. The controller-level @RequestMapping#value
              String resourceKey =
                  controllerCanonicalName
                      + controllerRequestMappingValues[i]
                      + requestMappingRequestMethod;
              if ((!(resourceMap.containsKey(resourceKey)))) {
                resourceMap.put(
                    resourceKey,
                    new SpringResource(
                        clazz, controllerRequestMappingValues[i], resourceKey, description));
              }
              resourceMap.get(resourceKey).addMethod(method);
            } else {
              // Here we know that method-level @RequestMapping#value is populated, so
              // iterate over all the @RequestMapping#value attributes, and add them to the resource
              // map.
              for (String methodRequestMappingValue : methodRequestMappingValues) {
                String resourceName = methodRequestMappingValue;
                // The map key is a concat of the following:
                //   1. The controller package
                //   2. The controller class name
                //   3. The controller-level @RequestMapping#value
                //   4. The method-level @RequestMapping#value
                //   5. The method-level @RequestMapping#method
                String resourceKey =
                    controllerCanonicalName
                        + controllerRequestMappingValues[i]
                        + resourceName
                        + requestMappingRequestMethod;
                if (!(resourceName.equals(""))) {
                  if ((!(resourceMap.containsKey(resourceKey)))) {
                    resourceMap.put(
                        resourceKey,
                        new SpringResource(clazz, resourceName, resourceKey, description));
                  }
                  resourceMap.get(resourceKey).addMethod(method);
                }
              }
            }
          }
        }
      }
    }
    clazz.getFields();
    clazz
        .getDeclaredFields(); // <--In case developer declares a field without an associated
                              // getter/setter.
    // this will allow NoClassDefFoundError to be caught before it triggers bamboo failure.

    return resourceMap;
  }