Java Code Examples for javax.lang.model.SourceVersion#valueOf()

The following examples show how to use javax.lang.model.SourceVersion#valueOf() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ReindenterTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testLineIndentationBeforeHalfIndentedRecordBody() throws Exception {
    try {
        SourceVersion.valueOf("RELEASE_14");
    } catch (IllegalArgumentException ex) {
        //OK, skip test:
        return;
    }
    Preferences preferences = MimeLookup.getLookup(JavaTokenId.language().mimeType()).lookup(Preferences.class);
    preferences.put("classDeclBracePlacement", CodeStyle.BracePlacement.NEW_LINE_HALF_INDENTED.name());
    try {
        performLineIndentationTest("package t;\npublic record T()\n|{\n}\n",
                "package t;\npublic record T()\n  {\n}\n");
    } finally {
        preferences.remove("classDeclBracePlacement");
    }
}
 
Example 2
Source File: DetectorTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testBindingPattern() throws Exception {
    try {
        SourceVersion.valueOf("RELEASE_14");
    } catch (IllegalArgumentException iae) {
        //OK, presumably no support for pattern matching
        return ;
    }
    setSourceLevel("14");
    performTest("BindingPattern",
                "public class BindingPattern {\n" +
                "    public boolean test(Object o) {\n" +
                "        return o instanceof String str && str.isEmpty();\n" +
                "    }\n" +
                "}\n",
                "[PUBLIC, CLASS, DECLARATION], 0:13-0:27",
                "[PUBLIC, METHOD, DECLARATION], 1:19-1:23",
                "[PUBLIC, CLASS], 1:24-1:30",
                "[PARAMETER, DECLARATION], 1:31-1:32",
                "[PARAMETER], 2:15-2:16",
                "[PUBLIC, CLASS], 2:28-2:34",
                "[LOCAL_VARIABLE, DECLARATION], 2:35-2:38",
                "[LOCAL_VARIABLE], 2:42-2:45",
                "[PUBLIC, METHOD], 2:46-2:53");
}
 
Example 3
Source File: MarkOccDetTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testMatchBindings() throws Exception {
    try {
        SourceVersion.valueOf("RELEASE_14"); //NOI18N
    } catch (IllegalArgumentException ex) {
        //OK, no RELEASE_14, skip tests
        return ;
    }
    performTest("MatchBindings.java",
                "public class MatchBindings {\n" +
                "    public boolean t(Object o) {\n" +
                "        if (o instanceof String str && str.isEmpty()) {\n" +
                "            return str.equals(str);\n" +
                "        }\n" +
                "        return false;\n" +
                "    }\n" +
                "}\n",
               3,
               33,
               "[MARK_OCCURRENCES], 2:32-2:35",
               "[MARK_OCCURRENCES], 2:39-2:42",
               "[MARK_OCCURRENCES], 3:19-3:22",
               "[MARK_OCCURRENCES], 3:30-3:33");
}
 
Example 4
Source File: TestSourceVersion.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public boolean process(Set<? extends TypeElement> annotations,
                       RoundEnvironment roundEnvironment) {
    SourceVersion expectedVersion =
        SourceVersion.valueOf(processingEnv.getOptions().get("ExpectedVersion"));
    SourceVersion actualVersion =  processingEnv.getSourceVersion();
    System.out.println("Expected SourceVersion " + expectedVersion +
                       " actual SourceVersion "  + actualVersion);
    if (expectedVersion != actualVersion)
        throw new RuntimeException();

    return true;
}
 
Example 5
Source File: TypingCompletionUnitTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testTextBlock2() throws Exception {
    try {
        SourceVersion.valueOf("RELEASE_13");
    } catch (IllegalArgumentException ex) {
        //OK, skip test
        return ;
    }
    Context ctx = new Context(new JavaKit(), "\"\"\"\n|\"\"\"");
    ctx.typeChar('\"');
    ctx.assertDocumentTextEquals("\"\"\"\n\"|\"\"");
}
 
Example 6
Source File: InstantRenamePerformerTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testPatternBinding() throws Exception {
    try {
        SourceVersion.valueOf("RELEASE_14"); //NOI18N
    } catch (IllegalArgumentException ex) {
        //OK, no RELEASE_14, skip tests
        return ;
    }
    KeyEvent ke = new KeyEvent(new JFrame(), KeyEvent.KEY_TYPED, 0, 0, KeyEvent.VK_UNDEFINED, 'a');
    performTest("package test; public class Test { public void test(Object o) {boolean b = o instanceof String s|tr && str.isEmpty(); } }", 117 - 22, ke, "package test; public class Test { public void test(Object o) {boolean b = o instanceof String satr && satr.isEmpty(); } }", true);
}
 
Example 7
Source File: TestSourceVersionWarnings.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
@Override
public SourceVersion getSupportedSourceVersion() {
    String sourceVersion = processingEnv.getOptions().get("SourceVersion");
    if (sourceVersion == null) {
        processingEnv.getMessager().printMessage(WARNING,
                                                 "No SourceVersion option given");
        return SourceVersion.RELEASE_6;
    } else {
        return SourceVersion.valueOf(sourceVersion);
    }
}
 
