Exemplo n.º 1
0
  /**
   * The main method.
   *
   * @param args the arguments
   */
  public static void main(String[] args) {

    RandomModificatorsClass randomClass = new RandomModificatorsClass();

    Field[] flds = randomClass.getClass().getDeclaredFields();
    System.out.println("VARIABLES: \n");
    for (int i = 0; i < flds.length; i++) {
      String name = flds[i].getName();
      flds[i].setAccessible(true);
      Class<?> type = flds[i].getType();
      int modifier = flds[i].getModifiers();

      System.out.print(
          "Name:"
              + name
              + "\nType:"
              + type
              + "\nModifier: "
              + Modifier.toString(modifier)
              + "\n\n");
    }

    Method[] method = randomClass.getClass().getDeclaredMethods();
    System.out.println("METHODS:\n");
    for (int i = 0; i < method.length; i++) {
      String name = method[i].getName();
      flds[i].setAccessible(true);
      Class<?> type = method[i].getReturnType();
      int modifier = method[i].getModifiers();

      System.out.print(
          "Name:"
              + name
              + "\nType:"
              + type
              + "\nModifier: "
              + Modifier.toString(modifier)
              + "\n\n");
    }

    @SuppressWarnings("rawtypes")
    Constructor[] constr = randomClass.getClass().getDeclaredConstructors();
    System.out.println("CONSTRUCTORS:\n");
    for (int i = 0; i < constr.length; i++) {
      String name = constr[i].getName();
      flds[i].setAccessible(true);

      int modifier = constr[i].getModifiers();

      System.out.print(
          "Name:" + name + "\nType:" + "\nModifier: " + Modifier.toString(modifier) + "\n\n");
    }
  }
Exemplo n.º 2
0
 public String toString(ConstantInfo pool[]) {
   String t = "";
   t += java.lang.reflect.Modifier.toString(getAccessFlags()) + " ";
   t += getDescriptor(pool) + " ";
   t += getName(pool);
   return t;
 }
 public void generate(JNIField field) {
   String name = field.getName();
   Iterator<File> keys = files.keySet().iterator();
   while (keys.hasNext()) {
     File key = keys.next();
     String str = files.get(key);
     if (str.indexOf(name) != -1) {
       int modifiers = field.getModifiers();
       String modifiersStr = Modifier.toString(modifiers);
       output("\t");
       output(modifiersStr);
       if (modifiersStr.length() > 0) output(" ");
       output(field.getType().getTypeSignature3(false));
       output(" ");
       output(field.getName());
       output(" = ");
       output(getFieldValue(field));
       outputln(";");
       usedCount++;
       return;
     }
   }
   unusedCount++;
   // output("NOT USED=" + field.toString() + " \n");
 }
Exemplo n.º 4
0
  public static void main(String... args) {
    try {
      Class<?> c = Class.forName(args[0]);
      int searchMods = 0x0;
      for (int i = 1; i < args.length; i++) {
        searchMods |= modifierFromString(args[i]);
      }

      Field[] flds = c.getDeclaredFields();
      out.format(
          "Fields in Class '%s' containing modifiers:  %s%n",
          c.getName(), Modifier.toString(searchMods));
      boolean found = false;
      for (Field f : flds) {
        int foundMods = f.getModifiers();
        // Require all of the requested modifiers to be present
        if ((foundMods & searchMods) == searchMods) {
          out.format(
              "%-8s [ synthetic=%-5b enum_constant=%-5b ]%n",
              f.getName(), f.isSynthetic(), f.isEnumConstant());
          found = true;
        }
      }

      if (!found) {
        out.format("No matching fields%n");
      }

      // production code should handle this exception more gracefully
    } catch (ClassNotFoundException x) {
      x.printStackTrace();
    }
  }
