1/* 2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/** 25 * @test 26 * @bug 8056897 27 * @modules jdk.compiler/com.sun.tools.javac.parser 28 * jdk.compiler/com.sun.tools.javac.util 29 * @summary Proper lexing of integer literals. 30 */ 31 32import java.io.IOException; 33import java.net.URI; 34import java.util.Objects; 35 36import javax.tools.JavaFileObject; 37import javax.tools.SimpleJavaFileObject; 38 39import com.sun.tools.javac.parser.JavaTokenizer; 40import com.sun.tools.javac.parser.ScannerFactory; 41import com.sun.tools.javac.parser.Tokens.Token; 42import com.sun.tools.javac.parser.Tokens.TokenKind; 43import com.sun.tools.javac.util.Context; 44import com.sun.tools.javac.util.Log; 45 46public class JavaLexerTest { 47 public static void main(String... args) throws Exception { 48 new JavaLexerTest().run(); 49 } 50 51 void run() throws Exception { 52 Context ctx = new Context(); 53 Log log = Log.instance(ctx); 54 String input = "0bL 0b20L 0xL "; 55 log.useSource(new SimpleJavaFileObject(new URI("mem://Test.java"), JavaFileObject.Kind.SOURCE) { 56 @Override 57 public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { 58 return input; 59 } 60 }); 61 char[] inputArr = input.toCharArray(); 62 JavaTokenizer tokenizer = new JavaTokenizer(ScannerFactory.instance(ctx), inputArr, inputArr.length) { 63 }; 64 65 assertKind(input, tokenizer, TokenKind.LONGLITERAL, "0bL"); 66 assertKind(input, tokenizer, TokenKind.LONGLITERAL, "0b20L"); 67 assertKind(input, tokenizer, TokenKind.LONGLITERAL, "0xL"); 68 } 69 70 void assertKind(String input, JavaTokenizer tokenizer, TokenKind kind, String expectedText) { 71 Token token = tokenizer.readToken(); 72 73 if (token.kind != kind) { 74 throw new AssertionError("Unexpected token kind: " + token.kind); 75 } 76 77 String actualText = input.substring(token.pos, token.endPos); 78 79 if (!Objects.equals(actualText, expectedText)) { 80 throw new AssertionError("Unexpected token text: " + actualText); 81 } 82 } 83} 84