public Class<Factory> createProxyClass(Class<?> mockedType, Class<?>... interfaces) {
    if (mockedType == Object.class) {
      mockedType = ClassWithSuperclassToWorkAroundCglibBug.class;
    }

    Enhancer enhancer =
        new Enhancer() {
          @Override
          @SuppressWarnings("unchecked")
          protected void filterConstructors(Class sc, List constructors) {
            // Don't filter
          }
        };
    Class<?>[] allMockedTypes = prepend(mockedType, interfaces);
    enhancer.setClassLoader(SearchingClassLoader.combineLoadersOf(allMockedTypes));
    enhancer.setUseFactory(true);
    if (mockedType.isInterface()) {
      enhancer.setSuperclass(Object.class);
      enhancer.setInterfaces(allMockedTypes);
    } else {
      enhancer.setSuperclass(mockedType);
      enhancer.setInterfaces(interfaces);
    }
    enhancer.setCallbackTypes(new Class[] {MethodInterceptor.class, NoOp.class});
    enhancer.setCallbackFilter(IGNORE_BRIDGE_METHODS);
    if (mockedType.getSigners() != null) {
      enhancer.setNamingPolicy(
          NAMING_POLICY_THAT_ALLOWS_IMPOSTERISATION_OF_CLASSES_IN_SIGNED_PACKAGES);
    } else {
      enhancer.setNamingPolicy(MockitoNamingPolicy.INSTANCE);
    }

    enhancer.setSerialVersionUID(42L);

    try {
      return enhancer.createClass();
    } catch (CodeGenerationException e) {
      if (Modifier.isPrivate(mockedType.getModifiers())) {
        throw new MockitoException(
            "\n"
                + "Mockito cannot mock this class: "
                + mockedType
                + ".\n"
                + "Most likely it is a private class that is not visible by Mockito");
      }
      throw new MockitoException(
          "\n"
              + "Mockito cannot mock this class: "
              + mockedType
              + "\n"
              + "Mockito can only mock visible & non-final classes."
              + "\n"
              + "If you're not sure why you're getting this error, please report to the mailing list.",
          e);
    }
  }
Example #2
0
  private Enhancer createEnhancer(Class<?> toMock) {
    // Create the mock
    Enhancer enhancer =
        new Enhancer() {

          /** Filter all private constructors but do not check that there are some left */
          @SuppressWarnings("rawtypes")
          @Override
          protected void filterConstructors(Class sc, List constructors) {
            CollectionUtils.filter(constructors, new VisibilityPredicate(sc, true));
          }
        };
    enhancer.setSuperclass(toMock);

    // ///CLOVER:OFF (I don't know how to test it automatically yet)
    // See issue ID: 2994002
    if (toMock.getSigners() != null) {
      enhancer.setNamingPolicy(ALLOWS_MOCKING_CLASSES_IN_SIGNED_PACKAGES);
    }
    // ///CLOVER:ON

    return enhancer;
  }
Example #3
0
  /** sends id, version and distribution */
  public static void send() {
    String clientid = readID();
    if (clientid == null) {
      clientid = generateRandomString();
      saveID(clientid);
    }

    final RPAction action = new RPAction();

    // compatibility with old servers
    if (RPClass.getRPClass("cstatus") != null) {
      action.put("type", "cstatus");
    } else {
      action.put("type", "cid");
    }

    // a client id to help with the investigation of hacked accounts
    // especially in the common "angry sibling" case.
    if (clientid != null) {
      action.put("cid", clientid);
    }

    // the client version, the server will deactivate certain features that
    // are incompatible with old clients. E. g. changing of light and dark
    // in the current zone is implemented by retransmitting the tileset
    // information. an old client would mistake that for a zone change and
    // hang.
    String version = Debug.VERSION;
    if (Debug.PRE_RELEASE_VERSION != null) {
      version = version + " - " + Debug.PRE_RELEASE_VERSION;
    }
    action.put("version", version);

    // extract the signer of the client, so that we can ask bug
    // reporters to try again with the official client, if they
    // are using an unofficial one.
    try {
      Class<?> clazz = Class.forName("games.stendhal.client.update.Starter");
      if (clazz != null) {
        Object[] objects = clazz.getSigners();
        if (objects instanceof Certificate[]) {
          Certificate[] certs = (Certificate[]) objects;
          if ((certs.length > 0)) {
            byte[] key = certs[0].getPublicKey().getEncoded();
            action.put("dist", Hash.toHexString(Hash.hash(key)));
          }
        }
      }

      // Throwable: both errors and exceptions
    } catch (Throwable e) {
      logger.error(e, e);
    }

    // Get build number. The class is not in CVS to prevent lots of unwanted
    // conflicts. This has, however, the side effect, that it is missing in
    // an IDE (e. g. Eclipes) environment.
    // The build number is especially helpful for pre releases
    try {
      Class<?> clazz = Class.forName("games.stendhal.client.StendhalBuild");
      Object buildNumber = clazz.getMethod("getBuildNumber").invoke(null);
      if (buildNumber != null) {
        action.put("build", buildNumber.toString());
      }
    } catch (Throwable e) {
      logger.debug(e, e);
    }

    ClientSingletonRepository.getClientFramework().send(action);
  }