Exemplo n.º 5
0
  /**
   * 将RyJbxx转换为xml格式的字符串
   *
   * @param head XML的头名字<head>..</head>
   * @return 返回XML字符串
   * @throws Exception
   */
  public static String toXml(String head, ConsructionHeadVO headVo, Consruction1104RequestVO rj)
      throws Exception {

    StringBuffer sb = new StringBuffer();
    Field[] field = rj.getClass().getDeclaredFields();
    String ENDL = "\n";
    String TABLE = "\t";
    String name = "";

    for (int i = 0; i < field.length; i++) {
      // 跳过表态属性
      String mod = Modifier.toString(field[i].getModifiers());
      if (mod.indexOf("static") != -1) {
        continue;
      }
      name = TEST.FirstStringToUpperCase(field[i].getName());
      Method m = rj.getClass().getMethod("get" + name);
      String value = (String) m.invoke(rj);
      // 构造XML
      sb.append(TABLE)
          .append("<")
          .append(name)
          .append(">")
          .append(value)
          .append("</")
          .append(name)
          .append(">")
          .append(ENDL);
    }
    return "<" + head + ">" + sb.toString() + "</" + head + ">";
  }
Exemplo n.º 6
0
 protected void formatName(StringBuilder formatted) {
   if (modifiers != 0) {
     formatted.append(Modifier.toString(modifiers)).append(" ");
   }
   formatted.append(returntype.getSimpleName()).append(" ");
   formatted.append(methodname);
 }
Exemplo n.º 7
0
  public static void main(String[] args) {
    // read class name from command line args or user input
    String name;
    if (args.length > 0) name = args[0];
    else {
      Scanner in = new Scanner(System.in);
      System.out.println("Enter class name (e.g. java.util.Date): ");
      name = in.next();
    }

    try {
      // print class name and superclass name (if != Object)
      Class cl = Class.forName(name);
      Class supercl = cl.getSuperclass();
      String modifiers = Modifier.toString(cl.getModifiers());
      if (modifiers.length() > 0) System.out.print(modifiers + " ");
      System.out.print("class " + name);
      if (supercl != null && supercl != Object.class)
        System.out.print(" extends " + supercl.getName());

      System.out.print("\n{\n");
      printConstructors(cl);
      System.out.println();
      printMethods(cl);
      System.out.println();
      printFields(cl);
      System.out.println("}");
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    }
    System.exit(0);
  }
Exemplo n.º 8
0
  /**
   * Returns a string containing a concise, human-readable description of this method. The format of
   * the string is:
   *
   * <ol>
   *   <li>modifiers (if any)
   *   <li>return type or 'void'
   *   <li>declaring class name
   *   <li>'('
   *   <li>parameter types, separated by ',' (if any)
   *   <li>')'
   *   <li>'throws' plus exception types, separated by ',' (if any)
   * </ol>
   *
   * For example: {@code public native Object java.lang.Method.invoke(Object,Object) throws
   * IllegalAccessException,IllegalArgumentException ,InvocationTargetException}
   *
   * @return a printable representation for this method
   */
  @Override
  public String toString() {
    Class<?> returnType = getReturnType();
    Class<?> declaringClass = getDeclaringClass();
    String name = getName();
    Class<?>[] parameterTypes = getParameterTypes(false);
    Class<?>[] exceptionTypes = getExceptionTypes(false);

    StringBuilder result = new StringBuilder(Modifier.toString(getModifiers()));

    if (result.length() != 0) result.append(' ');
    appendTypeName(result, returnType);
    result.append(' ');
    result.append(declaringClass.getName());
    result.append('.');
    result.append(name);
    result.append("(");
    result.append(toString(parameterTypes));
    result.append(")");
    if (exceptionTypes != null && exceptionTypes.length != 0) {
      result.append(" throws ");
      result.append(toString(exceptionTypes));
    }

    return result.toString();
  }
