From 69f07ad01e5f3aa624b6f555d71c5a607b8d8db4 Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Fri, 8 Jun 2012 20:53:05 +0400 Subject: [PATCH] IDEA-87248 (smart type pointer manager should be more tolerant to unknown types) --- .../SmartTypePointerManagerImpl.java | 15 ++++++++++++--- .../src/com/intellij/psi/PsiType.java | 1 + .../src/com/intellij/psi/PsiTypeVisitor.java | 13 ++++++++++++- .../src/com/intellij/psi/Bottom.java | 6 ++++-- .../src/com/intellij/psi/PsiTypeVariable.java | 7 ++++--- .../src/com/intellij/psi/PsiTypeVisitorEx.java | 6 +++++- .../advHighlighting7/LambdaExpressions.java | 4 ++++ 7 files changed, 42 insertions(+), 10 deletions(-) diff --git a/java/java-impl/src/com/intellij/psi/impl/smartPointers/SmartTypePointerManagerImpl.java b/java/java-impl/src/com/intellij/psi/impl/smartPointers/SmartTypePointerManagerImpl.java index 175b3a143b4a..5d86dc4ec0a0 100644 --- a/java/java-impl/src/com/intellij/psi/impl/smartPointers/SmartTypePointerManagerImpl.java +++ b/java/java-impl/src/com/intellij/psi/impl/smartPointers/SmartTypePointerManagerImpl.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2012 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. @@ -42,6 +42,11 @@ import java.util.Set; public class SmartTypePointerManagerImpl extends SmartTypePointerManager { private static final Logger LOG = Logger.getInstance("#com.intellij.psi.impl.smartPointers.SmartTypePointerManagerImpl"); + private static final SmartTypePointer NULL_POINTER = new SmartTypePointer() { + @Override + public PsiType getType() { return null; } + }; + private final SmartPointerManager myPsiPointerManager; private final Project myProject; @@ -53,7 +58,8 @@ public class SmartTypePointerManagerImpl extends SmartTypePointerManager { @Override @NotNull public SmartTypePointer createSmartTypePointer(@NotNull PsiType type) { - return type.accept(new SmartTypeCreatingVisitor()); + final SmartTypePointer pointer = type.accept(new SmartTypeCreatingVisitor()); + return pointer != null ? pointer : NULL_POINTER; } private static class SimpleTypePointer implements SmartTypePointer { @@ -77,6 +83,7 @@ public class SmartTypePointerManagerImpl extends SmartTypePointerManager { myComponentTypePointer = componentTypePointer; } + @Nullable @Override protected PsiArrayType calcType() { final PsiType type = myComponentTypePointer.getType(); @@ -164,6 +171,7 @@ public class SmartTypePointerManagerImpl extends SmartTypePointerManager { return myType; } + @Nullable protected abstract T calcType(); } @@ -228,7 +236,8 @@ public class SmartTypePointerManagerImpl extends SmartTypePointerManager { @Override public SmartTypePointer visitArrayType(PsiArrayType arrayType) { - return new ArrayTypePointer(arrayType, arrayType.getComponentType().accept(this)); + final SmartTypePointer componentTypePointer = arrayType.getComponentType().accept(this); + return componentTypePointer != null ? new ArrayTypePointer(arrayType, componentTypePointer) : null; } @Override diff --git a/java/java-psi-api/src/com/intellij/psi/PsiType.java b/java/java-psi-api/src/com/intellij/psi/PsiType.java index 5ed21bd5fdd0..2c3af2dd9499 100644 --- a/java/java-psi-api/src/com/intellij/psi/PsiType.java +++ b/java/java-psi-api/src/com/intellij/psi/PsiType.java @@ -174,6 +174,7 @@ public abstract class PsiType implements PsiAnnotationOwner { * @param visitor the visitor to accept the type. * @return the value returned by the visitor. */ + @Nullable public abstract A accept(@NotNull PsiTypeVisitor visitor); /** diff --git a/java/java-psi-api/src/com/intellij/psi/PsiTypeVisitor.java b/java/java-psi-api/src/com/intellij/psi/PsiTypeVisitor.java index bd2add5b490a..29b1bce17fb4 100644 --- a/java/java-psi-api/src/com/intellij/psi/PsiTypeVisitor.java +++ b/java/java-psi-api/src/com/intellij/psi/PsiTypeVisitor.java @@ -15,44 +15,55 @@ */ package com.intellij.psi; +import org.jetbrains.annotations.Nullable; + /** * Visitor which can be used to visit Java types. * * @author dsl */ public class PsiTypeVisitor { + @Nullable public A visitType(PsiType type) { return null; } + @Nullable public A visitPrimitiveType(PsiPrimitiveType primitiveType) { return visitType(primitiveType); } + @Nullable public A visitArrayType(PsiArrayType arrayType) { return visitType(arrayType); } + @Nullable public A visitClassType(PsiClassType classType) { return visitType(classType); } + @Nullable public A visitCapturedWildcardType(PsiCapturedWildcardType capturedWildcardType) { return visitWildcardType(capturedWildcardType.getWildcard()); } + @Nullable public A visitWildcardType(PsiWildcardType wildcardType) { return visitType(wildcardType); } + @Nullable public A visitEllipsisType(PsiEllipsisType ellipsisType) { return visitArrayType(ellipsisType); } + @Nullable public A visitDisjunctionType(PsiDisjunctionType disjunctionType) { return visitType(disjunctionType); } - + + @Nullable public A visitDiamondType(PsiDiamondType diamondType) { return visitType(diamondType); } diff --git a/java/java-psi-impl/src/com/intellij/psi/Bottom.java b/java/java-psi-impl/src/com/intellij/psi/Bottom.java index 4b640f1b3b92..942d7792d80e 100644 --- a/java/java-psi-impl/src/com/intellij/psi/Bottom.java +++ b/java/java-psi-impl/src/com/intellij/psi/Bottom.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2012 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. @@ -66,7 +66,9 @@ public class Bottom extends PsiType { if (visitor instanceof PsiTypeVisitorEx) { return ((PsiTypeVisitorEx)visitor).visitBottom(this); } - return visitor.visitType(this); + else { + return visitor.visitType(this); + } } @Override diff --git a/java/java-psi-impl/src/com/intellij/psi/PsiTypeVariable.java b/java/java-psi-impl/src/com/intellij/psi/PsiTypeVariable.java index 8625b26292a0..872c8ded71ea 100644 --- a/java/java-psi-impl/src/com/intellij/psi/PsiTypeVariable.java +++ b/java/java-psi-impl/src/com/intellij/psi/PsiTypeVariable.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2012 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. @@ -33,7 +33,8 @@ public abstract class PsiTypeVariable extends PsiType { if (visitor instanceof PsiTypeVisitorEx) { return ((PsiTypeVisitorEx)visitor).visitTypeVariable(this); } - - return visitor.visitType(this); + else { + return visitor.visitType(this); + } } } diff --git a/java/java-psi-impl/src/com/intellij/psi/PsiTypeVisitorEx.java b/java/java-psi-impl/src/com/intellij/psi/PsiTypeVisitorEx.java index eb31de2bcbc8..47eac032290e 100644 --- a/java/java-psi-impl/src/com/intellij/psi/PsiTypeVisitorEx.java +++ b/java/java-psi-impl/src/com/intellij/psi/PsiTypeVisitorEx.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2012 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. @@ -15,14 +15,18 @@ */ package com.intellij.psi; +import org.jetbrains.annotations.Nullable; + /** * @author ven */ public class PsiTypeVisitorEx extends PsiTypeVisitor { + @Nullable public A visitTypeVariable(PsiTypeVariable var) { return visitType(var); } + @Nullable public A visitBottom (Bottom bottom) { return visitType(bottom); } diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlighting7/LambdaExpressions.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlighting7/LambdaExpressions.java index 14ce1ba5cffd..129b60a46457 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlighting7/LambdaExpressions.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlighting7/LambdaExpressions.java @@ -37,4 +37,8 @@ class C { IntParser intParser = (String s) -> Integer.parseInt(s); ListProducer listProducer = () -> new ArrayList(); } + + Runnable foo() { + return () -> { System.out.println("foo"); }; + } } \ No newline at end of file