Merge remote branch 'origin/master'
This commit is contained in:
+9
@@ -0,0 +1,9 @@
|
||||
/*
|
||||
* @author max
|
||||
*/
|
||||
package org.jetbrains.jet.lang.resolve.java;
|
||||
|
||||
import com.intellij.psi.PsiClass;
|
||||
|
||||
public interface JetJavaMirrorMarker extends PsiClass {
|
||||
}
|
||||
@@ -16,6 +16,7 @@ import org.jetbrains.jet.lang.psi.*;
|
||||
import org.jetbrains.jet.lang.resolve.BindingContext;
|
||||
import org.jetbrains.jet.lang.resolve.BindingContextUtils;
|
||||
import org.jetbrains.jet.lang.resolve.BindingTrace;
|
||||
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
|
||||
import org.jetbrains.jet.lang.types.JetStandardClasses;
|
||||
import org.jetbrains.jet.lang.types.JetType;
|
||||
import org.jetbrains.jet.lexer.JetTokens;
|
||||
@@ -354,8 +355,11 @@ public class JetFlowInformationProvider {
|
||||
return true;
|
||||
}
|
||||
|
||||
JetClassOrObject propertyClassOrObject = PsiTreeUtil.getParentOfType(property, JetClassOrObject.class);
|
||||
if (propertyClassOrObject == null || !PsiTreeUtil.isAncestor(propertyClassOrObject, element, false)) {
|
||||
JetNamedDeclaration parentDeclaration = PsiTreeUtil.getParentOfType(element, JetNamedDeclaration.class);
|
||||
DeclarationDescriptor declarationDescriptor = trace.get(BindingContext.DECLARATION_TO_DESCRIPTOR, parentDeclaration);
|
||||
assert declarationDescriptor != null;
|
||||
ClassDescriptor variableClass = DescriptorUtils.getParentOfType(variableDescriptor, ClassDescriptor.class);
|
||||
if (variableClass == null || !DescriptorUtils.isAncestor(variableClass, declarationDescriptor, false)) {
|
||||
trace.report(Errors.INACCESSIBLE_BACKING_FIELD.on(element));
|
||||
return true;
|
||||
}
|
||||
@@ -482,11 +486,7 @@ public class JetFlowInformationProvider {
|
||||
assert pseudocode != null;
|
||||
JetControlFlowGraphTraverser<Map<VariableDescriptor, VariableStatus>> traverser = JetControlFlowGraphTraverser.create(pseudocode, true, false);
|
||||
final Collection<VariableDescriptor> declaredVariables = collectDeclaredVariables(subroutine);
|
||||
Collection<VariableDescriptor> usedVariables = collectUsedVariables(pseudocode);
|
||||
Map<VariableDescriptor, VariableStatus> sinkInstructionData = Maps.newHashMap();
|
||||
for (VariableDescriptor usedVariable : usedVariables) {
|
||||
sinkInstructionData.put(usedVariable, VariableStatus.UNUSED);
|
||||
}
|
||||
traverser.collectInformationFromInstructionGraph(new JetControlFlowGraphTraverser.InstructionDataMergeStrategy<Map<VariableDescriptor, VariableStatus>>() {
|
||||
@Override
|
||||
public Pair<Map<VariableDescriptor, VariableStatus>, Map<VariableDescriptor, VariableStatus>> execute(Instruction instruction, @NotNull Collection<Map<VariableDescriptor, VariableStatus>> incomingEdgesData) {
|
||||
@@ -505,7 +505,17 @@ public class JetFlowInformationProvider {
|
||||
exitResult.put(variableDescriptor, VariableStatus.READ);
|
||||
}
|
||||
else if (instruction instanceof WriteValueInstruction) {
|
||||
exitResult.put(variableDescriptor, VariableStatus.WRITTEN);
|
||||
VariableStatus variableStatus = enterResult.get(variableDescriptor);
|
||||
if (variableStatus == null) variableStatus = VariableStatus.UNUSED;
|
||||
switch(variableStatus) {
|
||||
case UNUSED:
|
||||
case ONLY_WRITTEN:
|
||||
exitResult.put(variableDescriptor, VariableStatus.ONLY_WRITTEN);
|
||||
break;
|
||||
case WRITTEN:
|
||||
case READ:
|
||||
exitResult.put(variableDescriptor, VariableStatus.WRITTEN);
|
||||
}
|
||||
}
|
||||
}
|
||||
return new Pair<Map<VariableDescriptor, VariableStatus>, Map<VariableDescriptor, VariableStatus>>(enterResult, exitResult);
|
||||
@@ -516,13 +526,12 @@ public class JetFlowInformationProvider {
|
||||
public void execute(Instruction instruction, @Nullable Map<VariableDescriptor, VariableStatus> enterData, @Nullable Map<VariableDescriptor, VariableStatus> exitData) {
|
||||
assert enterData != null && exitData != null;
|
||||
VariableDescriptor variableDescriptor = extractVariableDescriptorIfAny(instruction, false);
|
||||
if (variableDescriptor == null || !declaredVariables.contains(variableDescriptor)) return;
|
||||
PsiElement variableDeclarationElement = trace.get(BindingContext.DESCRIPTOR_TO_DECLARATION, variableDescriptor);
|
||||
assert variableDeclarationElement instanceof JetDeclaration;
|
||||
if (!JetPsiUtil.isLocal((JetDeclaration) variableDeclarationElement)) return;
|
||||
if (variableDescriptor == null || !declaredVariables.contains(variableDescriptor) ||
|
||||
!DescriptorUtils.isLocal(variableDescriptor.getContainingDeclaration(), variableDescriptor)) return;
|
||||
VariableStatus variableStatus = enterData.get(variableDescriptor);
|
||||
if (instruction instanceof WriteValueInstruction) {
|
||||
JetElement element = ((WriteValueInstruction) instruction).getElement();
|
||||
if (enterData.get(variableDescriptor) == VariableStatus.WRITTEN || enterData.get(variableDescriptor) == VariableStatus.UNUSED) {
|
||||
if (variableStatus != VariableStatus.READ) {
|
||||
if (element instanceof JetBinaryExpression && ((JetBinaryExpression) element).getOperationToken() == JetTokens.EQ) {
|
||||
JetExpression right = ((JetBinaryExpression) element).getRight();
|
||||
if (right != null) {
|
||||
@@ -542,7 +551,7 @@ public class JetFlowInformationProvider {
|
||||
if (element instanceof JetNamedDeclaration) {
|
||||
PsiElement nameIdentifier = ((JetNamedDeclaration) element).getNameIdentifier();
|
||||
PsiElement elementToMark = nameIdentifier != null ? nameIdentifier : element;
|
||||
if (enterData.get(variableDescriptor) == VariableStatus.UNUSED) {
|
||||
if (variableStatus == null || variableStatus == VariableStatus.UNUSED) {
|
||||
if (element instanceof JetProperty) {
|
||||
trace.report(Errors.UNUSED_VARIABLE.on((JetProperty) element, elementToMark, variableDescriptor));
|
||||
}
|
||||
@@ -559,9 +568,15 @@ public class JetFlowInformationProvider {
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (enterData.get(variableDescriptor) == VariableStatus.WRITTEN && element instanceof JetProperty) {
|
||||
else if (variableStatus == VariableStatus.ONLY_WRITTEN && element instanceof JetProperty) {
|
||||
trace.report(Errors.ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE.on((JetNamedDeclaration) element, elementToMark, variableDescriptor));
|
||||
}
|
||||
else if (variableStatus == VariableStatus.WRITTEN && element instanceof JetProperty) {
|
||||
JetExpression initializer = ((JetProperty) element).getInitializer();
|
||||
if (initializer != null) {
|
||||
trace.report(Errors.VARIABLE_WITH_REDUNDANT_INITIALIZER.on((JetNamedDeclaration) element, initializer, variableDescriptor));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -570,8 +585,9 @@ public class JetFlowInformationProvider {
|
||||
}
|
||||
|
||||
private static enum VariableStatus {
|
||||
READ(2),
|
||||
WRITTEN(1),
|
||||
READ(3),
|
||||
WRITTEN(2),
|
||||
ONLY_WRITTEN(1),
|
||||
UNUSED(0);
|
||||
|
||||
private int importance;
|
||||
|
||||
@@ -154,6 +154,7 @@ public interface Errors {
|
||||
UnusedElementDiagnosticFactory<JetProperty, VariableDescriptor> UNUSED_VARIABLE = UnusedElementDiagnosticFactory.create(WARNING, "Variable ''{0}'' is never used", NAME);
|
||||
UnusedElementDiagnosticFactory<JetParameter, VariableDescriptor> UNUSED_PARAMETER = UnusedElementDiagnosticFactory.create(WARNING, "Parameter ''{0}'' is never used", NAME);
|
||||
UnusedElementDiagnosticFactory<JetNamedDeclaration, DeclarationDescriptor> ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE = UnusedElementDiagnosticFactory.create(WARNING, "Variable ''{0}'' is assigned but never accessed", NAME);
|
||||
PsiElementOnlyDiagnosticFactory1<JetNamedDeclaration, DeclarationDescriptor> VARIABLE_WITH_REDUNDANT_INITIALIZER = PsiElementOnlyDiagnosticFactory1.create(WARNING, "Variable ''{0}'' initializer is redundant", NAME);
|
||||
PsiElementOnlyDiagnosticFactory2<JetElement, JetElement, DeclarationDescriptor> UNUSED_VALUE = new PsiElementOnlyDiagnosticFactory2<JetElement, JetElement, DeclarationDescriptor>(WARNING, "The value ''{0}'' assigned to ''{1}'' is never used", NAME) {
|
||||
@Override
|
||||
protected String makeMessageForA(@NotNull JetElement element) {
|
||||
|
||||
@@ -103,17 +103,4 @@ public class JetPsiUtil {
|
||||
return unquoteIdentifier(quoted);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isLocal(@NotNull JetElement element) {
|
||||
JetClassOrObject classOrObject = PsiTreeUtil.getParentOfType(element, JetClassOrObject.class);
|
||||
JetDeclarationWithBody function = PsiTreeUtil.getParentOfType(element, JetDeclarationWithBody.class);
|
||||
if (function != null && PsiTreeUtil.isAncestor(classOrObject, function, false)) {
|
||||
return true;
|
||||
}
|
||||
if (classOrObject != null && PsiTreeUtil.isAncestor(function, classOrObject, false)) {
|
||||
return false;
|
||||
}
|
||||
if (function != null) return true;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package org.jetbrains.jet.lang.resolve;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Maps;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.lang.descriptors.*;
|
||||
import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor;
|
||||
import org.jetbrains.jet.lang.types.*;
|
||||
@@ -164,4 +165,35 @@ public class DescriptorUtils {
|
||||
|
||||
return descriptor.getName();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static <D extends DeclarationDescriptor> D getParentOfType(@Nullable DeclarationDescriptor descriptor, @NotNull Class<D> aClass) {
|
||||
return getParentOfType(descriptor, aClass, true);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static <D extends DeclarationDescriptor> D getParentOfType(@Nullable DeclarationDescriptor descriptor, @NotNull Class<D> aClass, boolean strict) {
|
||||
if (descriptor == null) return null;
|
||||
if (strict) {
|
||||
descriptor = descriptor.getContainingDeclaration();
|
||||
}
|
||||
while (descriptor != null) {
|
||||
if (aClass.isInstance(descriptor)) {
|
||||
//noinspection unchecked
|
||||
return (D) descriptor;
|
||||
}
|
||||
descriptor = descriptor.getContainingDeclaration();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static boolean isAncestor(@Nullable DeclarationDescriptor ancestor, @NotNull DeclarationDescriptor declarationDescriptor, boolean strict) {
|
||||
if (ancestor == null) return false;
|
||||
DeclarationDescriptor descriptor = strict ? declarationDescriptor.getContainingDeclaration() : declarationDescriptor;
|
||||
while (descriptor != null) {
|
||||
if (ancestor == descriptor) return true;
|
||||
descriptor = descriptor.getContainingDeclaration();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ class C() {
|
||||
}
|
||||
|
||||
fun f(): Unit {
|
||||
var <!ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE!>x<!>: Int? = 1
|
||||
var x: Int? = <!VARIABLE_WITH_REDUNDANT_INITIALIZER!>1<!>
|
||||
x = 1
|
||||
x <!UNSAFE_INFIX_CALL!>+<!> 1
|
||||
x <!UNSAFE_INFIX_CALL!>plus<!> 1
|
||||
|
||||
@@ -18,7 +18,7 @@ fun bbb() {
|
||||
return <!TYPE_MISMATCH!>1<!>
|
||||
}
|
||||
|
||||
fun foo(expr: StringBuilder): Int {
|
||||
fun foo(<!UNUSED_PARAMETER!>expr<!>: StringBuilder): Int {
|
||||
val c = 'a'
|
||||
when(c) {
|
||||
0.chr => throw Exception("zero")
|
||||
@@ -211,4 +211,4 @@ fun testFunctionLiterals() {
|
||||
object A {}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,7 @@ fun t1(b : Boolean) {
|
||||
}
|
||||
doSmth(<!UNINITIALIZED_VARIABLE!>u<!>)
|
||||
|
||||
var <!ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE!>r<!>: String
|
||||
var r: String
|
||||
if (b) {
|
||||
r = "s"
|
||||
}
|
||||
|
||||
@@ -137,7 +137,7 @@ fun tf() : Int {
|
||||
<!UNREACHABLE_CODE!>return 1<!>
|
||||
}
|
||||
|
||||
fun failtest(a : Int) : Int {
|
||||
fun failtest(<!UNUSED_PARAMETER!>a<!> : Int) : Int {
|
||||
if (fail() || <!UNREACHABLE_CODE!>true<!>) {
|
||||
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
namespace unused_variables
|
||||
|
||||
fun testSimpleCases() {
|
||||
var <!ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE!>i<!> = 2
|
||||
var i = <!VARIABLE_WITH_REDUNDANT_INITIALIZER!>2<!>
|
||||
i = <!UNUSED_VALUE!>34<!>
|
||||
i = 34
|
||||
doSmth(i)
|
||||
@@ -62,7 +62,7 @@ class MyTest() {
|
||||
}
|
||||
|
||||
fun testIf() {
|
||||
var <!ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE!>a<!> : Any
|
||||
var a : Any
|
||||
if (1 < 2) {
|
||||
a = 23
|
||||
}
|
||||
|
||||
@@ -3,55 +3,39 @@
|
||||
*/
|
||||
package org.jetbrains.jet.plugin.java;
|
||||
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.collect.Collections2;
|
||||
import com.intellij.openapi.compiler.ex.CompilerPathsEx;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.fileTypes.FileType;
|
||||
import com.intellij.openapi.fileTypes.FileTypeManager;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.roots.ProjectRootManager;
|
||||
import com.intellij.openapi.util.Comparing;
|
||||
import com.intellij.openapi.util.Key;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.impl.PsiManagerImpl;
|
||||
import com.intellij.psi.impl.compiled.ClsFileImpl;
|
||||
import com.intellij.psi.impl.file.PsiPackageImpl;
|
||||
import com.intellij.psi.impl.java.stubs.PsiClassStub;
|
||||
import com.intellij.psi.impl.java.stubs.PsiJavaFileStub;
|
||||
import com.intellij.psi.impl.java.stubs.impl.PsiJavaFileStubImpl;
|
||||
import com.intellij.psi.search.GlobalSearchScope;
|
||||
import com.intellij.psi.stubs.PsiClassHolderFileStub;
|
||||
import com.intellij.psi.stubs.StubElement;
|
||||
import com.intellij.util.SmartList;
|
||||
import com.intellij.util.containers.Stack;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.codegen.ClassBuilder;
|
||||
import org.jetbrains.jet.codegen.ClassBuilderFactory;
|
||||
import org.jetbrains.jet.codegen.CodegenUtil;
|
||||
import org.jetbrains.jet.codegen.GenerationState;
|
||||
import org.jetbrains.jet.lang.psi.JetClassOrObject;
|
||||
import org.jetbrains.jet.lang.psi.JetDeclaration;
|
||||
import org.jetbrains.jet.lang.psi.JetFile;
|
||||
import org.jetbrains.jet.lang.psi.JetNamespace;
|
||||
import org.jetbrains.jet.lang.resolve.BindingContext;
|
||||
import org.jetbrains.jet.lang.resolve.java.AnalyzerFacade;
|
||||
import org.jetbrains.jet.plugin.JetFileType;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public class JavaElementFinder extends PsiElementFinder {
|
||||
private static final Logger LOG = Logger.getInstance("#org.jetbrains.jet.plugin.java.JavaElementFinder");
|
||||
|
||||
private final Project project;
|
||||
private final PsiManager psiManager;
|
||||
|
||||
public JavaElementFinder(Project project) {
|
||||
this.project = project;
|
||||
psiManager = PsiManager.getInstance(project);
|
||||
}
|
||||
|
||||
private final static Key<PsiJavaFileStub> JAVA_API_STUB = Key.create("JAVA_API_STUB");
|
||||
|
||||
@Override
|
||||
public PsiClass findClass(@NotNull String qualifiedName, @NotNull GlobalSearchScope scope) {
|
||||
final PsiClass[] allClasses = findClasses(qualifiedName, scope);
|
||||
@@ -65,14 +49,22 @@ public class JavaElementFinder extends PsiElementFinder {
|
||||
if (qualifiedName.startsWith("java.")) return PsiClass.EMPTY_ARRAY;
|
||||
|
||||
List<PsiClass> answer = new SmartList<PsiClass>();
|
||||
final List<PsiFile> filesInScope = ensureUpToDate(scope);
|
||||
for (PsiFile file : filesInScope) {
|
||||
final PsiJavaFileStub stub = file.getUserData(JAVA_API_STUB);
|
||||
if (stub == null) continue;
|
||||
|
||||
final String packageName = stub.getPackageName();
|
||||
final List<JetFile> filesInScope = collectProjectFiles(project, scope);
|
||||
for (JetFile file : filesInScope) {
|
||||
JetNamespace rootNamespace = file.getRootNamespace();
|
||||
final String packageName = CodegenUtil.getFQName(rootNamespace);
|
||||
if (packageName != null && qualifiedName.startsWith(packageName)) {
|
||||
collectClasses(answer, qualifiedName, stub);
|
||||
if (qualifiedName.equals(fqn(packageName, "namespace"))) {
|
||||
answer.add(new JetLightClass(psiManager, file, "namespace"));
|
||||
}
|
||||
|
||||
for (JetDeclaration declaration : rootNamespace.getDeclarations()) {
|
||||
if (declaration instanceof JetClassOrObject) {
|
||||
if (qualifiedName.equals(fqn(packageName, declaration.getName()))) {
|
||||
answer.add(new JetLightClass(psiManager, file, declaration.getName()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return answer.toArray(new PsiClass[answer.size()]);
|
||||
@@ -83,15 +75,13 @@ public class JavaElementFinder extends PsiElementFinder {
|
||||
Set<String> answer = new HashSet<String>();
|
||||
|
||||
String packageFQN = psiPackage.getQualifiedName();
|
||||
for (PsiFile psiFile : collectProjectFiles(project, GlobalSearchScope.allScope(project))) {
|
||||
if (psiFile instanceof JetFile) {
|
||||
final JetNamespace rootNamespace = ((JetFile) psiFile).getRootNamespace();
|
||||
if (packageFQN.equals(CodegenUtil.getFQName(rootNamespace))) {
|
||||
answer.add("namespace");
|
||||
for (JetDeclaration declaration : rootNamespace.getDeclarations()) {
|
||||
if (declaration instanceof JetClassOrObject) {
|
||||
answer.add(declaration.getName());
|
||||
}
|
||||
for (JetFile psiFile : collectProjectFiles(project, GlobalSearchScope.allScope(project))) {
|
||||
final JetNamespace rootNamespace = psiFile.getRootNamespace();
|
||||
if (packageFQN.equals(CodegenUtil.getFQName(rootNamespace))) {
|
||||
answer.add("namespace");
|
||||
for (JetDeclaration declaration : rootNamespace.getDeclarations()) {
|
||||
if (declaration instanceof JetClassOrObject) {
|
||||
answer.add(declaration.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -100,15 +90,18 @@ public class JavaElementFinder extends PsiElementFinder {
|
||||
return answer;
|
||||
}
|
||||
|
||||
private static String fqn(String packageName, String className) {
|
||||
if (StringUtil.isEmpty(packageName)) return className;
|
||||
return packageName + "." + className;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PsiPackage findPackage(@NotNull String qualifiedName) {
|
||||
final List<PsiFile> psiFiles = collectProjectFiles(project, GlobalSearchScope.allScope(project));
|
||||
final List<JetFile> psiFiles = collectProjectFiles(project, GlobalSearchScope.allScope(project));
|
||||
|
||||
for (PsiFile psiFile : psiFiles) {
|
||||
if (psiFile instanceof JetFile) {
|
||||
if (qualifiedName.equals(CodegenUtil.getFQName(((JetFile) psiFile).getRootNamespace()))) {
|
||||
return new PsiPackageImpl(psiFile.getManager(), qualifiedName);
|
||||
}
|
||||
for (JetFile psiFile : psiFiles) {
|
||||
if (qualifiedName.equals(CodegenUtil.getFQName(psiFile.getRootNamespace()))) {
|
||||
return new PsiPackageImpl(psiFile.getManager(), qualifiedName);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -119,107 +112,26 @@ public class JavaElementFinder extends PsiElementFinder {
|
||||
@Override
|
||||
public PsiClass[] getClasses(@NotNull PsiPackage psiPackage, @NotNull GlobalSearchScope scope) {
|
||||
List<PsiClass> answer = new SmartList<PsiClass>();
|
||||
final List<PsiFile> filesInScope = ensureUpToDate(scope);
|
||||
final String qualifiedName = psiPackage.getQualifiedName();
|
||||
for (PsiFile file : filesInScope) {
|
||||
final PsiJavaFileStub stub = file.getUserData(JAVA_API_STUB);
|
||||
if (stub == null) continue;
|
||||
|
||||
if (Comparing.equal(qualifiedName, stub.getPackageName())) {
|
||||
for (StubElement child : stub.getChildrenStubs()) {
|
||||
if (child instanceof PsiClassStub) {
|
||||
answer.add((PsiClass) child.getPsi());
|
||||
final List<JetFile> filesInScope = collectProjectFiles(project, scope);
|
||||
String packageFQN = psiPackage.getQualifiedName();
|
||||
for (JetFile file : filesInScope) {
|
||||
final JetNamespace rootNamespace = file.getRootNamespace();
|
||||
if (packageFQN.equals(CodegenUtil.getFQName(rootNamespace))) {
|
||||
answer.add(new JetLightClass(psiManager, file, "namespace"));
|
||||
for (JetDeclaration declaration : rootNamespace.getDeclarations()) {
|
||||
if (declaration instanceof JetClassOrObject) {
|
||||
answer.add(new JetLightClass(psiManager, file, declaration.getName()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return answer.toArray(new PsiClass[answer.size()]);
|
||||
}
|
||||
|
||||
private static void collectClasses(List<PsiClass> answer, String qualifiedName, StubElement<?> stub) {
|
||||
if (stub instanceof PsiClassStub && qualifiedName.equals(((PsiClassStub) stub).getQualifiedName())) {
|
||||
answer.add((PsiClass) stub.getPsi());
|
||||
}
|
||||
|
||||
for (StubElement child : stub.getChildrenStubs()) {
|
||||
collectClasses(answer, qualifiedName, child);
|
||||
}
|
||||
}
|
||||
|
||||
private List<PsiFile> ensureUpToDate(GlobalSearchScope scope) {
|
||||
final Stack<StubElement> stubStack = new Stack<StubElement>();
|
||||
|
||||
final ClassBuilderFactory builderFactory = new ClassBuilderFactory() {
|
||||
@Override
|
||||
public ClassBuilder newClassBuilder() {
|
||||
return new StubClassBuilder(stubStack);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String asText(ClassBuilder builder) {
|
||||
throw new UnsupportedOperationException("asText is not implemented"); // TODO
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] asBytes(ClassBuilder builder) {
|
||||
throw new UnsupportedOperationException("asBytes is not implemented"); // TODO
|
||||
}
|
||||
};
|
||||
|
||||
final GenerationState state = new GenerationState(project, builderFactory) {
|
||||
@Override
|
||||
protected void generateNamespace(JetNamespace namespace) {
|
||||
final PsiJavaFileStubImpl fileStub = new PsiJavaFileStubImpl(CodegenUtil.getFQName(namespace), true);
|
||||
PsiManager manager = PsiManager.getInstance(project);
|
||||
stubStack.push(fileStub);
|
||||
|
||||
final PsiFile file = namespace.getContainingFile();
|
||||
file.putUserData(JAVA_API_STUB, fileStub);
|
||||
fileStub.setPsiFactory(new ClsWrapperStubPsiFactory());
|
||||
final ClsFileImpl fakeFile = new ClsFileImpl((PsiManagerImpl) manager, new ClassFileViewProvider(manager, file.getVirtualFile())) {
|
||||
@NotNull
|
||||
@Override
|
||||
public PsiClassHolderFileStub getStub() {
|
||||
return fileStub;
|
||||
}
|
||||
};
|
||||
|
||||
fakeFile.setPhysical(false);
|
||||
fileStub.setPsi(fakeFile);
|
||||
|
||||
try {
|
||||
super.generateNamespace(namespace);
|
||||
}
|
||||
finally {
|
||||
final StubElement pop = stubStack.pop();
|
||||
if (pop != fileStub) {
|
||||
LOG.error("Unbalanced stack operations");
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
final List<PsiFile> psiFiles = collectProjectFiles(project, scope);
|
||||
|
||||
Collection<PsiFile> dirty = Collections2.filter(psiFiles, new Predicate<PsiFile>() {
|
||||
@Override
|
||||
public boolean apply(PsiFile psiFile) {
|
||||
return psiFile.getUserData(JAVA_API_STUB) == null;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
if (dirty.size() > 0) {
|
||||
final BindingContext context = AnalyzerFacade.shallowAnalyzeFiles(dirty);
|
||||
state.compileCorrectNamespaces(context, AnalyzerFacade.collectRootNamespaces(dirty));
|
||||
state.getFactory().files();
|
||||
}
|
||||
|
||||
return psiFiles;
|
||||
}
|
||||
|
||||
private static List<PsiFile> collectProjectFiles(final Project project, @NotNull final GlobalSearchScope scope) {
|
||||
final List<PsiFile> answer = new ArrayList<PsiFile>();
|
||||
private static List<JetFile> collectProjectFiles(final Project project, @NotNull final GlobalSearchScope scope) {
|
||||
final List<JetFile> answer = new ArrayList<JetFile>();
|
||||
|
||||
|
||||
final FileTypeManager fileTypeManager = FileTypeManager.getInstance();
|
||||
@@ -235,7 +147,7 @@ public class JavaElementFinder extends PsiElementFinder {
|
||||
if (scope.accept(file)) {
|
||||
final PsiFile psiFile = psiManager.findFile(file);
|
||||
if (psiFile instanceof JetFile) {
|
||||
answer.add(psiFile);
|
||||
answer.add((JetFile) psiFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,152 @@
|
||||
/*
|
||||
* @author max
|
||||
*/
|
||||
package org.jetbrains.jet.plugin.java;
|
||||
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Key;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.impl.PsiManagerImpl;
|
||||
import com.intellij.psi.impl.compiled.ClsFileImpl;
|
||||
import com.intellij.psi.impl.java.stubs.PsiClassStub;
|
||||
import com.intellij.psi.impl.java.stubs.PsiJavaFileStub;
|
||||
import com.intellij.psi.impl.java.stubs.impl.PsiJavaFileStubImpl;
|
||||
import com.intellij.psi.impl.light.AbstractLightClass;
|
||||
import com.intellij.psi.stubs.PsiClassHolderFileStub;
|
||||
import com.intellij.psi.stubs.StubElement;
|
||||
import com.intellij.util.containers.Stack;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.codegen.ClassBuilder;
|
||||
import org.jetbrains.jet.codegen.ClassBuilderFactory;
|
||||
import org.jetbrains.jet.codegen.CodegenUtil;
|
||||
import org.jetbrains.jet.codegen.GenerationState;
|
||||
import org.jetbrains.jet.lang.psi.JetFile;
|
||||
import org.jetbrains.jet.lang.psi.JetNamespace;
|
||||
import org.jetbrains.jet.lang.resolve.BindingContext;
|
||||
import org.jetbrains.jet.lang.resolve.java.AnalyzerFacade;
|
||||
import org.jetbrains.jet.lang.resolve.java.JetJavaMirrorMarker;
|
||||
import org.jetbrains.jet.plugin.JetLanguage;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class JetLightClass extends AbstractLightClass implements JetJavaMirrorMarker {
|
||||
private static final Logger LOG = Logger.getInstance("#org.jetbrains.jet.plugin.java.JetLightClass");
|
||||
private final static Key<PsiJavaFileStub> JAVA_API_STUB = Key.create("JAVA_API_STUB");
|
||||
|
||||
private final JetFile file;
|
||||
private final String className;
|
||||
private PsiClass delegate;
|
||||
|
||||
public JetLightClass(PsiManager manager, JetFile file, String className) {
|
||||
super(manager, JetLanguage.INSTANCE);
|
||||
this.file = file;
|
||||
this.className = className;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return className;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PsiElement copy() {
|
||||
return new JetLightClass(getManager(), file, className);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PsiClass getDelegate() {
|
||||
if (delegate == null) {
|
||||
delegate = findClass(className, getStub());
|
||||
}
|
||||
return delegate;
|
||||
}
|
||||
|
||||
private static PsiClass findClass(String name, StubElement<?> stub) {
|
||||
if (stub instanceof PsiClassStub && name.equals(((PsiClassStub) stub).getName())) {
|
||||
return (PsiClass) stub.getPsi();
|
||||
}
|
||||
|
||||
for (StubElement child : stub.getChildrenStubs()) {
|
||||
PsiClass answer = findClass(name, child);
|
||||
if (answer != null) return answer;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
private PsiJavaFileStub getStub() {
|
||||
PsiJavaFileStub answer = file.getUserData(JAVA_API_STUB);
|
||||
if (answer == null) {
|
||||
answer = calcStub();
|
||||
file.putUserData(JAVA_API_STUB, answer);
|
||||
}
|
||||
|
||||
return answer;
|
||||
}
|
||||
|
||||
private PsiJavaFileStub calcStub() {
|
||||
final PsiJavaFileStubImpl answer = new PsiJavaFileStubImpl(CodegenUtil.getFQName(file.getRootNamespace()), true);
|
||||
final Project project = getProject();
|
||||
|
||||
final Stack<StubElement> stubStack = new Stack<StubElement>();
|
||||
|
||||
final ClassBuilderFactory builderFactory = new ClassBuilderFactory() {
|
||||
@Override
|
||||
public ClassBuilder newClassBuilder() {
|
||||
return new StubClassBuilder(stubStack);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String asText(ClassBuilder builder) {
|
||||
throw new UnsupportedOperationException("asText is not implemented"); // TODO
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] asBytes(ClassBuilder builder) {
|
||||
throw new UnsupportedOperationException("asBytes is not implemented"); // TODO
|
||||
}
|
||||
};
|
||||
|
||||
final GenerationState state = new GenerationState(project, builderFactory) {
|
||||
@Override
|
||||
protected void generateNamespace(JetNamespace namespace) {
|
||||
PsiManager manager = PsiManager.getInstance(project);
|
||||
stubStack.push(answer);
|
||||
|
||||
answer.setPsiFactory(new ClsWrapperStubPsiFactory());
|
||||
final ClsFileImpl fakeFile = new ClsFileImpl((PsiManagerImpl) manager, new ClassFileViewProvider(manager, file.getVirtualFile())) {
|
||||
@NotNull
|
||||
@Override
|
||||
public PsiClassHolderFileStub getStub() {
|
||||
return answer;
|
||||
}
|
||||
};
|
||||
|
||||
fakeFile.setPhysical(false);
|
||||
answer.setPsi(fakeFile);
|
||||
|
||||
try {
|
||||
super.generateNamespace(namespace);
|
||||
}
|
||||
finally {
|
||||
final StubElement pop = stubStack.pop();
|
||||
if (pop != answer) {
|
||||
LOG.error("Unbalanced stack operations");
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
List<PsiFile> files = Collections.<PsiFile>singletonList(file);
|
||||
final BindingContext context = AnalyzerFacade.shallowAnalyzeFiles(files);
|
||||
state.compileCorrectNamespaces(context, AnalyzerFacade.collectRootNamespaces(files));
|
||||
state.getFactory().files();
|
||||
|
||||
return answer;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -11,7 +11,7 @@ class C() {
|
||||
}
|
||||
|
||||
fun f(): Unit {
|
||||
var <warning>x</warning>: Int? = 1
|
||||
var x: Int? = <warning>1</warning>
|
||||
x = 1
|
||||
x <error>+</error> 1
|
||||
x <error>plus</error> 1
|
||||
|
||||
@@ -13,7 +13,7 @@ fun bbb() {
|
||||
return <error>1</error>
|
||||
}
|
||||
|
||||
fun foo(expr: StringBuilder): Int {
|
||||
fun foo(<warning>expr</warning>: StringBuilder): Int {
|
||||
val c = 'a'
|
||||
when(c) {
|
||||
0.chr => throw Exception("zero")
|
||||
|
||||
@@ -135,7 +135,7 @@ fun tf() : Int {
|
||||
<error>return 1</error>
|
||||
}
|
||||
|
||||
fun failtest(a : Int) : Int {
|
||||
fun failtest(<warning>a</warning> : Int) : Int {
|
||||
if (fail() || <error>true</error>) {
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user