test/tools/javac/parser/StringFoldingTest.java

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 3074
ca136b17b9f4
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

     1 /*
     2  * Copyright (c) 2011, 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  */
    24 /*
    25  * @test
    26  * @bug 7068902
    27  * @summary verify that string folding can be enabled or disabled
    28  */
    30 import com.sun.source.tree.CompilationUnitTree;
    31 import com.sun.source.util.JavacTask;
    32 import java.io.File;
    33 import java.io.IOException;
    34 import java.net.URI;
    35 import java.util.ArrayList;
    36 import java.util.Arrays;
    37 import java.util.List;
    38 import javax.tools.JavaCompiler;
    39 import javax.tools.JavaFileObject;
    40 import javax.tools.SimpleJavaFileObject;
    41 import javax.tools.ToolProvider;
    43 public class StringFoldingTest {
    44     final JavaCompiler tool;
    45     final JavaSource source;
    47     public StringFoldingTest() {
    48         tool = ToolProvider.getSystemJavaCompiler();
    49         source = new JavaSource();
    50     }
    52     static class JavaSource extends SimpleJavaFileObject {
    54         final static String source =
    55                 "class C {String X=\"F\" + \"O\" + \"L\" + \"D\" + \"E\" + \"D\";}";
    57         JavaSource() {
    58             super(URI.create("myfo:/C.java"), JavaFileObject.Kind.SOURCE);
    59         }
    61         @Override
    62         public CharSequence getCharContent(boolean ignoreEncodingErrors) {
    63             return source;
    64         }
    65     }
    67     public static void main(String... args) throws IOException {
    68         StringFoldingTest t = new StringFoldingTest();
    69         t.run(false);
    70         t.run(true);
    71     }
    73     void run(boolean disableStringFolding) throws IOException {
    74         List<String> argsList = new ArrayList<String>();
    75         if (disableStringFolding) {
    76             argsList.add("-XDallowStringFolding=false");
    77         }
    78         JavacTask ct = (JavacTask)tool.getTask(null, null, null,
    79                 argsList,
    80                 null,
    81                 Arrays.asList(source));
    82         Iterable<? extends CompilationUnitTree> trees = ct.parse();
    83         String text = trees.toString();
    84         System.out.println(text);
    86         if (disableStringFolding) {
    87             if (text.contains("FOLDED")) {
    88                 throw new AssertionError("Expected string folding");
    89             }
    90         } else {
    91             if (!text.contains("FOLDED")) {
    92                 throw new AssertionError("Expected no string folding");
    93             }
    94         }
    95     }
    96 }

mercurial