Example 8
Source File: TypingCompletionUnitTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testTextBlock3() throws Exception {
    try {
        SourceVersion.valueOf("RELEASE_13");
    } catch (IllegalArgumentException ex) {
        //OK, skip test
        return ;
    }
    Context ctx = new Context(new JavaKit(), "\"\"\"\n\"|\"\"");
    ctx.typeChar('\"');
    ctx.assertDocumentTextEquals("\"\"\"\n\"\"|\"");
}
 
Example 9
Source File: ReindenterTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testNewLineIndentationTextBlock1() throws Exception {
    try {
        SourceVersion.valueOf("RELEASE_13");
    } catch (IllegalArgumentException ex) {
        //OK, skip test:
        return ;
    }
    performNewLineIndentationTest("package t;\npublic class T {\n    private final String s = \"\"\"|\n}\n",
            "package t;\npublic class T {\n    private final String s = \"\"\"\n                             \n}\n");
}
 
Example 10
Source File: TestSourceVersion.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public boolean process(Set<? extends TypeElement> annotations,
                       RoundEnvironment roundEnvironment) {
    SourceVersion expectedVersion =
        SourceVersion.valueOf(processingEnv.getOptions().get("ExpectedVersion"));
    SourceVersion actualVersion =  processingEnv.getSourceVersion();
    System.out.println("Expected SourceVersion " + expectedVersion +
                       " actual SourceVersion "  + actualVersion);
    if (expectedVersion != actualVersion)
        throw new RuntimeException();

    return true;
}
 
Example 11
Source File: ConvertTextBlockToStringTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Test
public void newLineAtEnd() throws Exception {
    try {
        SourceVersion.valueOf("RELEASE_13");
    } catch (IllegalArgumentException ex) {
        //OK, skip test
        return;
    }
    HintTest.create()
            .input("package helloworld;\n"
                    + "public class Test {\n"
                    + "    public static void main(String[] args) {\n"
                    + "        String a =\"\"\"\n"
                    + "                  abc\n"
                    + "                  def\n"
                    + "                  hij\n"
                    + "                  \"\"\";\n"
                    + "    }\n"
                    + "}")
            .sourceLevel(SourceVersion.latest().name())
            .options("--enable-preview")
            .run(ConvertTextBlockToString.class)
            .findWarning("3:18-3:21:verifier:" + Bundle.ERR_ConvertTextBlockToString())
            .applyFix()
            .assertCompilable()
            .assertOutput("package helloworld;\n"
                    + "public class Test {\n"
                    + "    public static void main(String[] args) {\n"
                    + "        String a =\"abc\\n\" + \"def\\n\" + \"hij\\n\";\n"
                    + "    }\n"
                    + "}");
}
 
Example 12
Source File: TestSourceVersion.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public boolean process(Set<? extends TypeElement> annotations,
                       RoundEnvironment roundEnvironment) {
    SourceVersion expectedVersion =
        SourceVersion.valueOf(processingEnv.getOptions().get("ExpectedVersion"));
    SourceVersion actualVersion =  processingEnv.getSourceVersion();
    System.out.println("Expected SourceVersion " + expectedVersion +
                       " actual SourceVersion "  + actualVersion);
    if (expectedVersion != actualVersion)
        throw new RuntimeException();

    return true;
}
 
Example 13
Source File: ConvertTextBlockToStringTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Test
public void manyLineTextBlock() throws Exception {
    try {
        SourceVersion.valueOf("RELEASE_13");
    } catch (IllegalArgumentException ex) {
        //OK, skip test
        return;
    }
    HintTest.create()
            .input("package helloworld;\n"
                    + "public class Test {\n"
                    + "    public static void main(String[] args) {\n"
                    + "        String a =\"\"\"\n"
                    + "                  abc\n"
                    + "                  def\n"
                    + "                  ghi\n"
                    + "                  jkl\n"
                    + "                  mno\n"
                    + "                  pqrs\n"
                    + "                  tuv\n"
                    + "                  wxyz\"\"\";\n"
                    + "    }\n"
                    + "}")
            .sourceLevel(SourceVersion.latest().name())
            .options("--enable-preview")
            .run(ConvertTextBlockToString.class)
            .findWarning("3:18-3:21:verifier:" + Bundle.ERR_ConvertTextBlockToString())
            .applyFix()
            .assertCompilable()
            .assertOutput("package helloworld;\n"
                    + "public class Test {\n"
                    + "    public static void main(String[] args) {\n"
                    + "        String a =\"abc\\n\" + \"def\\n\" + \"ghi\\n\" + \"jkl\\n\" + \"mno\\n\" + \"pqrs\\n\" + \"tuv\\n\" + \"wxyz\";\n"
                    + "    }\n"
                    + "}");
}
 
