コード例 #1
0
ファイル: AssertReplacer.java プロジェクト: maxcleme/nopol
  private void createExecutionMethod(CtInvocation<?> ctInvocation) {
    CtClass<?> parent = ctInvocation.getParent(CtClass.class);
    if (!parent.isTopLevel()) {
      return;
    }
    if (parent.getModifiers().contains(ModifierKind.ABSTRACT)) {
      return;
    }
    CtTypeReference<String[]> typedReference = getFactory().Class().createReference(String[].class);
    if (parent.getMethod("runJPFTest", typedReference) != null) {
      return;
    }
    CtTypeReference<Object> returntypedReference = getFactory().Class().createReference("void");

    Set<ModifierKind> modifiers = new LinkedHashSet<ModifierKind>(2);
    modifiers.add(ModifierKind.PUBLIC);
    modifiers.add(ModifierKind.STATIC);

    String invocation = parent.getQualifiedName() + "(";
    if (parent.getConstructor() == null) {
      // CtTypeReference<?> superClass = parent.getSuperclass();
      if (parent.getConstructor(ctInvocation.getFactory().Class().STRING) != null) {
        invocation += "\"" + parent.getSimpleName() + "\"";
      } else {
        return;
      }
    }
    invocation += ")";

    CtBlock<?> body = getFactory().Core().createBlock();
    String bodyString = "for (String method : methods) {\n";
    bodyString += "\t\tSystem.out.println(method);\n\t\t";
    bodyString += parent.getQualifiedName() + " instance = new " + invocation + ";\n\t\t";
    if (parent.getMethod("setUp") != null) {
      bodyString += "instance.setUp();";
    }
    bodyString +=
        parent.getQualifiedName() + ".class.getMethod(method, null).invoke(instance);\n\t\t";
    bodyString += "}\n";
    body.addStatement(getFactory().Code().createCodeSnippetStatement(bodyString));
    HashSet<CtTypeReference<? extends Throwable>> exceptions =
        new HashSet<CtTypeReference<? extends Throwable>>();
    exceptions.add(getFactory().Class().createReference(Exception.class));
    CtMethod<?> method =
        getFactory()
            .Method()
            .create(
                parent,
                modifiers,
                returntypedReference,
                "runJPFTest",
                new ArrayList<CtParameter<?>>(),
                exceptions,
                body);
    getFactory().Method().createParameter(method, typedReference, "methods");
  }
コード例 #2
0
ファイル: PerstTranslator.java プロジェクト: kowalot/volante
 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();
   }
 }
コード例 #3
0
ファイル: MemberResolver.java プロジェクト: veinrein/usemon
  private Method lookupMethod(
      CtClass clazz,
      String methodName,
      int[] argTypes,
      int[] argDims,
      String[] argClassNames,
      boolean onlyExact)
      throws CompileError {
    Method maybe = null;
    ClassFile cf = clazz.getClassFile2();
    // If the class is an array type, the class file is null.
    // If so, search the super class java.lang.Object for clone() etc.
    if (cf != null) {
      List list = cf.getMethods();
      int n = list.size();
      for (int i = 0; i < n; ++i) {
        MethodInfo minfo = (MethodInfo) list.get(i);
        if (minfo.getName().equals(methodName)) {
          int res = compareSignature(minfo.getDescriptor(), argTypes, argDims, argClassNames);
          if (res != NO) {
            Method r = new Method(clazz, minfo, res);
            if (res == YES) return r;
            else if (maybe == null || maybe.notmatch > res) maybe = r;
          }
        }
      }
    }

    if (onlyExact) maybe = null;
    else onlyExact = maybe != null;

    int mod = clazz.getModifiers();
    boolean isIntf = Modifier.isInterface(mod);
    try {
      // skip searching java.lang.Object if clazz is an interface type.
      if (!isIntf) {
        CtClass pclazz = clazz.getSuperclass();
        if (pclazz != null) {
          Method r = lookupMethod(pclazz, methodName, argTypes, argDims, argClassNames, onlyExact);
          if (r != null) return r;
        }
      }
    } catch (NotFoundException e) {
    }

    if (isIntf || Modifier.isAbstract(mod))
      try {
        CtClass[] ifs = clazz.getInterfaces();
        int size = ifs.length;
        for (int i = 0; i < size; ++i) {
          Method r = lookupMethod(ifs[i], methodName, argTypes, argDims, argClassNames, onlyExact);
          if (r != null) return r;
        }

        if (isIntf) {
          // finally search java.lang.Object.
          CtClass pclazz = clazz.getSuperclass();
          if (pclazz != null) {
            Method r =
                lookupMethod(pclazz, methodName, argTypes, argDims, argClassNames, onlyExact);
            if (r != null) return r;
          }
        }
      } catch (NotFoundException e) {
      }

    return maybe;
  }