Exemplo n.º 9
0
 public static void main(String[] args) {
   String name;
   if (args.length > 0) {
     name = args[0];
   } else {
     Scanner in = new Scanner(System.in);
     System.out.println("Enter class name (e.g. java.util.Date ):)");
     name = in.next();
   }
   try {
     Class<?> aClass = Class.forName(name);
     Class<?> superclass = aClass.getSuperclass();
     String modifiers = Modifier.toString(aClass.getModifiers());
     if (modifiers.length() > 0) {
       System.out.print(modifiers + " ");
     }
     System.out.print("class " + name);
     if (superclass != null && superclass != Object.class) {
       System.out.print(" extends " + superclass.getName());
     }
     System.out.print("\n{\n");
     printConstructors(aClass);
     System.out.println();
     printMethods(aClass);
     System.out.println();
     printFields(aClass);
     System.out.println("}");
   } catch (ClassNotFoundException e) {
     e.printStackTrace();
   }
   System.exit(0);
 }
 public void generate(JNIField field) {
   String name = field.getName();
   Enumeration keys = files.keys();
   while (keys.hasMoreElements()) {
     Object key = keys.nextElement();
     String str = (String) files.get(key);
     if (str.indexOf(name) != -1) {
       int modifiers = field.getModifiers();
       String modifiersStr = Modifier.toString(modifiers);
       output("\t");
       output(modifiersStr);
       if (modifiersStr.length() > 0) output(" ");
       output(field.getType().getTypeSignature3(false));
       output(" ");
       output(field.getName());
       output(" = ");
       output(getFieldValue(field));
       outputln(";");
       usedCount++;
       return;
     }
   }
   unusedCount++;
   // output("NOT USED=" + field.toString() + " \n");
 }
Exemplo n.º 11
0
  public void dumpDeclaration(
      TabbedPrintWriter writer, ProgressListener pl, double done, double scale) throws IOException {
    if (fields == null) {
      /* This means that the class could not be loaded.
       * give up.
       */
      return;
    }

    writer.startOp(writer.NO_PAREN, 0);
    /* Clear the SUPER bit, which is also used as SYNCHRONIZED bit. */
    int modifiedModifiers = modifiers & ~(Modifier.SYNCHRONIZED | STRICTFP);
    if (clazz.isInterface())
      /* interfaces are implicitily abstract */
      modifiedModifiers &= ~Modifier.ABSTRACT;
    if (parent instanceof MethodAnalyzer) {
      /* method scope classes are implicitly private */
      modifiedModifiers &= ~Modifier.PRIVATE;
      /* anonymous classes are implicitly final */
      if (name == null) modifiedModifiers &= ~Modifier.FINAL;
    }
    String modif = Modifier.toString(modifiedModifiers);
    if (modif.length() > 0) writer.print(modif + " ");
    if (isStrictFP()) {
      /* The STRICTFP modifier is set.
       * We handle it, since java.lang.reflect.Modifier is too dumb.
       */
      writer.print("strictfp ");
    }
    /* interface is in modif */
    if (!clazz.isInterface()) writer.print("class ");
    writer.print(name);
    ClassInfo superClazz = clazz.getSuperclass();
    if (superClazz != null && superClazz != ClassInfo.javaLangObject) {
      writer.breakOp();
      writer.print(" extends " + (writer.getClassString(superClazz, Scope.CLASSNAME)));
    }
    ClassInfo[] interfaces = clazz.getInterfaces();
    if (interfaces.length > 0) {
      writer.breakOp();
      writer.print(clazz.isInterface() ? " extends " : " implements ");
      writer.startOp(writer.EXPL_PAREN, 1);
      for (int i = 0; i < interfaces.length; i++) {
        if (i > 0) {
          writer.print(", ");
          writer.breakOp();
        }
        writer.print(writer.getClassString(interfaces[i], Scope.CLASSNAME));
      }
      writer.endOp();
    }
    writer.println();

    writer.openBraceClass();
    writer.tab();
    dumpBlock(writer, pl, done, scale);
    writer.untab();
    writer.closeBraceClass();
  }
Exemplo n.º 12
0
 /**
  * append the modifier's source representation to the writer; return true if any source was
  * actually written to the writer (it is possible that no source was written out); this return
  * value allows callers to know whether to append a space if necessary
  */
 boolean writeSource(PrintWriter pw) {
   String source = Modifier.toString(this.code);
   if (source.length() == 0) {
     return false;
   }
   pw.print(source);
   return true;
 }
Exemplo n.º 13
0
 public ClassBuffer createLocalClass(final String classDef) {
   final ClassBuffer cls = new ClassBuffer(context, indent);
   cls.setDefinition(classDef, classDef.trim().endsWith("{"));
   assert cls.privacy == 0 : "A local class cannot be " + Modifier.toString(cls.privacy);
   addToEnd(cls);
   setNotIndent();
   return cls;
 }
Exemplo n.º 14
0
 public String toString() {
   return Modifier.toString(modifiers)
       + "("
       + Integer.toHexString(modifiers)
       + ") "
       + name()
       + signature();
 }
