Package japa.parser.ast

Examples of japa.parser.ast.CompilationUnit


   */
  public List<CompilationUnit> findMatchingSources(Collection<File> sources) {
    List<CompilationUnit> returned = new LinkedList<CompilationUnit>();
    for (File f : sources) {
      try {
        CompilationUnit searched = JavaParser.parse(f);
        ScanningMatcher interfaces = new RequiredInterfacesScanner(requiredInterfaces);
        ScanningMatcher annotations = new RequiredAnnotationsScanner(requiredAnnotations);
        if (interfaces.matches(searched) && annotations.matches(searched)) {
          returned.add(searched);
        }
View Full Code Here


public class JavaParserCreateCU {

    private static final Logger LOG = LoggerFactory.getLogger(JavaParserCreateCU.class);

    public static void main(String[] args) {
        CompilationUnit cu = new CompilationUnit();
        //set the package
        cu.setPackage(new PackageDeclaration(ASTHelper.createNameExpr("cn.shenyanchao.javaparser.test")));
        //create the type declaration
        ClassOrInterfaceDeclaration type = new ClassOrInterfaceDeclaration(ModifierSet.PUBLIC, false, "HelloWorld");
        ASTHelper.addTypeDeclaration(cu, type);
        //create a method
        MethodDeclaration method = new MethodDeclaration(ModifierSet.PUBLIC, ASTHelper.VOID_TYPE, "main");
        method.setModifiers(ModifierSet.addModifier(method.getModifiers(), ModifierSet.STATIC));
        ASTHelper.addMember(type, method);

        //add a parameter to the method
        Parameter param = ASTHelper.createParameter(ASTHelper.createReferenceType("String", 1), "args");
//        param.setVarArgs(true);
        ASTHelper.addParameter(method, param);

        //add a body to the method
        BlockStmt block = new BlockStmt();
        method.setBody(block);

        //add a statement do the method body
        NameExpr clazz = new NameExpr("System");
        FieldAccessExpr field = new FieldAccessExpr(clazz, "out");
        MethodCallExpr call = new MethodCallExpr(field, "println");
        ASTHelper.addArgument(call, new StringLiteralExpr("Hello World"));
        ASTHelper.addStmt(block, call);

        LOG.info(cu.toString());
    }
View Full Code Here

    /**
     * @param javaFile
     */
    private void convertJavaFile2Test(File javaFile) {

        CompilationUnit sourceCU = JavaParserFactory.getCompilationUnit(javaFile, sourceEncode);

        CompilationUnitBuilder compilationUnitBuilder = null;
        String testJavaFileName = JavaParserUtils.findTestJavaFileName(sourceCU, javaFile, testDir);
        boolean testExist = FileChecker.isTestJavaClassExist(new File(testJavaFileName));
        if (!testExist) {
            CommandInvoker invoker = new CommandInvoker(new NewTestCommand(new NewTestReceiver(sourceCU, javaFile)));
            compilationUnitBuilder = invoker.action();
        } else if (testExist) {
            CompilationUnit testCU = JavaParserFactory.getCompilationUnit(new File(testJavaFileName), sourceEncode);
            CommandInvoker invoker = new CommandInvoker(new ExistTestCommand(new ExistTestReceiver(sourceCU, javaFile,
                    testCU)));
            compilationUnitBuilder = invoker.action();
        }

        if (null != compilationUnitBuilder){
            CompilationUnit testCU = compilationUnitBuilder.build();
            //写入测试代码文件
            TestWriter.writeJavaTest(testJavaFileName, testCU.toString(), sourceEncode);
        }
    }
View Full Code Here

    private static final Logger LOG = LoggerFactory.getLogger(JavaParserFactory.class);

    public static CompilationUnit getCompilationUnit(File javaFile, String encoding) {

        CompilationUnit compilationUnit = null;
        try {
            compilationUnit = JavaParser.parse(javaFile, encoding);
        } catch (ParseException e) {
            LOG.error(e.getMessage());
        } catch (IOException e) {
View Full Code Here

        }
        return n1.equals(n2);
    }

    public Boolean visit(CompilationUnit n1, Node arg) {
        CompilationUnit n2 = (CompilationUnit) arg;

        if (!nodeEquals(n1.getPackage(), n2.getPackage())) {
            return Boolean.FALSE;
        }

        if (!nodesEquals(n1.getImports(), n2.getImports())) {
            return Boolean.FALSE;
        }

        if (!nodesEquals(n1.getTypes(), n2.getTypes())) {
            return Boolean.FALSE;
        }

        if (!nodesEquals(n1.getComments(), n2.getComments())) {
            return Boolean.FALSE;
        }

        return Boolean.TRUE;
    }
View Full Code Here

  }

  public void findAndReplaceIdentifier(File file, String word, String replacement) throws ParseException, IOException {
    System.out.println("\tReplacing \""+word+"\" with \""+replacement+"\"  in: "+file.getName());
   
    CompilationUnit cu = JavaParser.parse(file);
  }
View Full Code Here

                jj_consume_token(-1);
                throw new ParseException();
        }
        {
            if (true) {
                return new CompilationUnit(line == -1 ? 0 : line, column, token.endLine, token.endColumn, pakage, imports, types, getComments());
            }
        }
        throw new Error("Missing return statement in function");
    }
View Full Code Here

*
*/
public class InformerTextGenerator {

  public static CompilationUnit generateCompilationUnit(InformerInfos informerInfos, Collection<String> qualifiedEnums, Map<String, Class> resolvedInformers) {
    CompilationUnit cu = new CompilationUnit();
    // set the package
    cu.setPackage(new PackageDeclaration(ASTHelper.createNameExpr(informerInfos.classPackage)));

    // Finally add imports (they may have been "improved" by informers generation)
    List<ImportDeclaration> baseImports = informerInfos.imports;
    // Extracting effective imports
    Collection<String> imports = new LinkedList<String>();
   
    // Add current package to import for resolution (but do not forget to remove it before writing the effective imports)
    imports.add(informerInfos.classPackage);
    imports.add("java.lang");
    for (ImportDeclaration d : baseImports) {
      imports.add(d.getName().toString());
    }

    // create the type declaration
    ClassOrInterfaceDeclaration type = new ClassOrInterfaceDeclaration(ModifierSet.PUBLIC, true, informerInfos.getInformerName());
    type.setExtends(Arrays.asList(new ClassOrInterfaceType(Informer.class.getSimpleName() + "<" + informerInfos.className + ">")));
    type.setJavaDoc(new JavadocComment("\n" + "Informer for {@link " + informerInfos.className + "}\n" + "@author InformerMojos\n"));
    ASTHelper.addTypeDeclaration(cu, type);
    for (PropertyInfos infos : informerInfos.properties) {
      // create a method
      MethodDeclaration method = new MethodDeclaration(ModifierSet.PUBLIC, ASTHelper.VOID_TYPE, "get" + Utils.uppercaseFirst(infos.name));
      String informerTypeFor = InformerTypeFinder.getInformerTypeFor(resolvedInformers, qualifiedEnums, imports, infos);
      method.setType(new ClassOrInterfaceType(informerTypeFor));
      method.setJavaDoc(new JavadocComment(infos.generateJavadoc(informerInfos, informerTypeFor)));
      ASTHelper.addMember(type, method);
    }
   
    imports.remove(informerInfos.classPackage);
    imports.remove("java.lang");

    baseImports = new LinkedList<ImportDeclaration>();
    for(String name : imports) {
      baseImports.add(new ImportDeclaration(ASTHelper.createNameExpr(name), false, false));
    }
    baseImports.add(new ImportDeclaration(ASTHelper.createNameExpr(Informer.class.getCanonicalName()), false, false));
    baseImports.add(new ImportDeclaration(ASTHelper.createNameExpr(ObjectFieldInformer.class.getPackage().getName()), false, true));
    cu.setImports(baseImports);
    return cu;
  }
View Full Code Here

    if(enums!=null) {
      returned.addAll(Arrays.asList(enums));
    }
    for (File f : sources) {
      try {
        CompilationUnit searched = JavaParser.parse(f);
        EnumScanner enums = new EnumScanner();
        enums.visit(searched, null);
        if (enums.getQualifiedName()!=null) {
          returned.add(enums.getQualifiedName());
        }
View Full Code Here

   * @throws IOException
   */
  public void createInformerFileFrom(InformerInfos informerInfos, File baseOutput, Collection<String> qualifiedEnums, Map<String, Class> resolvedInformers) throws IOException {
    getLog().info("generating informer for "+informerInfos.classPackage+"."+informerInfos.className);
    getLog().debug("it should have as FieldInformers "+informerInfos.properties);
    CompilationUnit cu = InformerTextGenerator.generateCompilationUnit(informerInfos, qualifiedEnums, resolvedInformers);
    File effective = new File(baseOutput+"/"+informerInfos.classPackage.replace('.', '/')+"/"+informerInfos.getInformerName()+".java");
    effective.getParentFile().mkdirs();
    FileUtils.fileWrite(effective, cu.toString());
    if(generateMappings) {
      currentInformerMappings.setProperty(informerInfos.getQualifiedClassName(), informerInfos.getQualifiedInformerName());
    }
  }
View Full Code Here

TOP

Related Classes of japa.parser.ast.CompilationUnit

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.