abstract syntax tree - How to check if given line of code is written in java? -


what right way check if given line java code?

input: logsupport.java:44 com/sun/activation/registries/logsupport log (ljava/lang/string;)v

expected output: false.

input: scanner in = new scanner(system.in);

expected output: true.

i tried eclipse jdt astparser check if can create ast. here's code:

public static boolean isjava(string line) {     boolean isjava = false;     astparser parser = astparser.newparser(ast.jls3);     parser.setsource(line.tochararray());     parser.setresolvebindings(false);     astnode node = null;      parser.setkind(astparser.k_statements);     try {         node = parser.createast(null);         if (node == null) return false;         isjava = true;     } catch (exception e) {         return false;     }     return isjava; } 

but not work. ideas? thanks!

try beanshell

http://www.beanshell.org/intro.html

java evaluation features:

evaluate full java source classes dynamically isolated java methods, statements, , expressions.

summary of features

dynamic execution of full java syntax, java code fragments, loosely typed java , additional scripting conveniences.

transparent access java objects , apis.

runs in 4 modes: command line, console, applet, remote session server. can work in security constrained environments without classloader or bytecode generation features.

the interpreter small ~150k jar file.

pure java.

it's free!!

the link below has other option try

syntax checking in java


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -