Java Parser for JavaScript/TypeScript, based on antlr4ts, grammar taken from antlr4's java grammar too (so please report bugs and open pull requests related to grammars upstream)
import { parse, createVisitor } from 'java-ast';
const countMethods = (source: string) => {
let ast = parse(source);
return createVisitor({
visitMethodDeclaration: () => 1,
defaultResult: () => 0,
aggregateResult: (a, b) => a + b,
}).visit(ast);
};
console.log(
countMethods(`
class A {
int a;
void b() {}
void c() {}
}
class B {
void z() {}
}
`),
); // logs 3
This project was originally created by Uri Shaked.
- java-model - Provides high-level access to the Java type model.