From 35d67a6f12a23bd7e6272f4c188b29ac771232dc Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Fri, 2 Feb 2018 16:28:22 +0700 Subject: [PATCH] BytecodeAnalysisTest: test for Java9 string concatenation (IDEA-CR-28861) Precompiled data folder was created, and existing TestConflict was also moved there, removing the dependency on compiler during runtime. --- .../BytecodeAnalysisTest.java | 39 ++++++++++-------- .../precompiledData/TestConflict.class | Bin 0 -> 845 bytes .../TestConflict.java | 2 + .../precompiledData/TestJava9.class | Bin 0 -> 967 bytes .../precompiledData/TestJava9.java | 16 +++++++ 5 files changed, 40 insertions(+), 17 deletions(-) create mode 100644 java/java-tests/testSrc/com/intellij/java/codeInspection/bytecodeAnalysis/precompiledData/TestConflict.class rename java/java-tests/testSrc/com/intellij/java/codeInspection/bytecodeAnalysis/{classConflict => precompiledData}/TestConflict.java (89%) create mode 100644 java/java-tests/testSrc/com/intellij/java/codeInspection/bytecodeAnalysis/precompiledData/TestJava9.class create mode 100644 java/java-tests/testSrc/com/intellij/java/codeInspection/bytecodeAnalysis/precompiledData/TestJava9.java diff --git a/java/java-tests/testSrc/com/intellij/java/codeInspection/bytecodeAnalysis/BytecodeAnalysisTest.java b/java/java-tests/testSrc/com/intellij/java/codeInspection/bytecodeAnalysis/BytecodeAnalysisTest.java index e80de8735da4..f62dd7a5df6a 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInspection/bytecodeAnalysis/BytecodeAnalysisTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInspection/bytecodeAnalysis/BytecodeAnalysisTest.java @@ -20,14 +20,13 @@ import org.jetbrains.org.objectweb.asm.tree.MethodNode; import org.jetbrains.org.objectweb.asm.tree.analysis.AnalyzerException; import org.junit.Assert; -import javax.tools.*; import java.io.File; import java.io.IOException; import java.lang.annotation.Annotation; import java.lang.reflect.Constructor; +import java.net.URL; +import java.net.URLClassLoader; import java.security.MessageDigest; -import java.util.Arrays; -import java.util.Collections; import java.util.HashMap; import java.util.stream.Stream; @@ -67,6 +66,13 @@ public class BytecodeAnalysisTest extends JavaCodeInsightFixtureTestCase { checkAnnotations(TestEnum.class); } + public void testJava9Inference() throws IOException, ClassNotFoundException { + try (URLClassLoader cl = new URLClassLoader(new URL[]{new File(myModule.getProject().getBasePath()).toURI().toURL()})) { + Class java9Class = cl.loadClass(Test01.class.getPackage().getName() + ".TestJava9"); + checkAnnotations(java9Class); + } + } + public void testHashCollision() { checkAnnotations(TestHashCollision.class); } @@ -118,7 +124,7 @@ public class BytecodeAnalysisTest extends JavaCodeInsightFixtureTestCase { isLeaking = true; } } - assertEquals(method.toString() + " #" + i, isLeaking, map.get(method)[i]); + assertEquals(method + " #" + i, isLeaking, map.get(method)[i]); } } } @@ -246,27 +252,26 @@ public class BytecodeAnalysisTest extends JavaCodeInsightFixtureTestCase { PsiTestUtil.addLibrary(myModule, "dataClasses", vFile.getPath(), new String[]{""}, ArrayUtil.EMPTY_STRING_ARRAY); if(getTestName(false).equals("Inference")) { - setUpConflictingClasses(basePath); + setUpPrecompiledDataClasses(basePath); } } - private void setUpConflictingClasses(String basePath) throws IOException { - JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); - DiagnosticCollector diagnostics = new DiagnosticCollector<>(); - StandardJavaFileManager manager = compiler.getStandardFileManager(diagnostics, null, null); + private void setUpPrecompiledDataClasses(String basePath) throws IOException { File sourcePath = new File(PlatformTestUtil.getCommunityPath() + "/java/java-tests/testSrc/" + PACKAGE_PATH); - File[] sourceFiles = new File(sourcePath.getParentFile(), "classConflict").listFiles((dir, name) -> name.endsWith(".java")); + File[] sourceFiles = new File(sourcePath.getParentFile(), "precompiledData").listFiles((dir, name) -> name.endsWith(".java")); assertNotNull(sourceFiles); - Iterable sources = manager.getJavaFileObjectsFromFiles(Arrays.asList(sourceFiles)); - File conflictOutput = new File(basePath + "/conflict/"); + File conflictOutput = new File(basePath + "/precompiled/"+PACKAGE_PATH); assertTrue(conflictOutput.mkdirs()); - manager.setLocation(StandardLocation.CLASS_OUTPUT, Collections.singleton(conflictOutput)); - JavaCompiler.CompilationTask task = compiler.getTask(null, manager, diagnostics, null, null, sources); - if(!task.call()) { - fail(diagnostics.getDiagnostics().toString()); + for (File file : sourceFiles) { + File precompiledFile = new File(file.getParentFile(), file.getName().replaceFirst(".java$", ".class")); + if(!precompiledFile.exists()) { + fail("Unable to find precompiled "+precompiledFile+" for source file "+file); + } + FileUtil.copy(precompiledFile, new File(conflictOutput, precompiledFile.getName())); } + VirtualFile vFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(conflictOutput); assertNotNull(vFile); - PsiTestUtil.addLibrary(myModule, "conflictClasses", vFile.getPath(), new String[]{""}, ArrayUtil.EMPTY_STRING_ARRAY); + PsiTestUtil.addLibrary(myModule, "precompiled", vFile.getPath(), new String[]{""}, ArrayUtil.EMPTY_STRING_ARRAY); } } diff --git a/java/java-tests/testSrc/com/intellij/java/codeInspection/bytecodeAnalysis/precompiledData/TestConflict.class b/java/java-tests/testSrc/com/intellij/java/codeInspection/bytecodeAnalysis/precompiledData/TestConflict.class new file mode 100644 index 0000000000000000000000000000000000000000..999d44f0fe83d85643b16a470845095f44bb4d82 GIT binary patch literal 845 zcma)4K~EDw7=2TqyIm`V0#%9>3W>2C+zU4iiB?RS5G1xS@i^U1Wt!cYW_K3t&w_&_ zn#i3WMf_%~-5O1B4)e{tdGCF1XXp0r+YbPT*!SR|=HQWs0%|2Z#-@ilwj4Zhu+30> zDU~!w3=7X%9~c(fdLS4oCsK*iH0q1E$NM26Tjo60pF8ReH~f?vc_BKgt<^A;fyu-j z;;3H-meiZ_b51F9c&*JjLt~uAWMFw+IS|Lgp$N>pVI!G?E$u85&<)Bf<4t5PFYEO_*SVeKC3^R;#JBD&8kSiHK>iScwr#sGy%Vm!;igvm0*riuou0 z4E-eZgFnC@CC)a%o+Jo*aJw_JGqdx|GyCJ`$#(#caWBLoR#!>B8elEJHHP9dsik|) zP}tr%U|4LJo?s|9r53Gh*cEAqcNJNRiO~sn3{SS3eSXYi#r0v_cB#~dwOPx~tcjuW zF4Im9#euYvlGe30jyq|zW!P;dW*AHDgi^8}pI(trE&JMzMB<&|?$`-$s%x&sR@(UG zCx6s1+NC^kHL8A;rJ_La7(@NP6Sj7jThk03~C%g}aojf=i^ANWV<~m0zeB437W+ literal 0 HcmV?d00001 diff --git a/java/java-tests/testSrc/com/intellij/java/codeInspection/bytecodeAnalysis/precompiledData/TestJava9.java b/java/java-tests/testSrc/com/intellij/java/codeInspection/bytecodeAnalysis/precompiledData/TestJava9.java new file mode 100644 index 000000000000..ec2f227c4152 --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/java/codeInspection/bytecodeAnalysis/precompiledData/TestJava9.java @@ -0,0 +1,16 @@ +package com.intellij.java.codeInspection.bytecodeAnalysis.data; + +import com.intellij.java.codeInspection.bytecodeAnalysis.ExpectContract; +import com.intellij.java.codeInspection.bytecodeAnalysis.ExpectNotNull; + +// Test that indified string concatenation is properly recognized +// This file is precompiled via Java 9 compiler and class file is placed in the same directory +// Compilation command is +// "C:\Program Files\Java\jdk-9.0.1\bin\javac.exe" -cp $PROJECT_DIR$\out\classes\test\intellij.java.tests TestJava9.java +public class TestJava9 { + @ExpectContract(pure = true) + @ExpectNotNull + String concat(String foo, String bar) { + return foo+"!"+bar; + } +} \ No newline at end of file