• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Java Parser类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Java中com.sun.tools.javac.parser.Parser的典型用法代码示例。如果您正苦于以下问题:Java Parser类的具体用法?Java Parser怎么用?Java Parser使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



Parser类属于com.sun.tools.javac.parser包,在下文中一共展示了Parser类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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);
    }
}
 
开发者ID:tranleduy2000,项目名称:javaide,代码行数:21,代码来源:JavacTaskImpl.java


示例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;
}
 
开发者ID:tranleduy2000,项目名称:javaide,代码行数:23,代码来源:JavaParser.java


示例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);
    }
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:21,代码来源:JavacTaskImpl.java


示例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);
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:25,代码来源:JavacTaskImpl.java


示例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);
    }
}
 
开发者ID:unktomi,项目名称:form-follows-function,代码行数:23,代码来源:JavacTaskImpl.java


示例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);
    }
}
 
开发者ID:lucasicf,项目名称:metricgenerator-jdk-compiler,代码行数:23,代码来源:JavacTaskImpl.java


示例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;
    }
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:38,代码来源:TreeUtilities.java


示例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);
    }
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:29,代码来源:Utilities.java


示例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);
    }
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:34,代码来源:Utilities.java


示例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;
}
 
开发者ID:tranleduy2000,项目名称:javaide,代码行数:32,代码来源:TestJavacParser.java


示例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;
}
 
开发者ID:tranleduy2000,项目名称:javaide,代码行数:32,代码来源:TestJavacParser.java


示例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;
}
 
开发者ID:tranleduy2000,项目名称:javaide,代码行数:32,代码来源:TestJavacParser.java


示例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;
}
 
开发者ID:tranleduy2000,项目名称:javaide,代码行数:32,代码来源:TestJavacParser.java


示例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.
}
 
开发者ID:git03394538,项目名称:lombok-ianchiu,代码行数:8,代码来源:CommentCollectingParserFactory.java


示例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);
	}
}
 
开发者ID:git03394538,项目名称:lombok-ianchiu,代码行数:12,代码来源:CommentCollectingParserFactory.java


示例16: newParser

import com.sun.tools.javac.parser.Parser; //导入依赖的package包/类
public Parser newParser(CharSequence input, boolean keepDocComments, boolean keepEndPos, boolean keepLineMap) {
	ScannerFactory scannerFactory = ScannerFactory.instance(context);
	Lexer lexer = scannerFactory.newScanner(input, true);
	Object x = new CommentCollectingParser(this, lexer, true, keepLineMap);
	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.
}
 
开发者ID:git03394538,项目名称:lombok-ianchiu,代码行数:10,代码来源:CommentCollectingParserFactory.java


示例17: test

import com.sun.tools.javac.parser.Parser; //导入依赖的package包/类
static void test(boolean genEndPos) throws IOException {
    Context context = new Context();

    Options options = Options.instance(context);
    options.put("diags", "%b:%s/%o/%e:%_%t%m|%p%m");

    Log log = Log.instance(context);
    log.multipleErrors = true;

    JavacFileManager.preRegister(context);
    ParserFactory pfac = ParserFactory.instance(context);

    final String text =
          "public class Foo {\n"
        + "  public static void main(String[] args) {\n"
        + "    if (args.length == 0)\n"
        + "      System.out.println(\"no args\");\n"
        + "    else\n"
        + "      System.out.println(args.length + \" args\");\n"
        + "  }\n"
        + "}\n";
    JavaFileObject fo = new StringJavaFileObject("Foo", text);
    log.useSource(fo);

    CharSequence cs = fo.getCharContent(true);
    Parser parser = pfac.newParser(cs, false, genEndPos, false);
    JCTree.JCCompilationUnit tree = parser.parseCompilationUnit();
    log.setEndPosTable(fo, tree.endPositions);

    TreeScanner ts = new LogTester(log, tree.endPositions);
    ts.scan(tree);

    check(log.nerrors, 4, "errors");
    check(log.nwarnings, 4, "warnings");
}
 
开发者ID:ojdkbuild,项目名称:lookaside_java-1.8.0-openjdk,代码行数:36,代码来源:TestLog.java


示例18: createParser

import com.sun.tools.javac.parser.Parser; //导入依赖的package包/类
public Parser createParser(String sources) {
    try {
        return (Parser) newParserMethod.invoke(parserFactory, sources, false, false, false);
    } catch (IllegalAccessException | InvocationTargetException ignored) {
    }
    return null;
}
 
开发者ID:ixkor,项目名称:genaroid,代码行数:8,代码来源:GenaroidEnvironment.java


示例19: test

import com.sun.tools.javac.parser.Parser; //导入依赖的package包/类
static void test(boolean genEndPos) throws IOException {
    Context context = new Context();

    Options options = Options.instance(context);
    options.put("diags", "%b:%s/%o/%e:%_%t%m|%p%m");

    Log log = Log.instance(context);
    log.multipleErrors = true;

    JavacFileManager.preRegister(context);
    Scanner.Factory sfac = Scanner.Factory.instance(context);
    Parser.Factory pfac = Parser.Factory.instance(context);

    final String text =
          "public class Foo {\n"
        + "  public static void main(String[] args) {\n"
        + "    if (args.length == 0)\n"
        + "      System.out.println(\"no args\");\n"
        + "    else\n"
        + "      System.out.println(args.length + \" args\");\n"
        + "  }\n"
        + "}\n";
    JavaFileObject fo = new StringJavaFileObject("Foo", text);
    log.useSource(fo);

    Scanner s = sfac.newScanner(fo.getCharContent(true));
    Parser parser = pfac.newParser(s, false, genEndPos);
    JCTree.JCCompilationUnit tree = parser.compilationUnit();
    log.setEndPosTable(fo, tree.endPositions);

    TreeScanner ts = new LogTester(log, tree.endPositions);
    ts.scan(tree);

    check(log.nerrors, 4, "errors");
    check(log.nwarnings, 4, "warnings");
}
 
开发者ID:unktomi,项目名称:form-follows-function,代码行数:37,代码来源:TestLog.java


示例20: parseExpression

import com.sun.tools.javac.parser.Parser; //导入依赖的package包/类
protected JCExpression parseExpression(String contents) {
  Parser parser =
      ParserFactory.instance(context)
          .newParser(
              contents,
              /* keepDocComments= */ false,
              /* keepEndPos= */ false,
              /* keepLineMap= */ true);
  return parser.parseExpression();
}
 
开发者ID:google,项目名称:error-prone,代码行数:11,代码来源:AbstractUTreeTest.java



注:本文中的com.sun.tools.javac.parser.Parser类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Java WebResourceFactory类代码示例发布时间:2022-05-21
下一篇:
Java DefaultSettingsDecryptionRequest类代码示例发布时间:2022-05-21
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap