java: .class file tests cleanup

- meaningful ClsRepositoryUseTest cases moved to ClsPsiTest
- related test data grouped under a single directory
- obsolete test data dropped
This commit is contained in:
Roman Shevchenko
2015-05-23 19:42:33 +02:00
parent 3b7a14ebf9
commit 5ef6cdb13d
96 changed files with 413 additions and 914 deletions
@@ -1,11 +0,0 @@
package pack;
public @interface Annotation2 {
float f1() default Float.NEGATIVE_INFINITY;
float f2() default Float.NaN;
float f3() default Float.POSITIVE_INFINITY;
double d1() default Double.NEGATIVE_INFINITY;
double d2() default Double.NaN;
double d3() default Double.POSITIVE_INFINITY;
}
@@ -1,23 +0,0 @@
package pack;
import java.util.ArrayList;
public class MyClass extends ArrayList implements Cloneable{
/**
* @deprecated
*/
static final int field1 = 123;
Object[] field2;
void method1(int[] p1, Object p2) throws Exception, java.io.IOException{
}
Integer method2()[]{
return null;
}
MyClass(){}
static class Inner{}
}
@@ -1,7 +0,0 @@
package pack;
/**
* @author dsl
*/
public class Parametrized<T> {
}
@@ -1,12 +0,0 @@
package pack;
import java.util.List;
/**
* @author dsl
*/
public class Variance<T> {
public Parametrized<? extends T> method() { return null; }
public <V> Parametrized<? extends V>methodWithTypeParameter() { return null; };
public void removeAll(Variance<?> parameter) {} ;
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
* Copyright 2000-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -24,93 +24,80 @@ import com.intellij.openapi.roots.ModuleRootModificationUtil;
import com.intellij.openapi.roots.OrderRootType;
import com.intellij.openapi.roots.impl.libraries.ProjectLibraryTable;
import com.intellij.openapi.roots.libraries.Library;
import com.intellij.openapi.roots.libraries.LibraryTable;
import com.intellij.openapi.util.Computable;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.JavaPsiFacade;
import com.intellij.psi.PsiClass;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.testFramework.IdeaTestCase;
import java.io.File;
import java.util.Arrays;
/**
* @author dsl
*/
public class ProjectLibrariesTest extends IdeaTestCase {
public void test() {
final LibraryTable libraryTable = ProjectLibraryTable.getInstance(myProject);
Library lib = WriteCommandAction.runWriteCommandAction(null, new Computable<Library>() {
private VirtualFile myRoot;
private Library myLib;
@Override
protected void setUp() throws Exception {
super.setUp();
myRoot = LocalFileSystem.getInstance().findFileByPath(PathManagerEx.getTestDataPath() + "/psi/cls/repo");
assertNotNull(myRoot);
myLib = WriteCommandAction.runWriteCommandAction(null, new Computable<Library>() {
@Override
public Library compute() {
return libraryTable.createLibrary("LIB");
return ProjectLibraryTable.getInstance(myProject).createLibrary("LIB");
}
});
ModuleRootModificationUtil.addDependency(myModule, lib);
final JavaPsiFacade manager = getJavaFacade();
assertNull(manager.findClass("pack.MyClass", GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(myModule)));
final File file = new File(PathManagerEx.getTestDataPath() + "/psi/repositoryUse/cls");
final VirtualFile root = WriteCommandAction.runWriteCommandAction(null, new Computable<VirtualFile>() {
@Override
public VirtualFile compute() {
return LocalFileSystem.getInstance().refreshAndFindFileByIoFile(file);
}
});
assertNotNull(root);
final Library.ModifiableModel modifyableModel = lib.getModifiableModel();
ApplicationManager.getApplication().runWriteAction(new Runnable() {
@Override
public void run() {
modifyableModel.addRoot(root, OrderRootType.CLASSES);
modifyableModel.commit();
}
});
final PsiClass aClass = manager.findClass("pack.MyClass", GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(myModule));
assertNotNull(aClass);
ModuleRootModificationUtil.addDependency(myModule, myLib);
}
public void test1() {
final LibraryTable libraryTable = ProjectLibraryTable.getInstance(myProject);
Library lib = WriteCommandAction.runWriteCommandAction(null, new Computable<Library>() {
@Override
public Library compute() {
return libraryTable.createLibrary("LIB");
}
});
ModuleRootModificationUtil.addDependency(myModule, lib);
final JavaPsiFacade manager = getJavaFacade();
assertNull(manager.findClass("pack.MyClass", GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(myModule)));
public void test() {
assertNull(getJavaFacade().findClass("pack.MyClass", GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(myModule)));
final ModifiableRootModel rootModel2 = ModuleRootManager.getInstance(myModule).getModifiableModel();
assertNotNull(rootModel2.findLibraryOrderEntry(lib));
final File file = new File(PathManagerEx.getTestDataPath() + "/psi/repositoryUse/cls");
final VirtualFile root = WriteCommandAction.runWriteCommandAction(null, new Computable<VirtualFile>() {
@Override
public VirtualFile compute() {
return LocalFileSystem.getInstance().refreshAndFindFileByIoFile(file);
}
});
assertNotNull(root);
final Library.ModifiableModel modifyableModel = lib.getModifiableModel();
final Library.ModifiableModel model = myLib.getModifiableModel();
ApplicationManager.getApplication().runWriteAction(new Runnable() {
@Override
public void run() {
modifyableModel.addRoot(root, OrderRootType.CLASSES);
modifyableModel.commit();
model.addRoot(myRoot, OrderRootType.CLASSES);
model.commit();
}
});
final PsiClass aClass = manager.findClass("pack.MyClass", GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(myModule));
assertNotNull(aClass);
assertTrue(Arrays.asList(rootModel2.orderEntries().librariesOnly().classes().getRoots()).contains(root));
ApplicationManager.getApplication().runWriteAction(new Runnable() {
@Override
public void run() {
rootModel2.commit();
}
});
final PsiClass aClass1 = manager.findClass("pack.MyClass", GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(myModule));
assertNotNull(aClass1);
assertNotNull(getJavaFacade().findClass("pack.MyClass", GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(myModule)));
}
public void testNestedModelUpdate() {
assertNull(getJavaFacade().findClass("pack.MyClass", GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(myModule)));
final ModifiableRootModel moduleModel = ModuleRootManager.getInstance(myModule).getModifiableModel();
try {
assertNotNull(moduleModel.findLibraryOrderEntry(myLib));
final Library.ModifiableModel libModel = myLib.getModifiableModel();
ApplicationManager.getApplication().runWriteAction(new Runnable() {
@Override
public void run() {
libModel.addRoot(myRoot, OrderRootType.CLASSES);
libModel.commit();
}
});
assertNotNull(getJavaFacade().findClass("pack.MyClass", GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(myModule)));
assertTrue(Arrays.asList(moduleModel.orderEntries().librariesOnly().classes().getRoots()).contains(myRoot));
}
finally {
ApplicationManager.getApplication().runWriteAction(new Runnable() {
@Override
public void run() {
moduleModel.commit();
}
});
}
assertNotNull(getJavaFacade().findClass("pack.MyClass", GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(myModule)));
}
}
@@ -47,7 +47,7 @@ public class ClsBuilderTest extends LightIdeaTestCase {
public void testAnnotatedEnumConstructor() { doTest(); }
public void testModifiers() {
String clsFilePath = JavaTestUtil.getJavaTestDataPath() + "/psi/repositoryUse/cls/pack/" + getTestName(false) + ".class";
String clsFilePath = JavaTestUtil.getJavaTestDataPath() + "/psi/cls/repo/pack/" + getTestName(false) + ".class";
VirtualFile clsFile = LocalFileSystem.getInstance().findFileByPath(clsFilePath);
assertNotNull(clsFile);
doTest(clsFile, getTestName(false) + ".txt");
@@ -16,24 +16,361 @@
package com.intellij.psi;
import com.intellij.openapi.application.ex.PathManagerEx;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.io.IoTestUtil;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.impl.compiled.ClsFileImpl;
import com.intellij.psi.impl.compiled.ClsParameterImpl;
import com.intellij.psi.impl.java.stubs.PsiMethodStub;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.util.PsiUtil;
import com.intellij.testFramework.LightIdeaTestCase;
import com.intellij.util.indexing.FileBasedIndex;
import java.io.File;
import java.io.IOException;
public class ClsPsiTest extends LightIdeaTestCase {
private static final String TEST_DATA_PATH = "/psi/cls/repo";
private PsiClass myObjectClass;
private GlobalSearchScope myScope;
@Override
protected void setUp() throws Exception {
super.setUp();
myScope = GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(getModule());
myObjectClass = getJavaFacade().findClass(CommonClassNames.JAVA_LANG_OBJECT, myScope);
assertNotNull(myObjectClass);
}
public void testClassFileUpdate() throws IOException {
File testFile = IoTestUtil.createTestFile("TestClass.class");
File file1 = new File(PathManagerEx.getTestDataPath() + TEST_DATA_PATH + "/1_TestClass.class");
FileUtil.copy(file1, testFile);
VirtualFile vFile = LocalFileSystem.getInstance().findFileByIoFile(testFile);
assertNotNull(testFile.getPath(), vFile);
FileBasedIndex.getInstance().requestReindex(vFile);
PsiFile file = PsiManager.getInstance(getProject()).findFile(vFile);
assertNotNull(file);
PsiClass aClass = ((PsiJavaFile)file).getClasses()[0];
assertTrue(aClass.isValid());
assertEquals(2, aClass.getFields().length);
assertEquals("field11", aClass.getFields()[0].getName());
assertEquals("field12", aClass.getFields()[1].getName());
assertEquals(2, aClass.getMethods().length);
assertEquals("TestClass", aClass.getMethods()[0].getName());
assertEquals("method1", aClass.getMethods()[1].getName());
File file2 = new File(PathManagerEx.getTestDataPath() + TEST_DATA_PATH + "/2_TestClass.class");
FileUtil.copy(file2, testFile);
assertTrue(testFile.setLastModified(System.currentTimeMillis() + 5000));
vFile.refresh(false, false);
aClass = ((PsiJavaFile)file).getClasses()[0];
assertTrue(aClass.isValid());
assertTrue(aClass.isValid());
assertEquals(1, aClass.getFields().length);
assertEquals("field2", aClass.getFields()[0].getName());
assertEquals(2, aClass.getMethods().length);
assertEquals("TestClass", aClass.getMethods()[0].getName());
assertEquals("method2", aClass.getMethods()[1].getName());
}
public void testFile() {
PsiJavaFile file = getFile("MyClass");
assertTrue(file.isValid());
assertEquals("pack", file.getPackageName());
assertEquals(1, file.getClasses().length);
}
public void testClassBasics() {
PsiJavaFile file = getFile("MyClass");
PsiClass aClass = file.getClasses()[0];
assertTrue(aClass.isValid());
assertEquals(file, aClass.getParent());
assertEquals(file, aClass.getContainingFile());
assertEquals("MyClass", aClass.getName());
assertEquals("pack.MyClass", aClass.getQualifiedName());
assertFalse(aClass.isInterface());
assertFalse(aClass.isDeprecated());
}
public void testClassMembers() {
PsiClass aClass = getFile("MyClass").getClasses()[0];
PsiClass[] inners = aClass.getInnerClasses();
assertEquals(1, inners.length);
assertEquals(aClass, inners[0].getParent());
PsiField[] fields = aClass.getFields();
assertEquals(2, fields.length);
assertEquals(aClass, fields[0].getParent());
PsiMethod[] methods = aClass.getMethods();
assertEquals(3, methods.length);
assertEquals(aClass, methods[0].getParent());
assertNotNull(aClass.findFieldByName("field1", false));
}
public void testModifierList() {
PsiClass aClass = getFile("MyClass").getClasses()[0];
PsiModifierList modifierList = aClass.getModifierList();
assertNotNull(modifierList);
assertTrue(modifierList.hasModifierProperty(PsiModifier.PUBLIC));
assertFalse(modifierList.hasModifierProperty(PsiModifier.STATIC));
assertEquals(modifierList.getParent(), aClass);
PsiField field = aClass.getFields()[0];
modifierList = field.getModifierList();
assertNotNull(modifierList);
assertTrue(modifierList.hasModifierProperty(PsiModifier.PACKAGE_LOCAL));
assertEquals(modifierList.getParent(), field);
PsiMethod method = aClass.getMethods()[0];
modifierList = method.getModifierList();
assertNotNull(modifierList);
assertTrue(modifierList.hasModifierProperty(PsiModifier.PACKAGE_LOCAL));
assertEquals(modifierList.getParent(), method);
}
public void testField() {
PsiClass aClass = getFile("MyClass").getClasses()[0];
PsiField field1 = aClass.getFields()[0], field2 = aClass.getFields()[1];
assertEquals("field1", field1.getName());
PsiType type1 = field1.getType();
assertEquals(PsiType.INT, type1);
assertEquals("int", type1.getPresentableText());
assertTrue(type1 instanceof PsiPrimitiveType);
PsiType type2 = field2.getType();
assertTrue(type2.equalsToText("java.lang.Object[]"));
assertEquals("Object[]", type2.getPresentableText());
assertFalse(type2 instanceof PsiPrimitiveType);
assertTrue(type2 instanceof PsiArrayType);
PsiType componentType = ((PsiArrayType)type2).getComponentType();
assertTrue(componentType.equalsToText(CommonClassNames.JAVA_LANG_OBJECT));
assertEquals(CommonClassNames.JAVA_LANG_OBJECT_SHORT, componentType.getPresentableText());
assertFalse(componentType instanceof PsiPrimitiveType);
assertFalse(componentType instanceof PsiArrayType);
PsiLiteralExpression initializer = (PsiLiteralExpression)field1.getInitializer();
assertNotNull(initializer);
assertEquals("123", initializer.getText());
assertEquals(123, initializer.getValue());
assertEquals(PsiType.INT, initializer.getType());
assertFalse(field2.hasInitializer());
assertNull(field2.getInitializer());
}
public void testMethod() {
PsiClass aClass = getFile("MyClass").getClasses()[0];
assertEquals("method1", aClass.getMethods()[0].getName());
PsiMethod method1 = aClass.getMethods()[0];
PsiTypeElement type1 = method1.getReturnTypeElement();
assertNotNull(type1);
assertEquals(method1, type1.getParent());
assertEquals("void", type1.getText());
assertTrue(type1.getType() instanceof PsiPrimitiveType);
assertFalse(method1.isConstructor());
PsiMethod method3 = aClass.getMethods()[2];
assertNull(method3.getReturnType());
assertTrue(method3.isConstructor());
}
public void testTypeReference() {
PsiClass aClass = getFile("MyClass").getClasses()[0];
PsiType type1 = aClass.getFields()[0].getType();
assertNull(PsiUtil.resolveClassInType(type1));
PsiType type2 = aClass.getFields()[1].getType();
PsiType componentType = ((PsiArrayType)type2).getComponentType();
assertTrue(componentType instanceof PsiClassType);
assertTrue(componentType.equalsToText(CommonClassNames.JAVA_LANG_OBJECT));
assertEquals(CommonClassNames.JAVA_LANG_OBJECT_SHORT, componentType.getPresentableText());
PsiElement target = PsiUtil.resolveClassInType(type2);
assertEquals(myObjectClass, target);
}
public void testReferenceLists() {
PsiClass aClass = getFile("MyClass").getClasses()[0];
PsiReferenceList extList = aClass.getExtendsList();
assertNotNull(extList);
PsiClassType[] refs = extList.getReferencedTypes();
assertEquals(1, refs.length);
assertEquals("ArrayList", refs[0].getPresentableText());
assertEquals("java.util.ArrayList", refs[0].getCanonicalText());
PsiReferenceList implList = aClass.getImplementsList();
assertNotNull(implList);
PsiClassType[] implRefs = implList.getReferencedTypes();
assertEquals(1, implRefs.length);
assertEquals("Cloneable", implRefs[0].getPresentableText());
assertEquals("java.lang.Cloneable", implRefs[0].getCanonicalText());
PsiReferenceList throwsList = aClass.getMethods()[0].getThrowsList();
PsiClassType[] throwsRefs = throwsList.getReferencedTypes();
assertEquals(2, throwsRefs.length);
assertEquals("Exception", throwsRefs[0].getPresentableText());
assertEquals("java.lang.Exception", throwsRefs[0].getCanonicalText());
assertEquals("IOException", throwsRefs[1].getPresentableText());
assertEquals("java.io.IOException", throwsRefs[1].getCanonicalText());
}
public void testParameters() {
PsiClass aClass = getFile("MyClass").getClasses()[0];
PsiParameter[] parameters = aClass.getMethods()[0].getParameterList().getParameters();
assertEquals(2, parameters.length);
PsiType type1 = parameters[0].getType();
assertEquals("int[]", type1.getPresentableText());
assertTrue(type1.equalsToText("int[]"));
assertTrue(type1 instanceof PsiArrayType);
assertNull(PsiUtil.resolveClassInType(type1));
PsiType type2 = parameters[1].getType();
assertEquals("Object", type2.getPresentableText());
assertTrue(type2.equalsToText(CommonClassNames.JAVA_LANG_OBJECT));
assertFalse(type2 instanceof PsiArrayType);
assertFalse(type2 instanceof PsiPrimitiveType);
PsiClass target2 = PsiUtil.resolveClassInType(type2);
assertEquals(myObjectClass, target2);
assertNotNull(parameters[0].getModifierList());
assertNotNull(parameters[1].getModifierList());
assertTrue(((ClsParameterImpl)parameters[0]).isAutoGeneratedName());
assertTrue(((ClsParameterImpl)parameters[1]).isAutoGeneratedName());
assertEquals("ints", parameters[0].getName());
assertEquals("o", parameters[1].getName());
}
@SuppressWarnings("ConstantConditions")
public void testModifiers() {
PsiClass aClass = getFile().getClasses()[0];
assertEquals("private transient", aClass.getFields()[0].getModifierList().getText());
assertEquals("private volatile", aClass.getFields()[1].getModifierList().getText());
assertEquals("public", aClass.getMethods()[0].getModifierList().getText());
assertEquals("private", aClass.getMethods()[1].getModifierList().getText());
assertEquals("private synchronized", aClass.getMethods()[2].getModifierList().getText());
}
public void testEnum() {
PsiClass aClass = getFile("MyEnum").getClasses()[0];
assertTrue(aClass.isEnum());
PsiField[] fields = aClass.getFields();
PsiClassType type = getJavaFacade().getElementFactory().createType(aClass);
assertEnumConstant(fields[0], "RED", type);
assertEnumConstant(fields[1], "GREEN", type);
assertEnumConstant(fields[2], "BLUE", type);
}
private static void assertEnumConstant(PsiField field, String name, PsiClassType type) {
assertEquals(name, field.getName());
assertTrue(field instanceof PsiEnumConstant);
assertEquals(type, field.getType());
}
public void testAnnotations() {
PsiClass aClass = getFile("Annotated").getClasses()[0];
PsiModifierList modifierList = aClass.getModifierList();
assertNotNull(modifierList);
PsiAnnotation[] annotations = modifierList.getAnnotations();
assertEquals(1, annotations.length);
assertTrue(annotations[0].getText().equals("@pack.Annotation"));
PsiMethod method = aClass.getMethods()[1];
annotations = method.getModifierList().getAnnotations();
assertEquals(1, annotations.length);
assertTrue(annotations[0].getText().equals("@pack.Annotation"));
PsiParameter[] params = method.getParameterList().getParameters();
assertEquals(1, params.length);
modifierList = params[0].getModifierList();
assertNotNull(modifierList);
annotations = modifierList.getAnnotations();
assertEquals(1, annotations.length);
assertTrue(annotations[0].getText().equals("@pack.Annotation"));
modifierList = aClass.getFields()[0].getModifierList();
assertNotNull(modifierList);
annotations = modifierList.getAnnotations();
assertEquals(1, annotations.length);
assertTrue(annotations[0].getText().equals("@pack.Annotation"));
}
public void testAnnotationMethods() {
PsiMethod[] methods = getFile("Annotation").getClasses()[0].getMethods();
assertNotNull(((PsiAnnotationMethod)methods[0]).getDefaultValue());
methods = getFile("Annotation2").getClasses()[0].getMethods();
for (PsiMethod method : methods) {
assertTrue(String.valueOf(method), method instanceof PsiAnnotationMethod);
try {
PsiAnnotationMemberValue defaultValue = ((PsiAnnotationMethod)method).getDefaultValue();
assertTrue(String.valueOf(defaultValue), defaultValue instanceof PsiBinaryExpression);
PsiPrimitiveType type = method.getName().startsWith("f") ? PsiType.FLOAT : PsiType.DOUBLE;
assertEquals(type, ((PsiBinaryExpression)defaultValue).getType());
}
catch (Exception e) {
String valueText = ((PsiMethodStub)((StubBasedPsiElement)method).getStub()).getDefaultValueText();
fail("Unable to compute default value of method " + method + " from text '" + valueText + "': " + e.getMessage());
}
}
}
public void testKeywordAnnotatedClass() {
PsiModifierList modList = getFile().getClasses()[0].getModifierList();
PsiModifierList modList = getFile("../KeywordAnnotatedClass").getClasses()[0].getModifierList();
assertNotNull(modList);
assertEquals("@pkg.native", modList.getAnnotations()[0].getText());
}
public void testGenerics() {
PsiClass aClass = getJavaFacade().findClass(CommonClassNames.JAVA_UTIL_HASH_MAP, myScope);
assertNotNull(aClass);
PsiTypeParameter[] parameters = aClass.getTypeParameters();
assertEquals(2, parameters.length);
assertEquals("K", parameters[0].getName());
assertEquals("V", parameters[1].getName());
PsiClassType extType = aClass.getExtendsListTypes()[0];
assertEquals("java.util.AbstractMap<K,V>", extType.getCanonicalText());
PsiType returnType = aClass.findMethodsByName("entrySet", false)[0].getReturnType();
assertNotNull(returnType);
assertEquals("java.util.Set<java.util.Map.Entry<K,V>>", returnType.getCanonicalText());
PsiType paramType = aClass.findMethodsByName("putAll", false)[0].getParameterList().getParameters()[0].getType();
assertNotNull(paramType);
assertEquals("java.util.Map<? extends K,? extends V>", paramType.getCanonicalText());
assertNotNull(((PsiClassType)paramType).resolveGenerics().getElement());
}
private PsiJavaFile getFile() {
return getFile(getTestName(false));
}
private static PsiJavaFile getFile(String name) {
String path = PathManagerEx.getTestDataPath() + "/psi/cls/tree/" + name + ".class";
String path = PathManagerEx.getTestDataPath() + TEST_DATA_PATH + "/pack/" + name + ".class";
VirtualFile file = LocalFileSystem.getInstance().findFileByPath(path);
assertNotNull(path, file);
PsiFile clsFile = PsiManager.getInstance(getProject()).findFile(file);
@@ -1,759 +0,0 @@
/*
* Copyright 2000-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.psi;
import com.intellij.ide.highlighter.JavaFileType;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ex.PathManagerEx;
import com.intellij.openapi.roots.ModuleRootModificationUtil;
import com.intellij.openapi.roots.OrderRootType;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileFilter;
import com.intellij.psi.impl.compiled.ClsParameterImpl;
import com.intellij.psi.impl.java.stubs.PsiMethodStub;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.util.PsiUtil;
import com.intellij.testFramework.IdeaTestUtil;
import com.intellij.testFramework.PlatformTestCase;
import com.intellij.testFramework.PsiTestCase;
import com.intellij.testFramework.PsiTestUtil;
import java.io.File;
import java.io.IOException;
@PlatformTestCase.WrapInCommand
public class ClsRepositoryUseTest extends PsiTestCase {
private static final String TEST_ROOT = PathManagerEx.getTestDataPath() + "/psi/repositoryUse/cls";
private GlobalSearchScope RESOLVE_SCOPE;
@Override
protected void setUp() throws Exception {
super.setUp();
ApplicationManager.getApplication().runWriteAction(
new Runnable() {
@Override
public void run() {
VirtualFile vDir = getRootFile();
PsiTestUtil.removeAllRoots(myModule, IdeaTestUtil.getMockJdk17());
addLibraryToRoots(vDir, OrderRootType.CLASSES);
}
}
);
RESOLVE_SCOPE = GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(myModule);
}
@Override
protected void tearDown() throws Exception {
RESOLVE_SCOPE = null;
super.tearDown();
}
public void testClassFileUpdate() throws IOException {
final File classes = createTempDir("classes", false);
final File com = new File(classes, "com");
FileUtil.ensureExists(com);
File dataPath = new File(PathManagerEx.getTestDataPath() + "/psi/cls");
final File target = new File(com, "TestClass.class");
FileUtil.copy(new File(dataPath, "1/TestClass.class"), target);
assertTrue(target.setLastModified(System.currentTimeMillis()));
ApplicationManager.getApplication().runWriteAction(
new Runnable() {
@Override
public void run() {
VirtualFile vDir = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(classes);
assertNotNull(vDir);
addLibraryToRoots(vDir, OrderRootType.CLASSES);
}
}
);
PsiClass psiClass = myJavaFacade.findClass("com.TestClass", GlobalSearchScope.allScope(myProject));
assert psiClass != null;
PsiJavaFile psiFile = (PsiJavaFile)psiClass.getContainingFile();
final VirtualFile vFile = psiFile.getVirtualFile();
assertNotNull(psiClass);
assertTrue(psiClass.isValid());
assertTrue(psiFile.isValid());
assertEquals(1, psiFile.getClasses().length);
assertSame(psiClass, psiFile.getClasses()[0]);
assertEquals(2, psiClass.getFields().length);
assertEquals("field11", psiClass.getFields()[0].getName());
assertEquals("field12", psiClass.getFields()[1].getName());
assertEquals(2, psiClass.getMethods().length);
assertEquals("TestClass", psiClass.getMethods()[0].getName());
assertEquals("method1", psiClass.getMethods()[1].getName());
FileUtil.copy(new File(dataPath, "2/TestClass.class"), target);
assertTrue(target.setLastModified(System.currentTimeMillis() + 5000));
assert vFile != null;
ApplicationManager.getApplication().runWriteAction(
new Runnable() {
@Override
public void run() {
vFile.refresh(false, false);
}
}
);
assertTrue(psiFile.isValid());
PsiClass oldClass = psiClass;
psiClass = myJavaFacade.findClass("com.TestClass", GlobalSearchScope.allScope(myProject));
assertNotSame(oldClass, psiClass);
assertFalse(oldClass.isValid());
assertNotNull(psiClass);
assertEquals(1, psiFile.getClasses().length);
assertSame(psiClass, psiFile.getClasses()[0]);
assertTrue(psiClass.isValid());
assertEquals(1, psiClass.getFields().length);
assertEquals("field2", psiClass.getFields()[0].getName());
assertEquals(2, psiClass.getMethods().length);
assertEquals("TestClass", psiClass.getMethods()[0].getName());
assertEquals("method2", psiClass.getMethods()[1].getName());
}
private static VirtualFile getRootFile() {
final String path = TEST_ROOT.replace(File.separatorChar, '/');
final VirtualFile vDir = LocalFileSystem.getInstance().findFileByPath(path);
assert vDir != null : path;
return vDir;
}
public void testGetClasses() {
final VirtualFile rootFile = getRootFile();
final VirtualFile pack = rootFile.findChild("pack");
assert pack != null;
VirtualFile child = pack.findChild("MyClass.class");
assertNotNull(child);
PsiJavaFile file = (PsiJavaFile)myPsiManager.findFile(child);
checkValid(file);
assert file != null;
PsiClass[] classes = file.getClasses();
assertTrue(classes.length == 1);
PsiClass aClass = classes[0];
checkValid(aClass);
assertEquals(file, aClass.getParent());
}
public void testGetClassName() {
final VirtualFile rootFile = getRootFile();
final VirtualFile pack = rootFile.findChild("pack");
assert pack != null;
VirtualFile child = pack.findChild("MyClass.class");
assertNotNull(child);
PsiJavaFile file = (PsiJavaFile)myPsiManager.findFile(child);
assert file != null;
PsiClass[] classes = file.getClasses();
assertTrue(classes.length == 1);
PsiClass aClass = classes[0];
checkValid(aClass);
assertEquals("MyClass", aClass.getName());
}
public void testGetClassQName() {
final VirtualFile rootFile = getRootFile();
final VirtualFile pack = rootFile.findChild("pack");
assert pack != null;
VirtualFile child = pack.findChild("MyClass.class");
assertNotNull(child);
PsiJavaFile file = (PsiJavaFile)myPsiManager.findFile(child);
assert file != null;
PsiClass[] classes = file.getClasses();
assertTrue(classes.length == 1);
PsiClass aClass = classes[0];
checkValid(aClass);
assertEquals("pack.MyClass", aClass.getQualifiedName());
}
public void testGetContainingFile() {
final VirtualFile rootFile = getRootFile();
final VirtualFile pack = rootFile.findChild("pack");
assert pack != null;
VirtualFile child = pack.findChild("MyClass.class");
assertNotNull(child);
PsiJavaFile file = (PsiJavaFile)myPsiManager.findFile(child);
assert file != null;
PsiClass[] classes = file.getClasses();
assertTrue(classes.length == 1);
PsiClass aClass = classes[0];
checkValid(aClass);
assertEquals(file, aClass.getContainingFile());
}
public void testFindClass() {
getJavaFacade().setAssertOnFileLoadingFilter(VirtualFileFilter.ALL, myTestRootDisposable);
PsiClass aClass = myJavaFacade.findClass("pack.MyClass", GlobalSearchScope.allScope(myProject));
assertNotNull(aClass);
checkValid(aClass);
}
public void testIsInterface() {
PsiClass aClass = myJavaFacade.findClass("pack.MyClass", GlobalSearchScope.allScope(myProject));
assert aClass != null;
checkValid(aClass);
assertTrue(!aClass.isInterface());
}
private static void checkValid(final PsiElement elt) {
assertTrue(elt.isValid());
}
public void testPackageName() {
PsiClass aClass = myJavaFacade.findClass("pack.MyClass", GlobalSearchScope.allScope(myProject));
assert aClass != null;
checkValid(aClass);
String packageName = ((PsiJavaFile)aClass.getContainingFile()).getPackageName();
assertEquals("pack", packageName);
}
public void testGetFields() {
PsiClass aClass = myJavaFacade.findClass("pack.MyClass", GlobalSearchScope.allScope(myProject));
assert aClass != null;
checkValid(aClass);
PsiField[] fields = aClass.getFields();
assertEquals(2, fields.length);
assertEquals(aClass, fields[0].getParent());
}
public void testGetMethods() {
PsiClass aClass = myJavaFacade.findClass("pack.MyClass", GlobalSearchScope.allScope(myProject));
assert aClass != null;
checkValid(aClass);
PsiMethod[] methods = aClass.getMethods();
assertEquals(3, methods.length);
assertEquals(aClass, methods[0].getParent());
}
public void testGetInnerClasses() {
PsiClass aClass = myJavaFacade.findClass("pack.MyClass", GlobalSearchScope.allScope(myProject));
assert aClass != null;
checkValid(aClass);
PsiClass[] inners = aClass.getInnerClasses();
assertEquals(1, inners.length);
assertEquals(aClass, inners[0].getParent());
}
public void testModifierList() {
PsiClass aClass = myJavaFacade.findClass("pack.MyClass", GlobalSearchScope.allScope(myProject));
assert aClass != null;
checkValid(aClass);
PsiModifierList modifierList = aClass.getModifierList();
assert modifierList != null : aClass;
assertTrue(modifierList.hasModifierProperty(PsiModifier.PUBLIC));
assertFalse(modifierList.hasModifierProperty(PsiModifier.STATIC));
assertEquals(modifierList.getParent(), aClass);
PsiField field = aClass.getFields()[0];
modifierList = field.getModifierList();
assert modifierList != null : field;
assertTrue(modifierList.hasModifierProperty(PsiModifier.PACKAGE_LOCAL));
assertEquals(modifierList.getParent(), field);
PsiMethod method = aClass.getMethods()[0];
modifierList = method.getModifierList();
assertTrue(modifierList.hasModifierProperty(PsiModifier.PACKAGE_LOCAL));
assertEquals(modifierList.getParent(), method);
}
public void testGetFieldName() {
PsiClass aClass = myJavaFacade.findClass("pack.MyClass", GlobalSearchScope.allScope(myProject));
assert aClass != null;
checkValid(aClass);
assertEquals("field1", aClass.getFields()[0].getName());
}
public void testGetMethodName() {
PsiClass aClass = myJavaFacade.findClass("pack.MyClass", GlobalSearchScope.allScope(myProject));
assert aClass != null;
checkValid(aClass);
assertEquals("method1", aClass.getMethods()[0].getName());
}
public void testFindFieldByName() {
PsiClass aClass = myJavaFacade.findClass("pack.MyClass", GlobalSearchScope.allScope(myProject));
assert aClass != null;
checkValid(aClass);
PsiField field = aClass.findFieldByName("field1", false);
assertNotNull(field);
}
public void testIsDeprecated() {
PsiClass aClass = myJavaFacade.findClass("pack.MyClass", GlobalSearchScope.allScope(myProject));
assert aClass != null;
checkValid(aClass);
assertTrue(!aClass.isDeprecated());
PsiField field = aClass.findFieldByName("field1", false);
assert field != null : aClass;
assertTrue(field.isDeprecated());
}
public void testFieldType() {
PsiClass aClass = myJavaFacade.findClass("pack.MyClass", GlobalSearchScope.allScope(myProject));
assert aClass != null;
checkValid(aClass);
PsiField field1 = aClass.getFields()[0];
PsiType type1 = field1.getType();
PsiField field2 = aClass.getFields()[1];
PsiType type2 = field2.getType();
assertEquals(PsiType.INT, type1);
assertTrue(type2.equalsToText("java.lang.Object[]"));
assertEquals("int", type1.getPresentableText());
assertEquals("Object[]", type2.getPresentableText());
assertTrue(type1 instanceof PsiPrimitiveType);
assertTrue(!(type2 instanceof PsiPrimitiveType));
assertTrue(type2 instanceof PsiArrayType);
}
public void testMethodType() {
PsiClass aClass = myJavaFacade.findClass("pack.MyClass", GlobalSearchScope.allScope(myProject));
assert aClass != null;
checkValid(aClass);
PsiMethod method1 = aClass.getMethods()[0];
PsiTypeElement type1 = method1.getReturnTypeElement();
assert type1 != null : method1;
assertEquals(method1, type1.getParent());
assertEquals("void", type1.getText());
assertTrue(type1.getType() instanceof PsiPrimitiveType);
PsiMethod method3 = aClass.getMethods()[2];
assertNull(method3.getReturnType());
}
public void testIsConstructor() {
PsiClass aClass = myJavaFacade.findClass("pack.MyClass", GlobalSearchScope.allScope(myProject));
assert aClass != null;
checkValid(aClass);
PsiMethod method1 = aClass.getMethods()[0];
assertFalse(method1.isConstructor());
PsiMethod method3 = aClass.getMethods()[2];
assertTrue(method3.isConstructor());
}
public void testComponentType() {
PsiClass aClass = myJavaFacade.findClass("pack.MyClass", GlobalSearchScope.allScope(myProject));
assert aClass != null;
checkValid(aClass);
PsiField field = aClass.getFields()[1];
PsiType type = field.getType();
assertTrue(type instanceof PsiArrayType);
PsiType componentType = ((PsiArrayType)type).getComponentType();
assertTrue(componentType.equalsToText(CommonClassNames.JAVA_LANG_OBJECT));
assertEquals("Object", componentType.getPresentableText());
assertFalse(componentType instanceof PsiPrimitiveType);
assertFalse(componentType instanceof PsiArrayType);
}
public void testTypeReference() {
PsiClass aClass = myJavaFacade.findClass("pack.MyClass", GlobalSearchScope.allScope(myProject));
assert aClass != null;
checkValid(aClass);
PsiField field1 = aClass.getFields()[0];
PsiType type1 = field1.getType();
assertNull(PsiUtil.resolveClassInType(type1));
PsiField field2 = aClass.getFields()[1];
PsiType type2 = ((PsiArrayType)field2.getType()).getComponentType();
assertTrue(type2 instanceof PsiClassType);
assertTrue(type2.equalsToText(CommonClassNames.JAVA_LANG_OBJECT));
assertEquals("Object", type2.getPresentableText());
}
public void testResolveTypeReference() {
PsiClass aClass = myJavaFacade.findClass("pack.MyClass", GlobalSearchScope.allScope(myProject));
assert aClass != null;
checkValid(aClass);
PsiType type1 = aClass.getFields()[1].getType();
PsiElement target1 = PsiUtil.resolveClassInType(type1);
assertNotNull(target1);
PsiClass objectClass = myJavaFacade.findClasses(CommonClassNames.JAVA_LANG_OBJECT, RESOLVE_SCOPE)[1];
assertEquals(objectClass, target1);
}
public void testInitializer() {
PsiClass aClass = myJavaFacade.findClass("pack.MyClass", GlobalSearchScope.allScope(myProject));
assert aClass != null;
checkValid(aClass);
PsiField field1 = aClass.getFields()[0];
assertTrue(field1.hasInitializer());
PsiLiteralExpression initializer = (PsiLiteralExpression)field1.getInitializer();
assert initializer != null : field1;
assertEquals("123", initializer.getText());
assertEquals(new Integer(123), initializer.getValue());
assertEquals(PsiType.INT, initializer.getType());
PsiField field2 = aClass.getFields()[1];
assertFalse(field2.hasInitializer());
assertNull(field2.getInitializer());
}
public void testExtendsList() {
PsiClass aClass = myJavaFacade.findClass("pack.MyClass", GlobalSearchScope.allScope(myProject));
assert aClass != null;
checkValid(aClass);
PsiReferenceList list = aClass.getExtendsList();
assert list != null : aClass;
PsiClassType[] refs = list.getReferencedTypes();
assertEquals(1, refs.length);
assertEquals("ArrayList", refs[0].getPresentableText());
assertEquals("java.util.ArrayList", refs[0].getCanonicalText());
}
public void testImplementsList() {
PsiClass aClass = myJavaFacade.findClass("pack.MyClass", GlobalSearchScope.allScope(myProject));
assert aClass != null;
checkValid(aClass);
PsiReferenceList list = aClass.getImplementsList();
assert list != null : aClass;
PsiClassType[] refs = list.getReferencedTypes();
assertEquals(1, refs.length);
assertEquals("Cloneable", refs[0].getPresentableText());
assertEquals("java.lang.Cloneable", refs[0].getCanonicalText());
}
public void testThrowsList() {
PsiClass aClass = myJavaFacade.findClass("pack.MyClass", GlobalSearchScope.allScope(myProject));
assert aClass != null;
checkValid(aClass);
PsiReferenceList list = aClass.getMethods()[0].getThrowsList();
PsiClassType[] refs = list.getReferencedTypes();
assertEquals(2, refs.length);
assertEquals("Exception", refs[0].getPresentableText());
assertEquals("IOException", refs[1].getPresentableText());
assertEquals("java.lang.Exception", refs[0].getCanonicalText());
assertEquals("java.io.IOException", refs[1].getCanonicalText());
}
public void testParameters() {
PsiClass aClass = myJavaFacade.findClass("pack.MyClass", GlobalSearchScope.allScope(myProject));
assert aClass != null;
checkValid(aClass);
PsiParameterList list = aClass.getMethods()[0].getParameterList();
PsiParameter[] parameters = list.getParameters();
assertEquals(2, parameters.length);
PsiType type1 = parameters[0].getType();
assertEquals("int[]", type1.getPresentableText());
assertTrue(type1.equalsToText("int[]"));
assertTrue(type1 instanceof PsiArrayType);
assertNull(PsiUtil.resolveClassInType(type1));
PsiType type2 = parameters[1].getType();
assertEquals("Object", type2.getPresentableText());
assertTrue(type2.equalsToText(CommonClassNames.JAVA_LANG_OBJECT));
assertFalse(type2 instanceof PsiArrayType);
assertFalse(type2 instanceof PsiPrimitiveType);
PsiClass target2 = PsiUtil.resolveClassInType(type2);
assertNotNull(target2);
PsiClass objectClass = myJavaFacade.findClasses(CommonClassNames.JAVA_LANG_OBJECT, RESOLVE_SCOPE)[1];
assertEquals(objectClass, target2);
assertNotNull(parameters[0].getModifierList());
assertNotNull(parameters[1].getModifierList());
assertTrue(((ClsParameterImpl)parameters[0]).isAutoGeneratedName());
assertTrue(((ClsParameterImpl)parameters[1]).isAutoGeneratedName());
assertEquals("ints", parameters[0].getName());
assertEquals("o", parameters[1].getName());
}
public void testGenericClass() {
disableJdk();
PsiClass map = myJavaFacade.findClass("java.util.HashMap", RESOLVE_SCOPE);
assert map != null;
PsiMethod entrySet = map.findMethodsByName("entrySet", false)[0];
PsiClassType ret = (PsiClassType)entrySet.getReturnType();
assert ret != null : entrySet;
final PsiClassType.ClassResolveResult setResolveResult = ret.resolveGenerics();
final PsiClass setResolveResultElement = setResolveResult.getElement();
assert setResolveResultElement != null : setResolveResult;
assertEquals(CommonClassNames.JAVA_UTIL_SET, setResolveResultElement.getQualifiedName());
final PsiTypeParameter typeParameter = setResolveResultElement.getTypeParameters()[0];
final PsiType substitutedResult = setResolveResult.getSubstitutor().substitute(typeParameter);
assertTrue(substitutedResult instanceof PsiWildcardType);
assertTrue(((PsiWildcardType)substitutedResult).isExtends());
PsiClassType setType = (PsiClassType)((PsiWildcardType)substitutedResult).getBound();
assert setType != null;
final PsiClassType.ClassResolveResult setTypeResolveResult = setType.resolveGenerics();
final PsiClass setTypeResolveResultElement = setTypeResolveResult.getElement();
assert setTypeResolveResultElement != null : setTypeResolveResult;
assertEquals("java.util.Map.Entry", setTypeResolveResultElement.getQualifiedName());
final PsiTypeParameter[] typeParameters = setTypeResolveResultElement.getTypeParameters();
assertEquals(2, typeParameters.length);
PsiType[] mapParams = {
setTypeResolveResult.getSubstitutor().substitute(typeParameters[0]),
setTypeResolveResult.getSubstitutor().substitute(typeParameters[1])
};
assertEquals(2, mapParams.length);
assertEquals("K", mapParams[0].getCanonicalText());
assertTrue(((PsiClassType)mapParams[0]).resolve() instanceof PsiTypeParameter);
assertEquals("V", mapParams[1].getCanonicalText());
assertTrue(((PsiClassType)mapParams[1]).resolve() instanceof PsiTypeParameter);
}
private void disableJdk() {
ModuleRootModificationUtil.setModuleSdk(myModule, null);
}
public void testGenericReturnType() {
disableJdk();
final PsiClass map = myJavaFacade.findClass(CommonClassNames.JAVA_UTIL_MAP, RESOLVE_SCOPE);
assert map != null;
final PsiElementFactory factory = myJavaFacade.getElementFactory();
final PsiClassType typeMapStringToInteger =
(PsiClassType)factory.createTypeFromText("java.util.Map <java.lang.String,java.lang.Integer>", null);
final PsiClassType.ClassResolveResult mapResolveResult = typeMapStringToInteger.resolveGenerics();
final PsiClass mapResolveResultElement = mapResolveResult.getElement();
assert mapResolveResultElement != null : typeMapStringToInteger;
assertTrue(mapResolveResultElement.equals(map));
final PsiMethod entrySetMethod = map.findMethodsByName("entrySet", false)[0];
final PsiType entrySetReturnType = entrySetMethod.getReturnType();
assert entrySetReturnType != null : entrySetMethod;
assertEquals("java.util.Set<? extends java.util.Map.Entry<K,V>>", entrySetReturnType.getCanonicalText());
final PsiSubstitutor substitutor = ((PsiClassType)entrySetReturnType).resolveGenerics().getSubstitutor();
assertEquals("E of java.util.Set -> ? extends java.util.Map.Entry<K,V>\n", substitutor.toString());
final PsiType typeSetOfEntriesOfStringAndInteger =
factory.createTypeFromText("java.util.Set<? extends java.util.Map.Entry<java.lang.String,java.lang.Integer>>", null);
final PsiType substitutedEntrySetReturnType = mapResolveResult.getSubstitutor().substitute(entrySetReturnType);
assertTrue(typeSetOfEntriesOfStringAndInteger.equals(substitutedEntrySetReturnType));
assertTrue(typeSetOfEntriesOfStringAndInteger.isAssignableFrom(substitutedEntrySetReturnType));
}
public void testGenericInheritance() {
final String text = "import java.util.*;\n" +
"class Dummy {\n" +
" { Map<Integer, Integer> list = new HashMap<Integer, Integer>();}\n" +
"}";
PsiJavaFile file = (PsiJavaFile)PsiFileFactory.getInstance(myProject).createFileFromText("Dummy.java", JavaFileType.INSTANCE, text);
PsiDeclarationStatement decl = (PsiDeclarationStatement)file.getClasses()[0].getInitializers()[0].getBody().getStatements()[0];
PsiVariable list = (PsiVariable)decl.getDeclaredElements()[0];
final PsiExpression initializer = list.getInitializer();
assert initializer != null : list;
final PsiType type = initializer.getType();
assert type != null : initializer;
assertTrue(list.getType().isAssignableFrom(type));
}
public void testSimplerGenericInheritance() {
PsiElementFactory factory = myJavaFacade.getElementFactory();
PsiClass map = myJavaFacade.findClass(CommonClassNames.JAVA_UTIL_MAP, RESOLVE_SCOPE);
PsiClass hashMap = myJavaFacade.findClass("java.util.HashMap", RESOLVE_SCOPE);
assert map != null && hashMap != null;
assertTrue(factory.createType(map).isAssignableFrom(factory.createType(hashMap)));
}
public void testAnnotations() {
PsiClass aClass = myJavaFacade.findClass("pack.Annotated", GlobalSearchScope.allScope(myProject));
assert aClass != null;
checkValid(aClass);
PsiModifierList modifierList = aClass.getModifierList();
assert modifierList != null : aClass;
PsiAnnotation[] annotations = modifierList.getAnnotations();
assertTrue(annotations.length == 1);
assertTrue(annotations[0].getText().equals("@pack.Annotation"));
PsiMethod[] methods = aClass.findMethodsByName("foo", false);
assertTrue(methods.length == 1);
annotations = methods[0].getModifierList().getAnnotations();
assertTrue(annotations.length == 1);
assertTrue(annotations[0].getText().equals("@pack.Annotation"));
PsiParameter[] params = methods[0].getParameterList().getParameters();
assertTrue(params.length == 1);
modifierList = params[0].getModifierList();
assert modifierList != null : params[0];
annotations = modifierList.getAnnotations();
assertTrue(annotations.length == 1);
assertTrue(annotations[0].getText().equals("@pack.Annotation"));
PsiField[] fields = aClass.getFields();
assertTrue(fields.length == 1);
modifierList = fields[0].getModifierList();
assert modifierList != null : fields[0];
annotations = modifierList.getAnnotations();
assertTrue(annotations.length == 1);
assertTrue(annotations[0].getText().equals("@pack.Annotation"));
}
public void testAnnotationMethodDefault() {
PsiClass aClass = myJavaFacade.findClass("pack.Annotation", GlobalSearchScope.allScope(myProject));
assert aClass != null;
checkValid(aClass);
PsiMethod[] methods = aClass.findMethodsByName("value", false);
assertTrue(methods.length == 1 && methods[0] instanceof PsiAnnotationMethod);
PsiAnnotationMemberValue defaultValue = ((PsiAnnotationMethod)methods[0]).getDefaultValue();
assertTrue(defaultValue != null);
}
public void testIndeterminateInAnnotationMethodDefault() {
final PsiClass aClass = myJavaFacade.findClass("pack.Annotation2", GlobalSearchScope.allScope(myProject));
assert aClass != null;
checkValid(aClass);
for (PsiMethod method : aClass.getMethods()) {
assert method instanceof PsiAnnotationMethod : method;
try {
final PsiAnnotationMemberValue defaultValue = ((PsiAnnotationMethod)method).getDefaultValue();
assert defaultValue instanceof PsiBinaryExpression : defaultValue;
final PsiPrimitiveType type = method.getName().startsWith("f") ? PsiType.FLOAT : PsiType.DOUBLE;
assertEquals(type, ((PsiBinaryExpression)defaultValue).getType());
}
catch (Exception e) {
final String valueText = ((PsiMethodStub)((StubBasedPsiElement)method).getStub()).getDefaultValueText();
fail("Unable to compute default value of method " + method + " from text '" + valueText + "': " + e.getMessage());
}
}
}
public void testEnum() {
PsiClass aClass = myJavaFacade.findClass("pack.MyEnum", GlobalSearchScope.allScope(myProject));
assert aClass != null;
checkValid(aClass);
assertTrue(aClass.isEnum());
PsiField[] fields = aClass.getFields();
PsiClassType type = myJavaFacade.getElementFactory().createType(aClass);
checkEnumConstant("RED", fields[0], type);
checkEnumConstant("GREEN", fields[1], type);
checkEnumConstant("BLUE", fields[2], type);
}
public void testVariance() {
final PsiClass aClass = myJavaFacade.findClass("pack.Variance", RESOLVE_SCOPE);
assert aClass != null;
checkValid(aClass);
final PsiMethod[] methodsByName = aClass.findMethodsByName("method", false);
assertEquals(1, methodsByName.length);
final PsiMethod methodsWithReturnType = methodsByName[0];
final PsiType returnType = methodsWithReturnType.getReturnType();
assert returnType != null : methodsWithReturnType;
assertEquals("pack.Parametrized<? extends T>", returnType.getCanonicalText());
}
private static void checkEnumConstant(String name, PsiField field, PsiClassType type) {
assertEquals(name, field.getName());
assertTrue(field instanceof PsiEnumConstant);
assertEquals(type, field.getType());
}
public void testInvariantParsing() {
final PsiClass collection = myJavaFacade.findClass("pack.Variance", RESOLVE_SCOPE);
assertNotNull(collection);
final PsiMethod[] methods = collection.findMethodsByName("removeAll", false);
assertEquals(1, methods.length);
final PsiParameter[] parameters = methods[0].getParameterList().getParameters();
assertEquals(1, parameters.length);
final PsiType parameterType = parameters[0].getType();
assertTrue(parameterType instanceof PsiClassType);
final PsiClassType psiClassType = ((PsiClassType)parameterType);
final PsiClassType.ClassResolveResult resolveResult = psiClassType.resolveGenerics();
final PsiClass resolveResultElement = resolveResult.getElement();
assert resolveResultElement != null : psiClassType;
final PsiTypeParameter[] typeParameters = resolveResultElement.getTypeParameters();
assertEquals(1, typeParameters.length);
final PsiType substitution = resolveResult.getSubstitutor().substitute(typeParameters[0]);
assertTrue(substitution instanceof PsiWildcardType);
assertEquals(PsiWildcardType.createUnbounded(myPsiManager), substitution);
}
public void testInvariantParsing2() {
disableJdk();
final PsiClass collection = myJavaFacade.findClass("java.util.Collection", RESOLVE_SCOPE);
assertNotNull(collection);
final PsiMethod[] methods = collection.findMethodsByName("removeAll", false);
assertEquals(1, methods.length);
final PsiParameter[] parameters = methods[0].getParameterList().getParameters();
assertEquals(1, parameters.length);
final PsiType parameterType = parameters[0].getType();
assertTrue(parameterType instanceof PsiClassType);
final PsiClassType psiClassType = ((PsiClassType)parameterType);
final PsiClassType.ClassResolveResult resolveResult = psiClassType.resolveGenerics();
final PsiClass resolveResultElement = resolveResult.getElement();
assert resolveResultElement != null : psiClassType;
final PsiTypeParameter[] typeParameters = resolveResultElement.getTypeParameters();
assertEquals(1, typeParameters.length);
final PsiType substitution = resolveResult.getSubstitutor().substitute(typeParameters[0]);
assertTrue(substitution instanceof PsiWildcardType);
assertEquals(PsiWildcardType.createUnbounded(myPsiManager), substitution);
}
@SuppressWarnings("ConstantConditions")
public void testModifiers() {
PsiClass psiClass = myJavaFacade.findClass("pack.Modifiers", RESOLVE_SCOPE);
assertNotNull(psiClass);
PsiField f1 = psiClass.findFieldByName("f1", false);
assertEquals("private transient", f1.getModifierList().getText());
PsiField f2 = psiClass.findFieldByName("f2", false);
assertEquals("private volatile", f2.getModifierList().getText());
PsiMethod init = psiClass.findMethodsByName("Modifiers", false)[0];
assertEquals("public", init.getModifierList().getText());
PsiMethod m1 = psiClass.findMethodsByName("m1", false)[0];
assertEquals("private", m1.getModifierList().getText());
PsiMethod m2 = psiClass.findMethodsByName("m2", false)[0];
assertEquals("private synchronized", m2.getModifierList().getText());
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
* Copyright 2000-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,59 +18,46 @@ package com.intellij.psi.impl.cache.impl;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ex.PathManagerEx;
import com.intellij.openapi.roots.ModuleRootModificationUtil;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VfsUtilCore;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.testFramework.PsiTestCase;
import com.intellij.psi.PsiClass;
import com.intellij.testFramework.IdeaTestCase;
import com.intellij.testFramework.PsiTestUtil;
import java.io.File;
import java.io.IOException;
/**
* @author max
*/
public class ClassFileUnderSourceRootTest extends PsiTestCase {
private static final String TEST_ROOT = PathManagerEx.getTestDataPath() + "/psi/repositoryUse/cls";
private VirtualFile myDir;
public class ClassFileUnderSourceRootTest extends IdeaTestCase {
@Override
protected void setUp() throws Exception {
super.setUp();
final File root = createTempDirectory();
File dir = createTempDirectory();
final VirtualFile root = LocalFileSystem.getInstance().findFileByIoFile(dir);
assertNotNull(root);
FileUtil.writeToFile(new File(dir, "p/A.java"), "package p;\npublic class A { }");
FileUtil.copy(new File(PathManagerEx.getTestDataPath() + "/psi/cls/repo/pack/MyClass.class"), new File(dir, "pack/MyClass.class"));
ApplicationManager.getApplication().runWriteAction(new Runnable() {
@Override
public void run() {
try {
VirtualFile rootVFile = LocalFileSystem.getInstance().refreshAndFindFileByPath(root.getAbsolutePath().replace(File.separatorChar, '/'));
myDir = rootVFile.createChildDirectory(null, "contentAndLibrary");
VirtualFile file1 = myDir.createChildData(null, "A.java");
VfsUtil.saveText(file1, "package p; public class A{ public void foo(); }");
PsiDocumentManager.getInstance(getProject()).commitAllDocuments();
VfsUtilCore.copyFile(null, getClassFile(), myDir);
PsiTestUtil.addSourceRoot(myModule, myDir);
ModuleRootModificationUtil.addModuleLibrary(myModule, myDir.getUrl());
}
catch (IOException e) {
LOG.error(e);
}
PsiTestUtil.addSourceRoot(myModule, root);
ModuleRootModificationUtil.addModuleLibrary(myModule, root.getUrl());
}
});
}
private static VirtualFile getClassFile() {
VirtualFile vDir = LocalFileSystem.getInstance().findFileByPath(TEST_ROOT.replace(File.separatorChar, '/'));
return vDir.findFileByRelativePath("pack/MyClass.class");
}
public void testFindClasses() {
PsiClass srcClass = myJavaFacade.findClass("p.A");
assertNotNull(srcClass);
assertEquals("p.A", srcClass.getQualifiedName());
public void testFindClass() throws Exception {
assertEquals("p.A", myJavaFacade.findClass("p.A").getQualifiedName());
assertEquals("pack.MyClass", myJavaFacade.findClass("pack.MyClass").getQualifiedName());
PsiClass pkgClass = myJavaFacade.findClass("pack.MyClass");
assertNotNull(pkgClass);
assertEquals("pack.MyClass", pkgClass.getQualifiedName());
}
}