Example 14
Source File: SchemaGenerator.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public SourceVersion getSupportedSourceVersion() {
    if (SourceVersion.latest().compareTo(SourceVersion.RELEASE_6) > 0)
        return SourceVersion.valueOf("RELEASE_7");
    else
        return SourceVersion.RELEASE_6;
}
 
Example 15
Source File: TypingCompletionUnitTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testTextBlock4() throws Exception {
    try {
        SourceVersion.valueOf("RELEASE_13");
    } catch (IllegalArgumentException ex) {
        //OK, skip test
        return ;
    }
    Context ctx = new Context(new JavaKit(), "\"\"\"\n\"\"|\"");
    ctx.typeChar('\"');
    ctx.assertDocumentTextEquals("\"\"\"\n\"\"\"|");
}
 
Example 16
Source File: GoToSupportTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static boolean hasRecords() {
    try {
        SourceVersion.valueOf("RELEASE_14"); //NOI18N
        return true;
    } catch (IllegalArgumentException ex) {
        //OK, no RELEASE_14, skip tests
        return false;
    }
}
 
Example 17
Source File: AnnotationParser.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public SourceVersion getSupportedSourceVersion() {
    if (SourceVersion.latest().compareTo(SourceVersion.RELEASE_6) > 0)
        return SourceVersion.valueOf("RELEASE_7");
    else
        return SourceVersion.RELEASE_6;
}
 
Example 18
Source File: TestSourceVersionWarnings.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public SourceVersion getSupportedSourceVersion() {
    String sourceVersion = processingEnv.getOptions().get("SourceVersion");
    if (sourceVersion == null) {
        processingEnv.getMessager().printMessage(WARNING,
                                                 "No SourceVersion option given");
        return SourceVersion.RELEASE_6;
    } else {
        return SourceVersion.valueOf(sourceVersion);
    }
}
 
Example 19
Source File: LiteralTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void testTextBlocksReplace() throws Exception {
    try {
        SourceVersion.valueOf("RELEASE_13");
    } catch (IllegalArgumentException ex) {
        //OK, skip test
        return ;
    }

    testFile = new File(getWorkDir(), "Test.java");
    TestUtilities.copyStringToFile(testFile,
        "package hierbas.del.litoral;\n" +
        "\n" +
        "public class Test {\n" +
        "    public static final String C = \"\"\"\n" +
        "                                   old\"\"\";\n" +
        "}\n"
        );
    String golden =
        "package hierbas.del.litoral;\n" +
        "\n" +
        "public class Test {\n" +
        "    public static final String C = \"\"\"\n" +
        "                                   new\n" +
        "                                   \"\"\";\n" +
        "}\n";
    JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
    Task<WorkingCopy> task = new Task<WorkingCopy>() {

        public void run(WorkingCopy workingCopy) throws java.io.IOException {
            workingCopy.toPhase(Phase.RESOLVED);
            TreeMaker make = workingCopy.getTreeMaker();

            ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
            VariableTree var = (VariableTree) clazz.getMembers().get(1);
            LiteralTree val = make.Literal(new String[] {"new",
                                                         ""});
            VariableTree nue = make.setInitialValue(var, val);
            workingCopy.rewrite(var, nue);
        }

    };
    testSource.runModificationTask(task).commit();
    String res = TestUtilities.copyFileToString(testFile);
    //System.err.println(res);
    assertEquals(golden, res);
}
 
Example 20
Source File: LiteralTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void testTextBlocksNew() throws Exception {
    try {
        SourceVersion.valueOf("RELEASE_13");
    } catch (IllegalArgumentException ex) {
        //OK, skip test:
        return ;
    }
    testFile = new File(getWorkDir(), "Test.java");
    TestUtilities.copyStringToFile(testFile,
        "package hierbas.del.litoral;\n" +
        "\n" +
        "public class Test {\n" +
        "    public static final String C;\n" +
        "}\n"
        );
    String golden =
        "package hierbas.del.litoral;\n" +
        "\n" +
        "public class Test {\n" +
        "    public static final String C = \"\"\"\n" +
        "                                   line \"1\n" +
        "                                   line\\\"\\\"\\\"\"\"2\"\n" +
        "                                   line\\n3\"\"\";\n" +
        "}\n";
    JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
    Task<WorkingCopy> task = new Task<WorkingCopy>() {

        public void run(WorkingCopy workingCopy) throws java.io.IOException {
            workingCopy.toPhase(Phase.RESOLVED);
            TreeMaker make = workingCopy.getTreeMaker();

            ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
            VariableTree var = (VariableTree) clazz.getMembers().get(1);
            LiteralTree val = make.Literal(new String[] {"line \"1",
                                                         "line\"\"\"\"\"2\"",
                                                         "line\n3"});
            VariableTree nue = make.setInitialValue(var, val);
            workingCopy.rewrite(var, nue);
        }

    };
    testSource.runModificationTask(task).commit();
    String res = TestUtilities.copyFileToString(testFile);
    //System.err.println(res);
    assertEquals(golden, res);
}