Exemplo n.º 15
0
  @Override
  public void visit(ConstructorDeclaration n, Object arg) {

    Set<String> consParaSet = new HashSet<String>();
    List<String> consParalist = new ArrayList<String>();

    currentConstructorMeta = new ConstructorMeta();
    currentConstructorMeta.constructorName = n.getName();
    currentConstructorMeta.constructorModifier = Modifier.toString(n.getModifiers());

    if (n.getParameters() != null) {
      for (int i = 0; i < n.getParameters().size(); i++) {

        String consPara = n.getParameters().get(i).toString();
        consParalist.add(consPara);
        String[] strings = consPara.split(" ");

        String consParaKey = currentConstructorMeta.constructorName;

        if (MapAndSet.constructorMap.containsKey(consParaKey)) {

          MapAndSet.constructorMap.get(consParaKey).add(consPara);
          MapAndSet.constructorMap.put(consParaKey, consParaSet);

        } else {
          consParaSet.add(consPara);
          MapAndSet.constructorMap.put(consParaKey, consParaSet);
        }

        String usesOrInterfaceKey = strings[0] + "," + currentConstructorMeta.constructorName;

        if (MapAndSet.usesMap.containsKey(usesOrInterfaceKey)) {

          //                    MapAndSet.usesOrInterfaceValue =
          // MapAndSet.usesMap.get(usesOrInterfaceKey);
          //                    MapAndSet.usesOrInterfaceValue =
          // MapAndSet.arrowsUpdate(MapAndSet.usesOrInterfaceValue, Arrows.uses.toString());
          //                    MapAndSet.usesOrInterfaceMap.put(usesOrInterfaceKey,
          // MapAndSet.usesOrInterfaceValue);

        } else if (!MapAndSet.usesMap.containsKey(usesOrInterfaceKey)) {

          MapAndSet.usesMap.put(usesOrInterfaceKey, Arrows.uses.toString());
        }
      }

      currentConstructorMeta.parameterList = consParalist;
      currentMeta.constructorMetas.add(currentConstructorMeta);

    } else {

      System.out.println("to check the ecnomy");
      currentMeta.constructorMetas.add(currentConstructorMeta);
    }

    MapAndSet.classMetaMap.put(MapAndSet.className, currentMeta);
  }
 public static int toString____Ljava_lang_String_2(MJIEnv env, int objRef) {
   StringBuilder sb = new StringBuilder();
   FieldInfo fi = getFieldInfo(env, objRef);
   sb.append(Modifier.toString(fi.getModifiers()));
   sb.append(' ');
   sb.append(fi.getType());
   sb.append(' ');
   sb.append(fi.getFullName());
   int sref = env.newString(sb.toString());
   return sref;
 }
Exemplo n.º 17
0
 public String getQualifiedName() {
   StringWriter sbuf = new StringWriter();
   sbuf.write(Modifier.toString(getModifiers()));
   sbuf.write(' ');
   sbuf.write(getType().getQualifiedName());
   sbuf.write(' ');
   sbuf.write(getContainingClass().getQualifiedName());
   sbuf.write('.');
   sbuf.write(getSimpleName());
   return sbuf.toString();
 }
Exemplo n.º 18
0
  private Method findMethod(Class<?> clz, Object[] args) throws LocalRunException {
    if (args == null || args.length == 0) {
      throw new LocalRunException("Input data can't be null");
    }
    Method method = ClassUtils.findMethod(clz, "evaluate", args);
    // method "evaluate" can't be static
    if (Modifier.toString(method.getModifiers()).contains("static")) {
      throw new LocalRunException("'evaluate' method can't be static");
    }

    return method;
  }
Exemplo n.º 19
0
  /**
   * Prints all fields of a class
   *
   * @param cl a class
   */
  public static void printFields(Class cl) {
    Field[] fields = cl.getDeclaredFields();

    for (Field f : fields) {
      Class type = f.getType();
      String name = f.getName();
      System.out.print("   ");
      String modifiers = Modifier.toString(f.getModifiers());
      if (modifiers.length() > 0) System.out.print(modifiers + " ");
      System.out.println(type.getName() + " " + name + ";");
    }
  }
