Search in sources :

Example 16 with CompilationUnit

use of japa.parser.ast.CompilationUnit in project gwt-test-utils by gwt-test-utils.

the class JavaFileParanamer method visitJavaFile.

private void visitJavaFile(Class<?> declaringClass) throws ParseException, IOException {
    InputStream is = getJavaFile(declaringClass);
    if (is == null) {
        return;
    }
    CompilationUnit cu;
    try {
        // parse the file
        cu = JavaParser.parse(is);
    } finally {
        is.close();
    }
    MethodParametersVisitor visitor = new MethodParametersVisitor();
    cu.accept(visitor, cache);
}
Also used : CompilationUnit(japa.parser.ast.CompilationUnit) InputStream(java.io.InputStream)

Example 17 with CompilationUnit

use of japa.parser.ast.CompilationUnit in project gwt-test-utils by gwt-test-utils.

the class JavaFileParanamer method visitJavaFileToPopulateCache.

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
            }
        }
    }
}
Also used : CompilationUnit(japa.parser.ast.CompilationUnit) InputStream(java.io.InputStream) IOException(java.io.IOException) IOException(java.io.IOException)

Example 18 with CompilationUnit

use of japa.parser.ast.CompilationUnit in project japid42 by branaway.

the class JavaSyntaxTool method isValidMethDecl.

/**
	 * verify that line is a valid method declaration part, excluding method body and the {} 
	 * @param line: something like foo(int a, String b)
	 */
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).");
    }
}
Also used : CompilationUnit(japa.parser.ast.CompilationUnit) ParseException(japa.parser.ParseException) VoidVisitorAdapter(japa.parser.ast.visitor.VoidVisitorAdapter)

Example 19 with CompilationUnit

use of japa.parser.ast.CompilationUnit in project japid42 by branaway.

the class JavaSyntaxTool method parseNamedArgs.

/**
	 * 
	 * @param line
	 * @return list of named args if all the args are named; empty list if none
	 *         is named; or an exception is thrown if the arg list is not valid
	 *         or named and un-named are mixed
	 * 
	 */
public static List<NamedArg> parseNamedArgs(String line) {
    final List<NamedArg> ret = new ArrayList<NamedArg>();
    if (line == null || line.trim().length() == 0)
        return ret;
    line = line.trim();
    //		if (line.startsWith("(")) {
    //			if (line.endsWith(")"))
    //				line = line.substring(1, line.length() - 1);
    //			else
    //				throw new RuntimeException("no closing ')' in arg expression: "
    //						+ line);
    //		}
    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 + ". ");
    }
    return ret;
}
Also used : CompilationUnit(japa.parser.ast.CompilationUnit) ArrayList(java.util.ArrayList) AssignExpr(japa.parser.ast.expr.AssignExpr) Expression(japa.parser.ast.expr.Expression) ParseException(japa.parser.ParseException) VoidVisitorAdapter(japa.parser.ast.visitor.VoidVisitorAdapter) MethodCallExpr(japa.parser.ast.expr.MethodCallExpr)

Example 20 with CompilationUnit

use of japa.parser.ast.CompilationUnit in project japid42 by branaway.

the class JavaSyntaxTool method isOpenIf.

public static boolean isOpenIf(String part) {
    part = part.trim();
    if (part.endsWith("{"))
        part = part.substring(0, part.length() - 1);
    String classString = String.format(IF_PREDICATE_OPEN, part);
    try {
        CompilationUnit cu = parse(classString);
        return true;
    } catch (ParseException e) {
        return false;
    }
}
Also used : CompilationUnit(japa.parser.ast.CompilationUnit) ParseException(japa.parser.ParseException)

Aggregations

CompilationUnit (japa.parser.ast.CompilationUnit)34 Test (org.junit.Test)17 ParseException (japa.parser.ParseException)15 JapidAbstractCompiler (cn.bran.japid.compiler.JapidAbstractCompiler)12 JapidTemplate (cn.bran.japid.template.JapidTemplate)12 VoidVisitorAdapter (japa.parser.ast.visitor.VoidVisitorAdapter)10 JapidTemplateCompiler (cn.bran.japid.compiler.JapidTemplateCompiler)9 MethodCallExpr (japa.parser.ast.expr.MethodCallExpr)7 ArrayList (java.util.ArrayList)6 Parameter (japa.parser.ast.body.Parameter)5 TypeParameter (japa.parser.ast.TypeParameter)4 AssignExpr (japa.parser.ast.expr.AssignExpr)4 Expression (japa.parser.ast.expr.Expression)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 JapidLayoutCompiler (cn.bran.japid.compiler.JapidLayoutCompiler)3 PackageDeclaration (japa.parser.ast.PackageDeclaration)3 ClassOrInterfaceDeclaration (japa.parser.ast.body.ClassOrInterfaceDeclaration)3 MethodDeclaration (japa.parser.ast.body.MethodDeclaration)3 TokenMgrError (japa.parser.TokenMgrError)2 BlockComment (japa.parser.ast.BlockComment)2