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 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"));
    List<AnnotationExpr> annotations = new LinkedList<AnnotationExpr>();
    // Constructing generated annotation value
    List<MemberValuePair> parameters = new LinkedList<MemberValuePair>();
    parameters.add(new MemberValuePair("date", new StringLiteralExpr(
            javax.xml.bind.DatatypeConverter.printDateTime(GregorianCalendar.getInstance()))));
    parameters.add(new MemberValuePair("comments", new StringLiteralExpr("generated by gaedo-informer-generator")));
    List<Expression> values = new LinkedList<Expression>();
    values.add(new StringLiteralExpr(informerInfos.getQualifiedClassName()));
    parameters.add(new MemberValuePair("value",
            new ArrayInitializerExpr(values)));
    NormalAnnotationExpr generated = new NormalAnnotationExpr(ASTHelper.createNameExpr(Generated.class.getName()), parameters);
    type.setAnnotations(annotations);
    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

  public static CompilationUnit parse(String src) throws ParseException {
    ByteArrayInputStream in;
    try {
      in = new ByteArrayInputStream(src.getBytes(UTF_8));
      CompilationUnit cu;
      cu = JavaParser.parse(in, UTF_8);
      return cu;
    } catch (UnsupportedEncodingException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
View Full Code Here

    return null;
  }

  public static boolean isValid(String src) {
    try {
      CompilationUnit cu = parse(src);
      return true;
    } catch (ParseException e) {
      String m = e.getMessage() + "\n";
      System.out.println(m.substring(0, m.indexOf('\n')));
      return false;
View Full Code Here

    // make it tolerant of lowercase default
    line = line.replace("@default(", "@Default(");
    String cl = String.format(classTempForParams, line);
    try {
      CompilationUnit cu = parse(cl);
      VoidVisitorAdapter visitor = new VoidVisitorAdapter() {
        @Override
        public void visit(Parameter p, Object arg) {
          ret.add(p);
        }
      };
      cu.accept(visitor, null);
    } catch (ParseException e) {
      throw new RuntimeException(
          "the line does not seem to be a valid param list declaration: "
              + line);
    }
View Full Code Here

    line = line.trim();
    if (line.startsWith("(")){
      // perhaps it's in the form of (...)
      String cl = String.format(classTempForArgsNoParenthesis, line);
      try {
        CompilationUnit cu = parse(cl);
        cu.accept(visitor, null);
        return ret;
      } catch (ParseException e) {
        // perhaps not really (...). fall through
      }
    }
   
    String cl = String.format(classTempForArgs, line);
    try {
      CompilationUnit cu = parse(cl);
      cu.accept(visitor, null);
    } catch (ParseException e) {
      throw new RuntimeException(
          "the line does not seem to be a valid arg list: " + line);
    }
    return ret;
View Full Code Here

//    }

    String cl = String.format(classTempForArgs, line);
    final String finalLine = line;
    try {
      CompilationUnit cu = parse(cl);
      VoidVisitorAdapter visitor = new VoidVisitorAdapter() {
        boolean hasNamed = false;
        boolean hasUnNamed = false;

        @Override
        public void visit(MethodCallExpr n, Object arg) {
          List<Expression> args = n.getArgs();
          // api issue: args can be null in case of empty arg list
          if (args != null)
            for (Expression expr : args) {
              if (expr instanceof AssignExpr) {
                if (hasUnNamed)
                  throw new RuntimeException(
                      "the line has mixed named and un-named arg list. It's not valid in Japid tag invocation. It must be all-or-none.: "
                          + finalLine);
                hasNamed = true;
                AssignExpr ae = (AssignExpr) expr;
                NamedArg na = new NamedArg(ae.getTarget(),
                    ae.getValue());
                ret.add(na);
              } else {
                if (hasNamed)
                  throw new RuntimeException(
                      "the line has mixed named and un-named arg list. It's not valid in Japid tag invocation. It must be all-or-none.: "
                          + finalLine);
                hasUnNamed = true;
              }
            }
        }
      };
      cu.accept(visitor, null);
    } catch (ParseException e) {
      throw new RuntimeException(
          "the line does not seem to be a valid arg list: " + line
              + ". ");
    }
View Full Code Here

    return ret;
  }

  public static boolean hasMethod(String javaSource, String string)
      throws ParseException {
    CompilationUnit cu = parse(javaSource);
    return hasMethod(cu, string);
  }
View Full Code Here

   */
  public static void isValidMethDecl(String line) {
    final String classTempForMeth = "class T {  %s{} }";
    String classString = String.format(classTempForMeth, line);
    try {
      CompilationUnit cu = parse(classString);
      VoidVisitorAdapter visitor = new VoidVisitorAdapter() {
      };
      cu.accept(visitor, null);
    } catch (ParseException e) {
      throw new RuntimeException(
          "the line does not seem to be a valid method declaration: "
              + line + ". Was expecting something like foo(int a, String b).");
    }
View Full Code Here

  }

  public static boolean isValidSingleVarDecl(String src) {
    String classString = String.format(varDeclTemp, src);
    try {
      CompilationUnit cu = parse(classString);
      return true;
    } catch (ParseException e) {
      return false;
    }
  }
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.