Java Class.getCanonicalName Examples

Java Class.getCanonicalName - 30 examples found. These are the top rated real world Java examples of Class.getCanonicalName extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
  /**
   * Convert a method call into a string according to TcpRest protocol that can be transmitted
   * across network.
   *
   * @param clazz Calling class
   * @param method Calling method
   * @param params parameters of calling method
   * @param mappers mapper for each parameter
   * @return
   * @throws MapperNotFoundException
   */
  public String encode(Class clazz, Method method, Object[] params, Map<String, Mapper> mappers)
      throws MapperNotFoundException {
    StringBuilder paramTokenBuffer = new StringBuilder();
    if (params != null) {
      for (Object param : params) {
        if (param == null) param = new NullObj();
        logger.log("***DefaultConverter - encode for class: " + param.getClass());

        Mapper mapper = getMapper(mappers, param.getClass());

        paramTokenBuffer
            .append(encodeParam(mapper.objectToString(param)))
            .append(TcpRestProtocol.PATH_SEPERATOR);

        logger.debug("***DefaultConverter - paramTokenBuffer " + paramTokenBuffer.toString());
      }

      return clazz.getCanonicalName()
          + "/"
          + method.getName()
          + "("
          + paramTokenBuffer.substring(
              0, paramTokenBuffer.length() - TcpRestProtocol.PATH_SEPERATOR.length())
          + ")";
    } else {
      return clazz.getCanonicalName() + "/" + method.getName() + "()";
    }
  }
Example #2
0
  /**
   * Recursively iterate the given package, and attempt to resolve all annotated references for
   * <code>RequestHandler</code> implementations.
   *
   * @param handlerPackage the base package to scan, for example "org.apache.awf".
   * @return a <code>Map&lt;String, RequestHandler&gt;</code> of handlers, which may be empty but
   *     not <code>null</code>.
   */
  public Map<String, RequestHandler> findHandlers(String handlerPackage) {

    if (Strings.isNullOrEmpty(handlerPackage)) {
      logger.warn("No RequestHandler package defined");
      return pathHandlers;
    }

    List<Class<?>> classes = findClasses(handlerPackage);
    for (Class<?> clazz : classes) {
      if (clazz.isAnnotationPresent(Path.class)) {

        RequestHandler handler =
            (RequestHandler) ReflectionTools.createInstance(clazz.getCanonicalName());
        Path path = clazz.getAnnotation(Path.class);
        pathHandlers.put(path.value(), handler);

        logger.info(
            "Added RequestHandler ["
                + clazz.getCanonicalName()
                + "] for Path ["
                + path.value()
                + "]");
      }
    }

    return pathHandlers;
  }
 /**
  * Discover default encodeables. Encodeables are discovered by inspecting all fields from
  * Identifiers class using reflection.
  *
  * @param map encodeable table to fill with builtin encodeables
  */
 @SuppressWarnings("unchecked")
 public static void discoverDefaultEncodeables(Map<NodeId, Class<IEncodeable>> map) {
   // Discover builtin classes
   Class<?> clazz = Identifiers.class;
   ClassLoader cl = clazz.getClassLoader();
   int index = clazz.getCanonicalName().lastIndexOf(".");
   String prefix = clazz.getCanonicalName().substring(0, index);
   for (Field f : clazz.getFields()) {
     f.setAccessible(true);
     try {
       String className = prefix + "." + f.getName();
       Class<IEncodeable> c = (Class<IEncodeable>) cl.loadClass(className);
       if (!IEncodeable.class.isAssignableFrom(c)) continue;
       for (Field cf : c.getFields()) {
         cf.setAccessible(true);
         if (!cf.getType().equals(NodeId.class)) continue;
         NodeId nodeId;
         try {
           nodeId = (NodeId) cf.get(null);
         } catch (IllegalArgumentException e) {
           throw new RuntimeException("Failed to load default identifiers", e);
         } catch (IllegalAccessException e) {
           throw new RuntimeException("Failed to load default identifiers", e);
         }
         if (nodeId == null) throw new RuntimeException("Failed to load default identifiers");
         map.put(nodeId, c);
       }
     } catch (ClassNotFoundException e) {
       continue;
     }
   }
 }
  // Verify that the test method level events for the test methods declared in the specified class
  // run in the same
  // thread for each instance of the test class for the specified suite and test
  public static void verifyEventsForTestMethodsRunInTheSameThread(
      Class<?> testClass, String suiteName, String testName) {

    for (Method method : testClass.getMethods()) {
      if (method.getDeclaringClass().equals(testClass)) {
        Multimap<Object, EventLog> testMethodEventLogs =
            getTestMethodEventLogsForMethod(
                suiteName, testName, testClass.getCanonicalName(), method.getName());

        for (Object instanceKey : testMethodEventLogs.keySet()) {
          long threadId = -1;

          for (EventLog eventLog : testMethodEventLogs.get(instanceKey)) {
            if (threadId == -1) {
              threadId = eventLog.getThreadId();
            } else {
              assertEquals(
                  eventLog.getThreadId(),
                  threadId,
                  "All of the method level events for the test "
                      + "method "
                      + method.getName()
                      + " in the test class "
                      + testClass.getCanonicalName()
                      + " for the test "
                      + suiteName
                      + " should be run in the same thread");
            }
          }
        }
      }
    }
  }
 /**
  * Checks type and its supertypes for {@link ExtraTypes} annotations.
  *
  * @param type the type to examine
  * @param addModelExtraTypes if {@code true} the contents of the {@link #extraTypes} field will be
  *     added to the returned list.
  */
 private List<EntityProxyModel> checkExtraTypes(JClassType type, boolean addModelExtraTypes)
     throws UnableToCompleteException {
   Set<EntityProxyModel> toReturn = new LinkedHashSet<EntityProxyModel>();
   if (addModelExtraTypes && extraTypes != null) {
     toReturn.addAll(extraTypes);
   }
   for (JClassType toExamine : type.getFlattenedSupertypeHierarchy()) {
     ExtraTypes proxyExtraTypes = toExamine.getAnnotation(ExtraTypes.class);
     if (proxyExtraTypes != null) {
       for (Class<? extends BaseProxy> clazz : proxyExtraTypes.value()) {
         JClassType proxy = oracle.findType(clazz.getCanonicalName());
         if (proxy == null) {
           poison(
               "Unknown class %s in @%s",
               clazz.getCanonicalName(), ExtraTypes.class.getSimpleName());
         } else {
           toReturn.add(getEntityProxyType(proxy));
         }
       }
     }
   }
   if (toReturn.isEmpty()) {
     return Collections.emptyList();
   }
   return new ArrayList<EntityProxyModel>(toReturn);
 }
  // Verify that all the test methods declared in the specified class have the same instances of the
  // class associated /
  // with them for the specified suite and test.
  public static void verifySameInstancesOfTestClassAssociatedWithMethods(
      String suiteName, String testName, Class<?> clazz) {

    Set<Object> instanceKeys = null;

    for (String methodName : getDeclaredTestMethods(clazz)) {
      Multimap<Object, EventLog> eventLogMap =
          getTestMethodEventLogsForMethod(
              suiteName, testName, clazz.getCanonicalName(), methodName);

      if (instanceKeys == null) {
        instanceKeys = eventLogMap.keySet();
      } else {
        assertTrue(
            instanceKeys.containsAll(eventLogMap.keySet())
                && eventLogMap.keySet().containsAll(instanceKeys),
            "The same instances of "
                + clazz.getCanonicalName()
                + " should be associated with its methods for the test "
                + testName
                + " in the suite "
                + suiteName);
      }
    }
  }