private void visitJavaFileToPopulateCache(AccessibleObject methodOrConstructor) {

    InputStream is = null;
    try {
      is = javaFileFinder.openJavaFile(methodOrConstructor);
      if (is != null) {
        // visit .java file using our custom GenericVisitorAdapter
        CompilationUnit cu = JavaParser.parse(is);
        MethodParametersVisitor visitor = new MethodParametersVisitor();
        cu.accept(visitor, cache);
      }
    } catch (Exception e) {
      throw new ParameterNamesNotFoundException(
          "Error while trying to read parameter names from the Java file which contains the declaration of "
              + methodOrConstructor.toString(),
          e);
    } finally {
      if (is != null) {
        try {
          is.close();
        } catch (IOException e) {
          // should never happen
        }
      }
    }
  }
  @Test
  public void testRpcSample() throws Exception {
    CompilationUnit compilationUnit =
        JavaParser.parse(getClass().getResourceAsStream("testRpcSample.java.resource"));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ClassOrInterfaceDeclaration mainType = getMainType(compilationUnit);
    rpcPojosAjGenerator.generateEntity(compilationUnit, mainType, baos);

    byte[] expectedBytes =
        IOUtils.toByteArray(getClass().getResourceAsStream("rpcSample_Aspect.aj.expected"));
    AssertUtils.assertContent(expectedBytes, baos.toByteArray());

    List<BodyDeclaration> members = mainType.getMembers();
    for (BodyDeclaration bodyDeclaration : members) {
      // look for inner classes
      baos = new ByteArrayOutputStream();
      if (bodyDeclaration instanceof ClassOrInterfaceDeclaration) {
        rpcPojosAjGenerator.generatePart(
            compilationUnit,
            (ClassOrInterfaceDeclaration) bodyDeclaration,
            baos,
            mainType.getName());
        String expected =
            ((ClassOrInterfaceDeclaration) bodyDeclaration).getName() + "_Aspect.aj.expected";
        expectedBytes = IOUtils.toByteArray(getClass().getResourceAsStream(expected));
        AssertUtils.assertContent(expectedBytes, baos.toByteArray());
      }
    }
  }
예제 #3
0
 private CompilationUnit getCompilationUnit(SourceFile sourceFile) {
   CompilationUnit compilationUnit;
   if (Files.exists(sourceFile.getOutputPath())) {
     try {
       compilationUnit = JavaParser.parse(sourceFile.getOutputPath().toFile());
     } catch (Throwable t) {
       throw new RuntimeException(t);
     }
   } else {
     compilationUnit = new CompilationUnit();
     compilationUnit.setComment(
         new LineComment(" Generated by GraphWalker (http://www.graphwalker.org)"));
     if (!"".equals(sourceFile.getPackageName())) {
       compilationUnit.setPackage(createPackageDeclaration(sourceFile));
     }
     compilationUnit.setImports(
         Arrays.asList(
             new ImportDeclaration(
                 new NameExpr("org.graphwalker.java.annotation.Model"), false, false),
             new ImportDeclaration(
                 new NameExpr("org.graphwalker.java.annotation.Vertex"), false, false),
             new ImportDeclaration(
                 new NameExpr("org.graphwalker.java.annotation.Edge"), false, false)));
     ASTHelper.addTypeDeclaration(compilationUnit, getInterfaceName(sourceFile));
   }
   return compilationUnit;
 }
