Search in sources :

Example 1 with ParseProblemException

use of com.github.javaparser.ParseProblemException in project vue-gwt by Axellience.

the class TemplateParser method processJavaExpression.

/**
 * Process the given string as a Java expression.
 * @param expressionString A valid Java expression
 * @return A processed expression, should be placed in the HTML in place of the original
 * expression
 */
private TemplateExpression processJavaExpression(String expressionString) {
    Expression expression;
    try {
        expression = JavaParser.parseExpression(expressionString);
    } catch (ParseProblemException parseException) {
        logger.error("Couldn't parse Expression, make sure it is valid Java.", expressionString);
        throw parseException;
    }
    resolveTypesUsingImports(expression);
    resolveStaticMethodsUsingImports(expression);
    checkMethodNames(expression);
    // Find the parameters used by the expression
    List<VariableInfo> expressionParameters = new LinkedList<>();
    findExpressionParameters(expression, expressionParameters);
    // If there is a cast first, we use this as the type of our expression
    if (currentProp == null)
        expression = getTypeFromCast(expression);
    // Update the expression as it might have been changed
    expressionString = expression.toString();
    // Add the resulting expression to our result
    return result.addExpression(expressionString, currentExpressionReturnType, currentProp == null, expressionParameters);
}
Also used : TemplateExpression(com.axellience.vuegwt.processors.component.template.parser.result.TemplateExpression) Expression(com.github.javaparser.ast.expr.Expression) VariableInfo(com.axellience.vuegwt.processors.component.template.parser.variable.VariableInfo) LocalVariableInfo(com.axellience.vuegwt.processors.component.template.parser.variable.LocalVariableInfo) LinkedList(java.util.LinkedList) ParseProblemException(com.github.javaparser.ParseProblemException)

Example 2 with ParseProblemException

use of com.github.javaparser.ParseProblemException in project structr by structr.

the class ParseJavaFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    try {
        if (!(arrayHasLengthAndAllElementsNotNull(sources, 1) && sources[0] instanceof String)) {
            return null;
        }
        try {
            final SecurityContext securityContext = ctx.getSecurityContext();
            final App app = StructrApp.getInstance(securityContext);
            // Parse string as Java code
            final String resultJson = new GsonBuilder().setPrettyPrinting().create().toJson(new JavaParserModule(app).parse((String) sources[0]).get());
            return resultJson;
        } catch (final ParseProblemException ex) {
            logException(caller, ex, sources);
        }
    } catch (final IllegalArgumentException e) {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
        return usage(ctx.isJavaScriptContext());
    }
    return "";
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) GsonBuilder(com.google.gson.GsonBuilder) SecurityContext(org.structr.common.SecurityContext) ParseProblemException(com.github.javaparser.ParseProblemException)

Example 3 with ParseProblemException

use of com.github.javaparser.ParseProblemException in project structr by structr.

the class StructrJavaTypeSolver method parse.

private void parse(final Folder folder) {
    for (final File file : folder.getFiles()) {
        final String fileName = file.getName();
        if (StringUtils.isNotEmpty(fileName) && fileName.endsWith(".java") && !(fileName.endsWith("ackage-info.java"))) {
            final String fileContent = file.getFavoriteContent();
            try {
                final CompilationUnit cu = JavaParser.parse(fileContent);
                index.add(cu);
                cuCount++;
            } catch (final ParseProblemException ex) {
                logger.warn("Couldn't parse " + fileName, ex);
            }
        }
    }
    folder.getFolders().forEach((subfolder) -> {
        parse(subfolder);
    });
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) File(org.structr.web.entity.File) ParseProblemException(com.github.javaparser.ParseProblemException)

Example 4 with ParseProblemException

use of com.github.javaparser.ParseProblemException in project checker-framework by typetools.

the class ToIndexFileConverter method convert.

/**
 * Augment given scene with information from stubfile, reading stubs from input stream and
 * writing JAIF to output stream.
 *
 * @param scene the initial scene
 * @param in stubfile contents
 * @param out JAIF representing augmented scene
 * @throws ParseException
 * @throws DefException
 */
private static void convert(AScene scene, InputStream in, OutputStream out) throws IOException, DefException, ParseException {
    StubUnit iu;
    try {
        iu = JavaParser.parseStubUnit(in);
    } catch (ParseProblemException e) {
        iu = null;
        ErrorReporter.errorAbort("ToIndexFileConverter: exception from JavaParser.parseStubUnit for InputStream." + System.lineSeparator() + "Problem message with problems encountered: " + e.getMessage());
    }
    extractScene(iu, scene);
    try (Writer w = new BufferedWriter(new OutputStreamWriter(out))) {
        IndexFileWriter.write(scene, w);
    }
}
Also used : StubUnit(com.github.javaparser.ast.StubUnit) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) IndexFileWriter(scenelib.annotations.io.IndexFileWriter) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter) ParseProblemException(com.github.javaparser.ParseProblemException) BufferedWriter(java.io.BufferedWriter)

Example 5 with ParseProblemException

use of com.github.javaparser.ParseProblemException in project javaparser by javaparser.

the class SourceRoot method parse.

/**
 * Parses a .java files under the source root and returns its CompilationUnit. It keeps track of the parsed file so
 * you can write it out with the saveAll() call. Note that the cache grows with every file parsed, so if you don't
 * need saveAll(), or you don't ask SourceRoot to parse files multiple times (where the cache is useful) you might
 * want to use the parse method with a callback.
 *
 * @param startPackage files in this package and deeper are parsed. Pass "" to parse all files.
 * @throws ParseProblemException when something went wrong.
 */
public CompilationUnit parse(String startPackage, String filename) {
    assertNotNull(startPackage);
    assertNotNull(filename);
    try {
        final ParseResult<CompilationUnit> result = tryToParse(startPackage, filename);
        if (result.isSuccessful()) {
            return result.getResult().get();
        }
        throw new ParseProblemException(result.getProblems());
    } catch (IOException e) {
        throw new ParseProblemException(e);
    }
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) IOException(java.io.IOException) ParseProblemException(com.github.javaparser.ParseProblemException)

Aggregations

ParseProblemException (com.github.javaparser.ParseProblemException)7 CompilationUnit (com.github.javaparser.ast.CompilationUnit)3 SecurityContext (org.structr.common.SecurityContext)2 App (org.structr.core.app.App)2 StructrApp (org.structr.core.app.StructrApp)2 TemplateExpression (com.axellience.vuegwt.processors.component.template.parser.result.TemplateExpression)1 LocalVariableInfo (com.axellience.vuegwt.processors.component.template.parser.variable.LocalVariableInfo)1 VariableInfo (com.axellience.vuegwt.processors.component.template.parser.variable.VariableInfo)1 StubUnit (com.github.javaparser.ast.StubUnit)1 JavadocComment (com.github.javaparser.ast.comments.JavadocComment)1 Expression (com.github.javaparser.ast.expr.Expression)1 GsonBuilder (com.google.gson.GsonBuilder)1 BufferedWriter (java.io.BufferedWriter)1 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Writer (java.io.Writer)1 LinkedList (java.util.LinkedList)1 File (org.structr.web.entity.File)1 IndexFileWriter (scenelib.annotations.io.IndexFileWriter)1