use of org.intellij.lang.annotations.Language in project android by JetBrains.
the class LombokPsiConverterTest method testJava7.
public void testJava7() {
@Language("JAVA") String testClass = "package test.pkg;\n" + "\n" + "import java.io.BufferedReader;\n" + "import java.io.FileReader;\n" + "import java.io.IOException;\n" + "import java.lang.reflect.InvocationTargetException;\n" + "import java.util.List;\n" + "import java.util.Map;\n" + "import java.util.TreeMap;\n" + "\n" + "public class Java7LanguageFeatureTest {\n" + " public void testDiamondOperator() {\n" + " Map<String, List<Integer>> map = new TreeMap<>();\n" + " }\n" + "\n" + " public int testStringSwitches(String value) {\n" + " final String first = \"first\";\n" + " final String second = \"second\";\n" + "\n" + " switch (value) {\n" + " case first:\n" + " return 41;\n" + " case second:\n" + " return 42;\n" + " default:\n" + " return 0;\n" + " }\n" + " }\n" + "\n" + " public String testTryWithResources(String path) throws IOException {\n" + " try (BufferedReader br = new BufferedReader(new FileReader(path))) {\n" + " return br.readLine();\n" + " }\n" + " }\n" + "\n" + " public void testNumericLiterals() {\n" + " int thousand = 1_000;\n" + " int million = 1_000_000;\n" + " int binary = 0B01010101;\n" + " }\n" + "\n" + " public void testMultiCatch() {\n" + "\n" + " try {\n" + " Class.forName(\"java.lang.Integer\").getMethod(\"toString\").invoke(null);\n" + " } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {\n" + " e.printStackTrace();\n" + " } catch (ClassNotFoundException e) {\n" + " // TODO: Logging here\n" + " }\n" + " }\n" + "}\n";
PsiFile psiFile = myFixture.addFileToProject("src/test/pkg/R9.java", testClass);
// Can't call check(psiFile, testClass) here; the source code won't parse
// with Lombok's parser since it doesn't support Java 7, so we just manually
// check that the LombokPsiConverter doesn't abort, and format it's view of
// the Lombok AST and check that it looks like what we expect; an AST containing
// fragments usable by lint, but not providing support for syntactic constructs
// such as try with resources or containing type variables where the diamond operator
// should have been etc.
assertTrue(psiFile.getClass().getName(), psiFile instanceof PsiJavaFile);
PsiJavaFile psiJavaFile = (PsiJavaFile) psiFile;
CompilationUnit node = LombokPsiConverter.convert(psiJavaFile);
assertNotNull(node);
TextFormatter formatter = new TextFormatter();
node.accept(new SourcePrinter(formatter));
String actual = formatter.finish();
assertEquals("package test.pkg;\n" + "\n" + "import java.io.BufferedReader;\n" + "import java.io.FileReader;\n" + "import java.io.IOException;\n" + "import java.lang.reflect.InvocationTargetException;\n" + "import java.util.List;\n" + "import java.util.Map;\n" + "import java.util.TreeMap;\n" + "\n" + "public class Java7LanguageFeatureTest {\n" + " public void testDiamondOperator() {\n" + " Map<String, List<Integer>> map = new TreeMap();\n" + " }\n" + " \n" + " public int testStringSwitches(String value) {\n" + " final String first = \"first\";\n" + " final String second = \"second\";\n" + " switch (value) {\n" + " case first:\n" + " return 41;\n" + " case second:\n" + " return 42;\n" + " case :\n" + " return 0;\n" + " }\n" + " }\n" + " \n" + " public String testTryWithResources(String path) throws IOException {\n" + " try {\n" + " return br.readLine();\n" + " }\n" + " }\n" + " \n" + " public void testNumericLiterals() {\n" + " int thousand = 1_000;\n" + " int million = 1_000_000;\n" + " int binary = 0B01010101;\n" + " }\n" + " \n" + " public void testMultiCatch() {\n" + " try {\n" + " Class.forName(\"java.lang.Integer\").getMethod(\"toString\").invoke(null);\n" + " } catch (?!?INVALID_IDENTIFIER: IllegalAccessException | InvocationTargetException | NoSuchMethodException?!? e) {\n" + " e.printStackTrace();\n" + " } catch (ClassNotFoundException e) {\n" + " }\n" + " }\n" + "}", actual);
}
use of org.intellij.lang.annotations.Language in project android by JetBrains.
the class LombokPsiConverterTest method testPsiToLombokConversion4.
public void testPsiToLombokConversion4() {
//noinspection MethodMayBeStatic
@Language("JAVA") String testClass = "package test.pkg;\n" + "\n" + "import android.annotation.SuppressLint;\n" + "import android.annotation.TargetApi;\n" + "import android.os.Build;\n" + "import org.w3c.dom.DOMError;\n" + "import org.w3c.dom.DOMErrorHandler;\n" + "import org.w3c.dom.DOMLocator;\n" + "\n" + "import android.view.ViewGroup.LayoutParams;\n" + "import android.app.Activity;\n" + "import android.app.ApplicationErrorReport;\n" + "import android.app.ApplicationErrorReport.BatteryInfo;\n" + "import android.graphics.PorterDuff;\n" + "import android.graphics.PorterDuff.Mode;\n" + "import android.widget.Chronometer;\n" + "import android.widget.GridLayout;\n" + "import dalvik.bytecode.OpcodeInfo;\n" + "\n" + "public class ApiCallTest extends Activity {\n" + " @TargetApi(Build.VERSION_CODES.GINGERBREAD_MR1)\n" + " public void method(Chronometer chronometer, DOMLocator locator) {\n" + " String s = \"/sdcard/fyfaen\";\n" + " // Virtual call\n" + " getActionBar(); // API 11\n" + "\n" + " // Class references (no call or field access)\n" + " DOMError error = null; // API 8\n" + " Class<?> clz = DOMErrorHandler.class; // API 8\n" + "\n" + " // Method call\n" + " chronometer.getOnChronometerTickListener(); // API 3\n" + "\n" + " // Inherited method call (from TextView\n" + " chronometer.setTextIsSelectable(true); // API 11\n" + "\n" + " // Field access\n" + " int field = OpcodeInfo.MAXIMUM_VALUE; // API 11\n" + " int fillParent = LayoutParams.FILL_PARENT; // API 1\n" + " // This is a final int, which means it gets inlined\n" + " int matchParent = LayoutParams.MATCH_PARENT; // API 8\n" + " // Field access: non final\n" + " BatteryInfo batteryInfo = getReport().batteryInfo;\n" + "\n" + " // Enum access\n" + " Mode mode = PorterDuff.Mode.OVERLAY; // API 11\n" + " }\n" + "\n" + " // Return type\n" + " GridLayout getGridLayout() { // API 14\n" + " return null;\n" + " }\n" + "\n" + " private ApplicationErrorReport getReport() {\n" + " return null;\n" + " }\n" + "}\n";
// Parse the above file as a PSI datastructure
PsiFile file = myFixture.addFileToProject("src/test/pkg/ApiCallTest.java", testClass);
check(file, testClass);
}
use of org.intellij.lang.annotations.Language in project android by JetBrains.
the class LombokPsiConverterTest method testPsiToLombokConversion5.
public void testPsiToLombokConversion5() {
@Language("JAVA") String testClass = "package test.pkg;\n" + "\n" + "/* This stub is for using by IDE only. It is NOT the Manifest class actually packed into APK */\n" + "public final class Manifest {\n" + "}";
PsiFile file = myFixture.addFileToProject("src/test/pkg/Manifest.java", testClass);
check(file, testClass);
}
use of org.intellij.lang.annotations.Language in project android by JetBrains.
the class LombokPsiConverterTest method testPsiNullValue.
public void testPsiNullValue() {
// From the IO scheduling app:
// String SESSIONS_SNIPPET = "snippet(" + Tables.SESSIONS_SEARCH + ",'{','}','…')";
@Language("JAVA") String testClass = "package test.pkg;\n" + "\n" + "public final class R6 {\n" + " public void foo() {\n" + " String s = \",'{','}','\\u2026')\";\n" + " }\n" + "}";
PsiFile file = myFixture.addFileToProject("src/test/pkg/R6.java", testClass);
check(file, testClass);
}
use of org.intellij.lang.annotations.Language in project android by JetBrains.
the class LombokPsiConverterTest method testPsiToLombokConversion11.
public void testPsiToLombokConversion11() {
// Test Polyadic Expression
@Language("JAVA") String testClass = "package test.pkg;\n" + "\n" + "public final class R5 {\n" + " public static final int test = 5;\n" + " public String getString(int id) { return \"\"; }\n" + " public void foo() {\n" + " String trackName = \"test\";\n" + " String x = trackName + \" - \" + getString(R5.test);\n" + " }\n" + "}";
PsiFile file = myFixture.addFileToProject("src/test/pkg/R5.java", testClass);
check(file, testClass);
}
Aggregations