Exemplo n.º 20
0
  protected IValueList parseModifiers(int modifiers) {
    IValueList extendedModifierList = new IValueList(values);

    for (String constructor : java.lang.reflect.Modifier.toString(modifiers).split(" ")) {
      Set<org.rascalmpl.value.type.Type> exConstr =
          typeStore.lookupConstructor(DATATYPE_RASCAL_AST_MODIFIER_NODE_TYPE, constructor);
      for (org.rascalmpl.value.type.Type con : exConstr) {
        extendedModifierList.add(values.constructor(con));
      }
    }

    return extendedModifierList;
  }
Exemplo n.º 21
0
 private static void printFields(Class<?> aClass) {
   Field[] fields = aClass.getDeclaredFields();
   for (Field field : fields) {
     String name = field.getName();
     Class<?> type = field.getType();
     System.out.print("  ");
     String s = Modifier.toString(field.getModifiers());
     if (s.length() > 0) {
       System.out.print(s + " ");
     }
     System.out.print(type.getName() + " " + name + ";");
     System.out.println();
   }
 }
Exemplo n.º 22
0
  public static void main(String... args) {
    try {
      Class<?> c = Class.forName(args[0]);
      out.format("Class:%n  %s%n%n", c.getCanonicalName());
      out.format("Modifiers:%n  %s%n%n", Modifier.toString(c.getModifiers()));

      out.format("Type Parameters:%n");
      TypeVariable<?>[] tv = c.getTypeParameters();
      if (tv.length != 0) {
        out.format("  ");
        for (TypeVariable<?> t : tv) out.format("%s ", t.getName());
        out.format("%n%n");
      } else {
        out.format("  -- No Type Parameters --%n%n");
      }

      out.format("Implemented Interfaces:%n");
      Type[] intfs = c.getGenericInterfaces();
      if (intfs.length != 0) {
        for (Type intf : intfs) out.format("  %s%n", intf.toString());
        out.format("%n");
      } else {
        out.format("  -- No Implemented Interfaces --%n%n");
      }

      out.format("Inheritance Path:%n");
      List<Class<?>> l = new ArrayList<Class<?>>();
      printAncestor(c, l);
      if (l.size() != 0) {
        for (Class<?> cl : l) out.format("  %s%n", cl.getCanonicalName());
        out.format("%n");
      } else {
        out.format("  -- No Super Classes --%n%n");
      }

      out.format("Annotations:%n");
      Annotation[] ann = c.getAnnotations();
      if (ann.length != 0) {
        for (Annotation a : ann) out.format("  %s%n", a.toString());
        out.format("%n");
      } else {
        out.format("  -- No Annotations --%n%n");
      }

      // production code should handle this exception more gracefully
    } catch (ClassNotFoundException x) {
      x.printStackTrace();
    }
  }
Exemplo n.º 23
0
  protected static final Map<String, Method> scanMethod(Class o) {

    Map<String, Method> objAnnotation = new HashMap<String, Method>();
    Method[] M = o.getDeclaredMethods();
    for (Method m : M) {
      String ax =
          String.format(
              "%s %s %s(%s);",
              Modifier.toString(m.getModifiers()),
              m.getReturnType().getName(),
              m.getName(),
              Util.getParameterTypes(m.getParameterTypes()));
      System.out.println(ax);
    }
    return objAnnotation;
  }
Exemplo n.º 24
0
  public static void main(String[] args) throws JClassAlreadyExistsException, IOException {
    JCodeModel codeModel = new JCodeModel();

    Class template = AbstractBaseWrapper.class;

    String packageName = template.getPackage().getName();

    String className = template.getSimpleName().replace("Abstract", "") + "Impl";
    JPackage generatedPackage = codeModel._package(packageName);

    JDefinedClass generatedClass = generatedPackage._class(JMod.FINAL, className); // Creates
    // a
    // new
    // class
    generatedClass._extends(template);

    for (Method method : template.getMethods()) {

      if (Modifier.isAbstract(method.getModifiers())) {
        System.out.println(
            "Found abstract method "
                + Modifier.toString(method.getModifiers())
                + " "
                + method.getName());

        JMethod generatedMethod =
            generatedClass.method(JMod.PUBLIC, method.getReturnType(), method.getName());

        for (Parameter parameter : method.getParameters()) {
          generatedMethod.param(parameter.getModifiers(), parameter.getType(), parameter.getName());
        }

        if (method.getReturnType().equals(Void.TYPE)) {
          generatedMethod.body().add(generateInvocation(method));
        } else {

          generatedMethod.body()._return(generateInvocation(method));
        }
      }
    }

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    codeModel.build(new SingleStreamCodeWriter(out));

    System.out.println(out);
  }
