Exemple #1
0
 public void onWrite(ClassPool pool, String className)
     throws NotFoundException, CannotCompileException {
   CtClass cc = pool.get(className);
   try {
     if (isPersistent(className)) {
       CtClass base = cc.getSuperclass();
       CtConstructor cons = new CtConstructor(constructorParams, cc);
       if (base.subclassOf(persistent) || base == object) {
         cons.setBody(null);
         cc.addConstructor(cons);
         if (base == object) {
           cc.setSuperclass(persistent);
         }
       } else {
         if (!isPersistent(base.getName())) {
           throw new NotFoundException(
               "Base class " + base.getName() + " was not declared as persistent");
         }
         cons.setBody("super($0);");
         cc.addConstructor(cons);
       }
       preprocessMethods(cc, true, true);
       if (base == persistent || base == object) {
         CtMethod m = new CtMethod(isRecursive, cc, null);
         m.setBody("return false;");
         cc.addMethod(m);
         addSerializeMethods(cc, false);
       } else if (base.subtypeOf(serializable)) {
         addSerializeMethods(cc, true);
       }
       if ((cc.getModifiers() & Modifier.PRIVATE) == 0) {
         CtClass f = pool.makeClass(className + "LoadFactory");
         f.addInterface(factory);
         CtMethod c = new CtMethod(create, f, null);
         c.setBody("return new " + className + "($1);");
         f.addMethod(c);
         CtNewConstructor.defaultConstructor(f);
       }
     } else {
       preprocessMethods(
           cc, cc.subtypeOf(persistent) && cc != persistent, !className.startsWith("org.nachodb"));
     }
   } catch (Exception x) {
     x.printStackTrace();
   }
 }
Exemple #2
0
  /*
   * Returns YES if actual parameter types matches the given signature.
   *
   * argTypes, argDims, and argClassNames represent actual parameters.
   *
   * This method does not correctly implement the Java method dispatch
   * algorithm.
   *
   * If some of the parameter types exactly match but others are subtypes of
   * the corresponding type in the signature, this method returns the number
   * of parameter types that do not exactly match.
   */
  private int compareSignature(String desc, int[] argTypes, int[] argDims, String[] argClassNames)
      throws CompileError {
    int result = YES;
    int i = 1;
    int nArgs = argTypes.length;
    if (nArgs != Descriptor.numOfParameters(desc)) return NO;

    int len = desc.length();
    for (int n = 0; i < len; ++n) {
      char c = desc.charAt(i++);
      if (c == ')') return (n == nArgs ? result : NO);
      else if (n >= nArgs) return NO;

      int dim = 0;
      while (c == '[') {
        ++dim;
        c = desc.charAt(i++);
      }

      if (argTypes[n] == NULL) {
        if (dim == 0 && c != 'L') return NO;

        if (c == 'L') i = desc.indexOf(';', i) + 1;
      } else if (argDims[n] != dim) {
        if (!(dim == 0 && c == 'L' && desc.startsWith("java/lang/Object;", i))) return NO;

        // if the thread reaches here, c must be 'L'.
        i = desc.indexOf(';', i) + 1;
        result++;
        if (i <= 0) return NO; // invalid descriptor?
      } else if (c == 'L') { // not compare
        int j = desc.indexOf(';', i);
        if (j < 0 || argTypes[n] != CLASS) return NO;

        String cname = desc.substring(i, j);
        if (!cname.equals(argClassNames[n])) {
          CtClass clazz = lookupClassByJvmName(argClassNames[n]);
          try {
            if (clazz.subtypeOf(lookupClassByJvmName(cname))) result++;
            else return NO;
          } catch (NotFoundException e) {
            result++; // should be NO?
          }
        }

        i = j + 1;
      } else {
        int t = descToType(c);
        int at = argTypes[n];
        if (t != at)
          if (t == INT && (at == SHORT || at == BYTE || at == CHAR)) result++;
          else return NO;
      }
    }

    return NO;
  }
 private static boolean isCtClassSerializable(
     JarArchiveComparatorOptions options,
     CtClass clazz,
     JarArchiveComparator jarArchiveComparator) {
   ClassPool pool = clazz.getClassPool();
   try {
     return clazz.subtypeOf(pool.get("java.io.Serializable"));
   } catch (NotFoundException e) {
     if (options.isIgnoreMissingClasses()) {
       return false;
     } else {
       throw JApiCmpException.forClassLoading(e, clazz.getName(), jarArchiveComparator);
     }
   }
 }