예제 #4
0
  /**
   * This adds a single file to the parser
   *
   * @param rootPath The root path to the source folder
   * @param relativeFilePath The relative path inside the root path to the file. The file will be
   *     added to the map with the key of the entire relative path so that lookups from within
   *     Sorbet do not have any ambiguity
   * @return 0 if success, 1 if failed
   */
  private int addFile(String rootPath, String relativeFilePath) {
    try {
      // Open file
      FileInputStream file = new FileInputStream(rootPath + File.separator + relativeFilePath);

      // Setup parser
      CompilationUnit cu = JavaParser.parse(file);

      // Visit the file with our visitor
      VariableVisitor<Object> variableVisitor = new VariableVisitor<Object>();
      variableVisitor.visit(cu, null);

      Lines lines = variableVisitor.getLines();

      if (files.containsKey(relativeFilePath)) {
        // Already contains this file, return failed
        return 1;
      } else {
        files.put(relativeFilePath, lines);
      }

    } catch (FileNotFoundException e) {
      // File doesn't exist, return failed
      return 1;
    } catch (ParseException e) {
      // File isn't valid, return failed
      return 1;
    }

    // Return success
    return 0;
  }
  private void testGenerate(String javaSource, String expectAspect)
      throws IOException, TemplateException, ParseException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    CompilationUnit compilationUnit = JavaParser.parse(getClass().getResourceAsStream(javaSource));

    rpcPojosAjGenerator.generateEntity(compilationUnit, getMainType(compilationUnit), baos);

    byte[] expectedBytes = IOUtils.toByteArray(getClass().getResourceAsStream(expectAspect));

    AssertUtils.assertContent(expectedBytes, baos.toByteArray());
  }
  public RealClass(String filePath, HashSet<String> classList) {

    this.realFilePath = filePath;
    this.classList = classList;
    this.className = getClassNameFromPath(realFilePath);
    // Initialize all lists
    setClassRelations(new ArrayList<ClassRelation>());
    setClassAttributes(new ArrayList<ClassAttribute>());
    setClassMethods(new ArrayList<ClassMethod>());
    setExtendsFrom(new ArrayList<String>());
    setImplementsFrom(new ArrayList<String>());
    setRelationList(new RelationList(className, classRelations));
    // Initialize all UML queries
    this.setUMLClassDetail("");
    this.UMLClassName = "";
    this.setUMLClassRelation("");
    // Get Compilation Unit
    FileInputStream inputStream;
    try {
      inputStream = new FileInputStream(realFilePath);
      cUnit = JavaParser.parse(inputStream);
      inputStream.close();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (ParseException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
    // Get data
    if (cUnit != null) {
      // Find extends & implements
      getExtends();
      // Find Attributes
      getAttributes();
      // Find Methods
      getMethods();
      // Find Constructor
      getConstructor();
      // Alter Attributes
      // invoked in TestRun.java(main function), due to all class information needed
      // Alter Methods
      // Same as Alter Attributes
    } else {
      System.out.println("CompilationUnit is null");
    }
    if (classRelations != null && classRelations.size() != 0 && relationList != null) {
      this.setRelationList(new RelationList(this.getClassName(), this.classRelations));
    } else {
      refreshRelationList();
    }
  }
예제 #7
0
  public void calculate(InputStream is) {
    try {
      CompilationUnit cunit = JavaParser.parse(is);

      classInfo = new ClassInfoVisitor();
      classInfo.visit(cunit, null);

      visitor = new CCVisitor();
      visitor.visit(cunit, null);

    } catch (Throwable t) {
      throw new RuntimeException(t);
    }
  }
  @Override
  public void calculate(SourceCode source, InputStream is) {
    this.source = source;
    try {
      CompilationUnit cunit = JavaParser.parse(is);

      classInfo = new ClassInfoVisitor();
      classInfo.visit(cunit, null);

      visitor = new UnitOrSystemTestVisitor(classInfo.getName());
      visitor.visit(cunit, null);

      tests = visitor.getTests();

    } catch (Throwable t) {
      throw new RuntimeException(t);
    }
  }
예제 #9
0
파일: Visit.java 프로젝트: utngz/wikbook
 CU(String source) throws ParseException {
   this.compilationUnit = JavaParser.parse(new ByteArrayInputStream(source.getBytes()));
   this.source = source;
   this.sb = new TextArea(source);
   this.types = new ArrayList<TypeSource>();
 }
예제 #10
0
 @Test
 public void testStatement() throws Exception {
   Statement stmt = JavaParser.parseStatement("x = x+y;");
   Assert.assertNotNull(stmt);
 }
예제 #11
0
  @Test
  public void testBlockStatement() throws Exception {
    BlockStmt bs = JavaParser.parseBlock("{return x+y;}");

    Assert.assertNotNull(bs);
  }