Exemplo n.º 25
0
 public MethodVisitor visitMethod(
     int access, String name, String desc, String signature, String[] exceptions) {
   pn("");
   pn("; -------------------------------------------------------------");
   p(".method ");
   p(Modifier.toString(access));
   p(" ");
   p(name);
   pn(desc);
   pn("; signature = " + signature);
   pn("; -------------------------------------------------------------\n");
   if (exceptions != null) {
     for (int i = 0; i < exceptions.length; i++) {
       p(".throws ");
       pn(exceptions[i]);
     }
   }
   return new DumpMethodVisitor();
 }
Exemplo n.º 26
0
  /**
   * Prints all constructors of a class
   *
   * @param cl a class
   */
  public static void printConstructors(Class cl) {
    Constructor[] constructors = cl.getDeclaredConstructors();

    for (Constructor c : constructors) {
      String name = c.getName();
      System.out.print("   ");
      String modifiers = Modifier.toString(c.getModifiers());
      if (modifiers.length() > 0) System.out.print(modifiers + " ");
      System.out.print(name + "(");

      // print parameter types
      Class[] paramTypes = c.getParameterTypes();
      for (int j = 0; j < paramTypes.length; j++) {
        if (j > 0) System.out.print(", ");
        System.out.print(paramTypes[j].getName());
      }
      System.out.println(");");
    }
  }
Exemplo n.º 27
0
  public void visit(
      int version,
      int access,
      String name,
      String signature,
      String superName,
      String[] interfaces) {
    p(".class ");
    p(Modifier.toString(access));
    p(" ");
    pn(name);
    if (superName != null) pn(".super " + superName);

    if (interfaces != null) {
      for (int i = 0; i < interfaces.length; i++) {
        p(".implements ");
        pn(interfaces[i]);
      }
    }
  }
Exemplo n.º 28
0
 private static void printConstructors(Class<?> aClass) {
   Constructor<?>[] constructors = aClass.getDeclaredConstructors();
   for (Constructor<?> constructor : constructors) {
     String name = constructor.getName();
     System.out.print("  ");
     String modifiers = Modifier.toString(aClass.getModifiers());
     if (modifiers.length() > 0) {
       System.out.print(modifiers + " ");
     }
     System.out.print(name + "(");
     Class<?>[] parameterTypes = constructor.getParameterTypes();
     for (int i = 0; i < parameterTypes.length; i++) {
       if (i > 0) {
         System.out.print(", ");
       }
       System.out.print(parameterTypes[i].getName());
     }
     System.out.println(");");
   }
 }
Exemplo n.º 29
0
 public FieldVisitor visitField(
     int access, String name, String desc, String signature, Object value) {
   p(".field ");
   p(Modifier.toString(access));
   p(" ");
   p(name);
   p(" ");
   p(desc);
   if (value != null) {
     p(" = ");
     if (value instanceof String) {
       pn("\"" + value + "\"");
     } else {
       pn(value.toString());
     }
   } else {
     pn();
   }
   return null;
 }
Exemplo n.º 30
0
 private static void printMethods(Class<?> aClass) {
   Method[] methods = aClass.getDeclaredMethods();
   for (Method method : methods) {
     String name = method.getName();
     Class<?> returnType = method.getReturnType();
     System.out.print("  ");
     String modifiers = Modifier.toString(method.getModifiers());
     if (modifiers.length() > 0) {
       System.out.print(modifiers + " ");
     }
     System.out.print(returnType + " " + name + "(");
     Class<?>[] parameterTypes = method.getParameterTypes();
     for (int i = 0; i < parameterTypes.length; i++) {
       if (i > 0) {
         System.out.print(", ");
       }
       System.out.print(parameterTypes[i].getName());
     }
     System.out.println(");");
   }
 }