public void testGetParameterizedType() {
    sc.getProjectSettings().ensureSystemClassesAreInPath();
    NameInfo ni = sc.getNameInfo();
    ClassType ct = ni.getClassType("java.util.List<java.lang.Number>");
    assertTrue(ct.getFullSignature().equals("java.util.List<java.lang.Number>"));
    ClassType ct2 = ni.getClassType("java.util.List<? extends java.lang.Number>");
    assertTrue(ct != ct2);
    assertTrue(!ct.equals(ct2));
    assertTrue(ct2.getFullSignature().equals("java.util.List<? extends java.lang.Number>"));
    ClassType ct3 = ni.getClassType("java.util.List<java.lang.String>[]");
    assertTrue(ct3.getFullSignature().equals("java.util.List<java.lang.String>[]"));
    ArrayType at3 = (ArrayType) ct3;
    ParameterizedType pt3 = (ParameterizedType) at3.getBaseType();
    assertTrue(pt3.getFullSignature().equals("java.util.List<java.lang.String>"));
    // query ct3 again, to see if caching works properly in NameInfo:
    ClassType ct3_2 = ni.getClassType("java.util.List<java.lang.String>[]");
    ArrayType at3_2 = (ArrayType) ct3_2;
    ParameterizedType pt3_2 = (ParameterizedType) at3_2.getBaseType();
    assertTrue(pt3_2 == pt3);
    assertTrue(at3_2 == at3);
    assertTrue(ct3_2 == ct3);
    ClassType ct4 = ni.getClassType("java.util.List<java.util.List<java.lang.String>>");
    assertTrue(ct4.getFullSignature().equals("java.util.List<java.util.List<java.lang.String>>"));
    ClassType ct5 = ni.getClassType("java.util.List<CannotFindThis>");
    assertNull(ct5); // should be null entirely!

    // try java.lang loading
    ClassType ct6 = ni.getClassType("String");
    assertTrue(ct6 == ni.getJavaLangString());
    ClassType ct7 = ni.getClassType("java.util.List<String>");
    assertEquals(ct7, pt3);
    ClassType ct8 = ni.getClassType("Thread");
    assertNotNull(ct8);
  }
Пример #2
0
  public static void main(String[] args) {
    // setting src path
    int cont = 0;
    List<CompilationUnit> cul =
        serviceConfiguration("/home/alexandre/NetBeansProjects/TesteSimples/src/testesimples/");
    String arg = "";
    // traversing the abstract syntax trees of the parsed .java files
    for (CompilationUnit cunit : cul) {
      TreeWalker tw = new TreeWalker(cunit);
      while (tw.next()) {
        ProgramElement pe = tw.getProgramElement();
        // getting class information

        if (pe instanceof ClassDeclaration) {
          System.out.println("\n------- Class -------");
          ClassDeclaration cls = (ClassDeclaration) pe;
          System.out.println(cls.getFullName());
          arg = cls.getFullName();
          System.out.println("\n------- Methods -------");
          List<Method> methods = cls.getMethods();
          List<? extends VariableSpecification> var = cls.getVariablesInScope();

          for (Method method : methods) {
            System.out.println(method.getFullName());
            if (method.isPublic()) {
              System.out.println("Acesso: Public");
            } else {
              System.out.println("Acesso: Default");
            }
            if (method.getReturnType() != null) {
              System.out.println("Tipo de retornado: " + method.getReturnType());
            } else {
              System.out.println("Tipo de retorno: void");
            }
            List<recoder.abstraction.Type> paramis = method.getSignature();
            if (paramis.isEmpty()) {
              System.out.println("Método não recebe parametros");

            } else {
              System.out.println("número de parâmetros " + paramis.size());
              cont = 0;
              for (recoder.abstraction.Type parami : paramis) {
                cont++;
                System.out.println(cont + " Parametro: " + parami.getName());
              }
            }
            if (!var.isEmpty()) {
              System.out.println("Variáveis declaradas no escopo");
              for (VariableSpecification va : var) {
                System.out.println(va.getFullName());
              }
            }
            List<ClassType> ex = method.getExceptions();
            if (!ex.isEmpty()) {
              System.out.println("EX");
              for (ClassType e : ex) {
                System.out.println(e.getName());
              }
            }
            System.out.println("\n------- ** -------");
          }
        }
        // getting method information
        if (pe instanceof MethodDeclaration) {
          TreeWalker tw2 = new TreeWalker(pe);
          while (tw2.next()) {
            ProgramElement pe2 = tw2.getProgramElement();
            if (pe2 instanceof CopyAssignment) {
              CopyAssignment ca = (CopyAssignment) pe2;
              String info = ca.getStartPosition().getLine() + ": " + ca.toSource().trim();
              System.out.println("\n--> Definition of " + ca.getFirstElement() + ":\n" + info);
            }
          }
        }
      }
    }
  }