Search in sources :

Example 1 with JavaCharStream

use of net.sourceforge.pmd.lang.ast.JavaCharStream in project pmd by pmd.

the class AbstractJspNodesTst method getNodes.

/**
 * Run the JSP parser on the source, and return the nodes of type clazz.
 *
 * @param clazz
 * @param source
 * @return Set
 */
public <T extends JspNode> Set<T> getNodes(Class<T> clazz, String source) {
    JspParser parser = new JspParser(new JavaCharStream(new StringReader(source)));
    Node rootNode = parser.CompilationUnit();
    Set<T> nodes = new HashSet<>();
    addNodeAndSubnodes(rootNode, nodes, clazz);
    return nodes;
}
Also used : Node(net.sourceforge.pmd.lang.ast.Node) StringReader(java.io.StringReader) JavaCharStream(net.sourceforge.pmd.lang.ast.JavaCharStream) HashSet(java.util.HashSet)

Example 2 with JavaCharStream

use of net.sourceforge.pmd.lang.ast.JavaCharStream in project pmd by pmd.

the class AbstractVfNodesTest method getNodes.

/**
 * Run the VF parser on the source, and return the nodes of type clazz.
 *
 * @param clazz
 * @param source
 * @return Set
 */
public <T extends VfNode> Set<T> getNodes(Class<T> clazz, String source) {
    VfParser parser = new VfParser(new JavaCharStream(new StringReader(source)));
    Node rootNode = parser.CompilationUnit();
    Set<T> nodes = new HashSet<>();
    addNodeAndSubnodes(rootNode, nodes, clazz);
    return nodes;
}
Also used : Node(net.sourceforge.pmd.lang.ast.Node) StringReader(java.io.StringReader) JavaCharStream(net.sourceforge.pmd.lang.ast.JavaCharStream) HashSet(java.util.HashSet)

Example 3 with JavaCharStream

use of net.sourceforge.pmd.lang.ast.JavaCharStream in project pmd by pmd.

the class AbstractJavaParser method createJavaParser.

/**
 * Subclass should override this method to modify the JavaParser as needed.
 */
protected JavaParser createJavaParser(Reader source) throws ParseException {
    parser = new JavaParser(new JavaCharStream(source));
    String suppressMarker = getParserOptions().getSuppressMarker();
    if (suppressMarker != null) {
        parser.setSuppressMarker(suppressMarker);
    }
    return parser;
}
Also used : JavaParser(net.sourceforge.pmd.lang.java.ast.JavaParser) JavaCharStream(net.sourceforge.pmd.lang.ast.JavaCharStream)

Example 4 with JavaCharStream

use of net.sourceforge.pmd.lang.ast.JavaCharStream in project pmd-eclipse-plugin by pmd.

the class PMDGenerateASTAction method generateAST.

/**
 * Generate a AST for a file
 *
 * @param file
 *            a file
 */
private void generateAST(IFile file) {
    LOG.info("Generating AST for file " + file.getName());
    ByteArrayOutputStream byteArrayOutputStream = null;
    ByteArrayInputStream astInputStream = null;
    try {
        JavaParser parser = new JavaParser(new JavaCharStream(file.getContents()));
        parser.setJdkVersion(Integer.MAX_VALUE);
        ASTCompilationUnit compilationUnit = parser.CompilationUnit();
        byteArrayOutputStream = new ByteArrayOutputStream();
        IAstWriter astWriter = PMDPlugin.getDefault().getAstWriter();
        astWriter.write(byteArrayOutputStream, compilationUnit);
        byteArrayOutputStream.flush();
        IFile astFile = createASTFile(file);
        if (astFile != null) {
            if (astFile.exists()) {
                astFile.delete(false, null);
            }
            astInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
            astFile.create(astInputStream, false, null);
        }
    } catch (CoreException e) {
        showErrorById(StringKeys.ERROR_CORE_EXCEPTION, e);
    } catch (ParseException e) {
        showErrorById(StringKeys.ERROR_PMD_EXCEPTION, e);
    } catch (WriterException e) {
        showErrorById(StringKeys.ERROR_PMD_EXCEPTION, e);
    } catch (IOException e) {
        showErrorById(StringKeys.ERROR_IO_EXCEPTION, e);
    } finally {
        IOUtil.closeQuietly(byteArrayOutputStream);
        IOUtil.closeQuietly(astInputStream);
    }
}
Also used : JavaParser(net.sourceforge.pmd.lang.java.ast.JavaParser) IFile(org.eclipse.core.resources.IFile) ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) CoreException(org.eclipse.core.runtime.CoreException) ByteArrayInputStream(java.io.ByteArrayInputStream) IAstWriter(net.sourceforge.pmd.eclipse.runtime.writer.IAstWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ParseException(net.sourceforge.pmd.lang.java.ast.ParseException) IOException(java.io.IOException) JavaCharStream(net.sourceforge.pmd.lang.ast.JavaCharStream) WriterException(net.sourceforge.pmd.eclipse.runtime.writer.WriterException)

Aggregations

JavaCharStream (net.sourceforge.pmd.lang.ast.JavaCharStream)4 StringReader (java.io.StringReader)2 HashSet (java.util.HashSet)2 Node (net.sourceforge.pmd.lang.ast.Node)2 JavaParser (net.sourceforge.pmd.lang.java.ast.JavaParser)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 IAstWriter (net.sourceforge.pmd.eclipse.runtime.writer.IAstWriter)1 WriterException (net.sourceforge.pmd.eclipse.runtime.writer.WriterException)1 ASTCompilationUnit (net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit)1 ParseException (net.sourceforge.pmd.lang.java.ast.ParseException)1 IFile (org.eclipse.core.resources.IFile)1 CoreException (org.eclipse.core.runtime.CoreException)1