本文整理汇总了Java中com.sun.tools.javac.parser.Parser类的典型用法代码示例。如果您正苦于以下问题:Java Parser类的具体用法?Java Parser怎么用?Java Parser使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Parser类属于com.sun.tools.javac.parser包,在下文中一共展示了Parser类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: parseType
import com.sun.tools.javac.parser.Parser; //导入依赖的package包/类
/**
* For internal use only. This method will be
* removed without warning.
*/
public Type parseType(String expr, TypeElement scope) {
if (expr == null || expr.equals(""))
throw new IllegalArgumentException();
compiler = JavaCompiler.instance(context);
JavaFileObject prev = compiler.log.useSource(null);
ParserFactory parserFactory = ParserFactory.instance(context);
Attr attr = Attr.instance(context);
try {
CharBuffer buf = CharBuffer.wrap((expr+"\u0000").toCharArray(), 0, expr.length());
Parser parser = parserFactory.newParser(buf, false, false, false);
JCTree tree = parser.parseType();
return attr.attribType(tree, (TypeSymbol)scope);
} finally {
compiler.log.useSource(prev);
}
}
示例2: parse
import com.sun.tools.javac.parser.Parser; //导入依赖的package包/类
@Nullable
public JCTree.JCCompilationUnit parse(final String src) {
if (!canParse) return null;
long time = System.currentTimeMillis();
SimpleJavaFileObject source = new SimpleJavaFileObject(URI.create("source"), JavaFileObject.Kind.SOURCE) {
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
return src;
}
};
Log.instance(context).useSource(source);
Parser parser = parserFactory.newParser(src,
/*keepDocComments=*/ true,
/*keepEndPos=*/ true,
/*keepLineMap=*/ true);
JCTree.JCCompilationUnit unit;
unit = parser.parseCompilationUnit();
unit.sourcefile = source;
android.util.Log.d(TAG, "parse: time " + (System.currentTimeMillis() - time) + " ms");
return unit;
}
示例3: parseType
import com.sun.tools.javac.parser.Parser; //导入依赖的package包/类
/**
* For internal use only. This method will be
* removed without warning.
*/
public Type parseType(String expr, TypeElement scope) {
if (expr == null || expr.equals(""))
throw new IllegalArgumentException();
compiler = JavaCompiler.instance(context);
JavaFileObject prev = compiler.log.useSource(null);
ParserFactory parserFactory = ParserFactory.instance(context);
Attr attr = Attr.instance(context);
try {
CharBuffer buf = CharBuffer.wrap((expr+"\u0000").toCharArray(), 0, expr.length());
Parser parser = parserFactory.newParser(buf, false, false, false);
JCTree tree = parser.parseType();
return attr.attribType(tree, (Symbol.TypeSymbol)scope);
} finally {
compiler.log.useSource(prev);
}
}
示例4: parseType
import com.sun.tools.javac.parser.Parser; //导入依赖的package包/类
/**
* For internal use only. This method will be
* removed without warning.
* @param expr the type expression to be analyzed
* @param scope the scope in which to analyze the type expression
* @return the type
* @throws IllegalArgumentException if the type expression of null or empty
*/
public Type parseType(String expr, TypeElement scope) {
if (expr == null || expr.equals(""))
throw new IllegalArgumentException();
compiler = JavaCompiler.instance(context);
JavaFileObject prev = compiler.log.useSource(null);
ParserFactory parserFactory = ParserFactory.instance(context);
Attr attr = Attr.instance(context);
try {
CharBuffer buf = CharBuffer.wrap((expr+"\u0000").toCharArray(), 0, expr.length());
Parser parser = parserFactory.newParser(buf, false, false, false);
JCTree tree = parser.parseType();
return attr.attribType(tree, (Symbol.TypeSymbol)scope);
} finally {
compiler.log.useSource(prev);
}
}
示例5: parseType
import com.sun.tools.javac.parser.Parser; //导入依赖的package包/类
/**
* For internal use by Sun Microsystems only. This method will be
* removed without warning.
*/
public Type parseType(String expr, TypeElement scope) {
if (expr == null || expr.equals(""))
throw new IllegalArgumentException();
compiler = JavaCompiler.instance(context);
JavaFileObject prev = compiler.log.useSource(null);
Scanner.Factory scannerFactory = Scanner.Factory.instance(context);
Parser.Factory parserFactory = Parser.Factory.instance(context);
Attr attr = Attr.instance(context);
try {
Scanner scanner = scannerFactory.newScanner((expr+"\u0000").toCharArray(),
expr.length());
Parser parser = parserFactory.newParser(scanner, false, false);
JCTree tree = parser.type();
return attr.attribType(tree, (Symbol.TypeSymbol)scope);
} finally {
compiler.log.useSource(prev);
}
}
示例6: parseType
import com.sun.tools.javac.parser.Parser; //导入依赖的package包/类
/**
* For internal use only. This method will be
* removed without warning.
*/
public Type parseType(String expr, TypeElement scope) {
if (expr == null || expr.equals(""))
throw new IllegalArgumentException();
compiler = JavaCompiler.instance(context);
JavaFileObject prev = compiler.log.useSource(null);
Scanner.Factory scannerFactory = Scanner.Factory.instance(context);
Parser.Factory parserFactory = Parser.Factory.instance(context);
Attr attr = Attr.instance(context);
try {
Scanner scanner = scannerFactory.newScanner((expr+"\u0000").toCharArray(),
expr.length());
Parser parser = parserFactory.newParser(scanner, false, false);
JCTree tree = parser.type();
return attr.attribType(tree, (Symbol.TypeSymbol)scope);
} finally {
compiler.log.useSource(prev);
}
}
示例7: doParse
import com.sun.tools.javac.parser.Parser; //导入依赖的package包/类
private static <T extends Tree> T doParse(JavacTaskImpl task, String text, SourcePositions[] sourcePositions, int offset, Function<Parser, T> actualParse) {
if (text == null || (sourcePositions != null && sourcePositions.length != 1))
throw new IllegalArgumentException();
//use a working init order:
com.sun.tools.javac.main.JavaCompiler.instance(task.getContext());
com.sun.tools.javac.tree.TreeMaker jcMaker = com.sun.tools.javac.tree.TreeMaker.instance(task.getContext());
int oldPos = jcMaker.pos;
try {
//from org/netbeans/modules/java/hints/spiimpl/Utilities.java:
Context context = task.getContext();
JavaCompiler compiler = JavaCompiler.instance(context);
JavaFileObject prev = compiler.log.useSource(new DummyJFO());
Log.DiagnosticHandler discardHandler = new Log.DiscardDiagnosticHandler(compiler.log) {
@Override
public void report(JCDiagnostic diag) {
//ignore:
}
};
try {
CharBuffer buf = CharBuffer.wrap((text+"\u0000").toCharArray(), 0, text.length());
ParserFactory factory = ParserFactory.instance(context);
Parser parser = factory.newParser(buf, false, true, false, false);
if (parser instanceof JavacParser) {
if (sourcePositions != null)
sourcePositions[0] = new ParserSourcePositions((JavacParser)parser, offset);
return actualParse.apply(parser);
}
return null;
} finally {
compiler.log.useSource(prev);
compiler.log.popDiagnosticHandler(discardHandler);
}
} finally {
jcMaker.pos = oldPos;
}
}
示例8: parseStatement
import com.sun.tools.javac.parser.Parser; //导入依赖的package包/类
private static JCStatement parseStatement(Context context, CharSequence stmt, SourcePositions[] pos, final List<Diagnostic<? extends JavaFileObject>> errors) {
if (stmt == null || (pos != null && pos.length != 1))
throw new IllegalArgumentException();
JavaCompiler compiler = JavaCompiler.instance(context);
JavaFileObject prev = compiler.log.useSource(new DummyJFO());
Log.DiagnosticHandler discardHandler = new Log.DiscardDiagnosticHandler(compiler.log) {
@Override
public void report(JCDiagnostic diag) {
errors.add(diag);
}
};
try {
CharBuffer buf = CharBuffer.wrap((stmt+"\u0000").toCharArray(), 0, stmt.length());
ParserFactory factory = ParserFactory.instance(context);
ScannerFactory scannerFactory = ScannerFactory.instance(context);
Names names = Names.instance(context);
Parser parser = new JackpotJavacParser(context, (NBParserFactory) factory, scannerFactory.newScanner(buf, false), false, false, CancelService.instance(context), names);
if (parser instanceof JavacParser) {
if (pos != null)
pos[0] = new ParserSourcePositions((JavacParser)parser);
return parser.parseStatement();
}
return null;
} finally {
compiler.log.useSource(prev);
compiler.log.popDiagnosticHandler(discardHandler);
}
}
示例9: parseExpression
import com.sun.tools.javac.parser.Parser; //导入依赖的package包/类
private static JCExpression parseExpression(Context context, CharSequence expr, boolean onlyFullInput, SourcePositions[] pos, final List<Diagnostic<? extends JavaFileObject>> errors) {
if (expr == null || (pos != null && pos.length != 1))
throw new IllegalArgumentException();
JavaCompiler compiler = JavaCompiler.instance(context);
JavaFileObject prev = compiler.log.useSource(new DummyJFO());
Log.DiagnosticHandler discardHandler = new Log.DiscardDiagnosticHandler(compiler.log) {
@Override
public void report(JCDiagnostic diag) {
errors.add(diag);
}
};
try {
CharBuffer buf = CharBuffer.wrap((expr+"\u0000").toCharArray(), 0, expr.length());
ParserFactory factory = ParserFactory.instance(context);
ScannerFactory scannerFactory = ScannerFactory.instance(context);
Names names = Names.instance(context);
Scanner scanner = scannerFactory.newScanner(buf, false);
Parser parser = new JackpotJavacParser(context, (NBParserFactory) factory, scanner, false, false, CancelService.instance(context), names);
if (parser instanceof JavacParser) {
if (pos != null)
pos[0] = new ParserSourcePositions((JavacParser)parser);
JCExpression result = parser.parseExpression();
if (!onlyFullInput || scanner.token().kind == TokenKind.EOF) {
return result;
}
}
return null;
} finally {
compiler.log.useSource(prev);
compiler.log.popDiagnosticHandler(discardHandler);
}
}
示例10: test1
import com.sun.tools.javac.parser.Parser; //导入依赖的package包/类
public void test1() {
Context context = new Context();
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
context.put(DiagnosticListener.class, diagnostics);
Options.instance(context).put("allowStringFolding", "false");
JCTree.JCCompilationUnit unit;
JavacFileManager fileManager = new JavacFileManager(context, true, UTF_8);
try {
fileManager.setLocation(StandardLocation.PLATFORM_CLASS_PATH, ImmutableList.<File>of());
} catch (IOException e) {
// impossible
throw new IOError(e);
}
SimpleJavaFileObject source =
new SimpleJavaFileObject(URI.create("source"), JavaFileObject.Kind.SOURCE) {
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
return src;
}
};
Log.instance(context).useSource(source);
ParserFactory parserFactory = ParserFactory.instance(context);
Parser parser =
parserFactory.newParser(
src,
/*keepDocComments=*/ true,
/*keepEndPos=*/ true,
/*keepLineMap=*/ true);
unit = parser.parseCompilationUnit();
unit.sourcefile = source;
}
示例11: test4
import com.sun.tools.javac.parser.Parser; //导入依赖的package包/类
public void test4() {
Context context = new Context();
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
context.put(DiagnosticListener.class, diagnostics);
Options.instance(context).put("allowStringFolding", "false");
JCTree.JCCompilationUnit unit;
JavacFileManager fileManager = new JavacFileManager(context, true, UTF_8);
try {
fileManager.setLocation(StandardLocation.PLATFORM_CLASS_PATH, ImmutableList.<File>of());
} catch (IOException e) {
// impossible
throw new IOError(e);
}
SimpleJavaFileObject source =
new SimpleJavaFileObject(URI.create("source"), JavaFileObject.Kind.SOURCE) {
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
return src;
}
};
Log.instance(context).useSource(source);
ParserFactory parserFactory = ParserFactory.instance(context);
Parser parser =
parserFactory.newParser(
src,
/*keepDocComments=*/ true,
/*keepEndPos=*/ true,
/*keepLineMap=*/ true);
unit = parser.parseCompilationUnit();
unit.sourcefile = source;
}
示例12: test3
import com.sun.tools.javac.parser.Parser; //导入依赖的package包/类
public void test3() {
Context context = new Context();
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
context.put(DiagnosticListener.class, diagnostics);
Options.instance(context).put("allowStringFolding", "false");
JCTree.JCCompilationUnit unit;
JavacFileManager fileManager = new JavacFileManager(context, true, UTF_8);
try {
fileManager.setLocation(StandardLocation.PLATFORM_CLASS_PATH, ImmutableList.<File>of());
} catch (IOException e) {
// impossible
throw new IOError(e);
}
SimpleJavaFileObject source =
new SimpleJavaFileObject(URI.create("source"), JavaFileObject.Kind.SOURCE) {
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
return src;
}
};
Log.instance(context).useSource(source);
ParserFactory parserFactory = ParserFactory.instance(context);
Parser parser =
parserFactory.newParser(
src,
/*keepDocComments=*/ true,
/*keepEndPos=*/ true,
/*keepLineMap=*/ true);
unit = parser.parseCompilationUnit();
unit.sourcefile = source;
}
示例13: test2
import com.sun.tools.javac.parser.Parser; //导入依赖的package包/类
public void test2() {
Context context = new Context();
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
context.put(DiagnosticListener.class, diagnostics);
Options.instance(context).put("allowStringFolding", "false");
JCTree.JCCompilationUnit unit;
JavacFileManager fileManager = new JavacFileManager(context, true, UTF_8);
try {
fileManager.setLocation(StandardLocation.PLATFORM_CLASS_PATH, ImmutableList.<File>of());
} catch (IOException e) {
// impossible
throw new IOError(e);
}
SimpleJavaFileObject source =
new SimpleJavaFileObject(URI.create("source"), JavaFileObject.Kind.SOURCE) {
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
return src;
}
};
Log.instance(context).useSource(source);
ParserFactory parserFactory = ParserFactory.instance(context);
Parser parser =
parserFactory.newParser(
src,
/*keepDocComments=*/ true,
/*keepEndPos=*/ true,
/*keepLineMap=*/ true);
unit = parser.parseCompilationUnit();
unit.sourcefile = source;
}
示例14: newParser
import com.sun.tools.javac.parser.Parser; //导入依赖的package包/类
@Override public Parser newParser(Lexer S, boolean keepDocComments, boolean genEndPos) {
Object x = new CommentCollectingParser(this, S, true);
return (Parser) x;
// CCP is based on a stub which extends nothing, but at runtime the stub is replaced with either
//javac6's EndPosParser which extends Parser, or javac7's EndPosParser which implements Parser.
//Either way this will work out.
}
示例15: setInCompiler
import com.sun.tools.javac.parser.Parser; //导入依赖的package包/类
public static void setInCompiler(JavaCompiler compiler, Context context) {
context.put(CommentCollectingParserFactory.key(), (Parser.Factory)null);
Field field;
try {
field = JavaCompiler.class.getDeclaredField("parserFactory");
field.setAccessible(true);
field.set(compiler, new CommentCollectingParserFactory(context));
} catch (Exception e) {
throw new IllegalStateException("Could not set comment sensitive parser in the compiler", e);
}
}