Java Class.isAnonymousClass Examples

Java Class.isAnonymousClass - 30 examples found. These are the top rated real world Java examples of Class.isAnonymousClass extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
  @SuppressWarnings("unchecked")
  static void annotatedWith(Set<Class<?>> contracts, Object obj, Class annotation) {
    if (null != obj) {
      Class<?> clazz = obj.getClass();

      while (Object.class != clazz) {
        if (!clazz.isAnonymousClass()) {
          Object t = clazz.getAnnotation(annotation);
          if (null != t) {
            contracts.add(clazz);
          } else {
            annotatedWith(contracts, annotation, clazz);
          }

          for (Class<?> iface : clazz.getInterfaces()) {
            t = iface.getAnnotation(annotation);
            if (null != t) {
              contracts.add(iface);
            } else {
              annotatedWith(contracts, annotation, iface);
            }
          }
        }

        clazz = clazz.getSuperclass();
      }
    }
  }
  protected <T> T executeCommand(Command cmd) {
    if (AcceptedClientCommands.isSendObjectParameterCommandClass(cmd.getClass())) {
      List<Object> extraClassInstanceList = new ArrayList<Object>();
      preprocessParameterCommand(cmd, extraClassInstanceList);

      if (!extraClassInstanceList.isEmpty()) {
        Set<Class<?>> extraJaxbClasses = new HashSet<Class<?>>();
        for (Object jaxbObject : extraClassInstanceList) {
          Class<?> jaxbClass = jaxbObject.getClass();
          if (jaxbClass.isLocalClass() || jaxbClass.isAnonymousClass()) {
            throw new SerializationException(
                "Only proper classes are allowed as parameters for the remote API: neither local nor anonymous classes are accepted: "
                    + jaxbClass.getName());
          }
          extraJaxbClasses.add(jaxbClass);
        }
        if (config.addJaxbClasses(extraJaxbClasses)) {
          for (Class<?> extraClass : extraJaxbClasses) {
            logger.debug(
                "Adding {} to the JAXBContext instance in this client instance.",
                extraClass.getName());
          }
          config.initializeJaxbSerializationProvider();
        }
      }
    }

    preprocessCommand(cmd);

    if (config.isRest()) {
      return executeRestCommand(cmd);
    } else {
      return executeJmsCommand(cmd);
    }
  }
Example #3
0
  private static String approximateSimpleName(Class<?> clazz, boolean dropOuterClassNames) {
    checkArgument(!clazz.isAnonymousClass(), "Attempted to get simple name of anonymous class");

    String fullName = clazz.getName();
    String shortName = fullName.substring(fullName.lastIndexOf('.') + 1);

    // Drop common suffixes for each named component.
    String[] names = shortName.split("\\$");
    for (int i = 0; i < names.length; i++) {
      names[i] = simplifyNameComponent(names[i]);
    }
    shortName = Joiner.on('$').join(names);

    if (dropOuterClassNames) {
      // Simplify inner class name by dropping outer class prefixes.
      Matcher m = NAMED_INNER_CLASS.matcher(shortName);
      if (m.matches()) {
        shortName = m.group("INNER");
      }
    } else {
      // Dropping anonymous outer classes
      shortName = shortName.replaceAll(ANONYMOUS_CLASS_REGEX, ".");
      shortName = shortName.replaceAll("\\$", ".");
    }
    return shortName;
  }
 @Test
 public void should_generate_assertion_for_classes_in_package_using_provided_class_loader()
     throws Exception {
   ClassLoader customClassLoader =
       new MyClassLoader(Thread.currentThread().getContextClassLoader());
   Set<Class<?>> classes =
       collectClasses(customClassLoader, "org.assertj.assertions.generator.data");
   for (Class<?> clazz : classes) {
     assertThat(clazz.isAnonymousClass())
         .as("check that " + clazz.getSimpleName() + " is not anonymous")
         .isFalse();
     assertThat(clazz.isLocalClass())
         .as("check that " + clazz.getSimpleName() + " is not local")
         .isFalse();
     assertThat(isPublic(clazz.getModifiers()))
         .as("check that " + clazz.getSimpleName() + " is public")
         .isTrue();
     logger.info("Generating assertions for {}", clazz.getName());
     final ClassDescription classDescription = converter.convertToClassDescription(clazz);
     File customAssertionFile = assertionGenerator.generateCustomAssertionFor(classDescription);
     logger.info(
         "Generated {} assertions file -> {}",
         clazz.getSimpleName(),
         customAssertionFile.getAbsolutePath());
   }
 }
Example #5
0
  /**
   * Replaces an anonymous inner class with a serializable substitution based on its {@code
   * writeReplace} method.
   *
   * @param object the object to substitute, not null
   * @return the substitution object as returned by its {@code writeReplace} method
   * @throws OpenGammaRuntimeException if no suitable method is defined or an error occurs in its
   *     execution
   */
  protected static Object substituteObject(final Object object) {
    final Class<?> clazz = object.getClass();
    if (clazz.isAnonymousClass()) {
      Method method = s_writeReplace.get(clazz);
      if (method == null) {
        method =
            AccessController.doPrivileged(
                new PrivilegedAction<Method>() {

                  @Override
                  public Method run() {
                    try {
                      final Method mtd = clazz.getMethod("writeReplace");
                      mtd.setAccessible(true);
                      return mtd;
                    } catch (NoSuchMethodException e) {
                      // Ignore
                    }
                    return null;
                  }
                });
        if (method == null) {
          throw new OpenGammaRuntimeException(
              "No serialization substitution available for anonymous inner class object " + object);
        }
        s_writeReplace.putIfAbsent(clazz, method);
      }
      try {
        return method.invoke(object);
      } catch (Exception e) {
        throw new OpenGammaRuntimeException("Couldn't call writeReplace on inner class", e);
      }
    }
    return object;
  }
  /**
   * Default constructor associates this handler with the queue for the current thread.
   *
   * <p>If there isn't one, this handler won't be able to receive messages.
   */
  public Handler() {
    if (FIND_POTENTIAL_LEAKS) {
      final Class<? extends Handler> klass = getClass();
      if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass())
          && (klass.getModifiers() & Modifier.STATIC) == 0) {
        Log.w(
            TAG,
            "The following Handler class should be static or leaks might occur: "
                + klass.getCanonicalName());
      }
    }
    // @wujl commented

    mLooper = Looper.myLooper();
    if (mLooper == null) {
      throw new RuntimeException(
          "Can't create handler inside thread that has not called Looper.prepare()");
    }
    mQueue = mLooper.mQueue;

    // #wujl
    // @wujl added
    // mLooper = null;
    // mQueue = null;
    // #wujl
    mCallback = null;
  }