private static PsiClass getTopLevelClass(@NotNull PsiElement place, PsiClass memberClass) { PsiClass lastClass = null; Boolean isAtLeast17 = null; for (PsiElement placeParent = place; placeParent != null; placeParent = placeParent.getContext()) { if (placeParent instanceof PsiClass && !(placeParent instanceof PsiAnonymousClass)) { final boolean isTypeParameter = placeParent instanceof PsiTypeParameter; if (isTypeParameter && isAtLeast17 == null) { isAtLeast17 = JavaVersionService.getInstance().isAtLeast(placeParent, JavaSdkVersion.JDK_1_7); } if (!isTypeParameter || isAtLeast17) { PsiClass aClass = (PsiClass)placeParent; if (memberClass != null && aClass.isInheritor(memberClass, true)) return aClass; lastClass = aClass; } } } return lastClass; }
@Override public boolean isAccessible() { Boolean Accessible = myAccessible; boolean accessible = true; if(Accessible == null) { if (myPlace != null && myCandidate instanceof PsiMember) { final PsiMember member = (PsiMember)myCandidate; accessible = JavaPsiFacade.getInstance(myPlace.getProject()).getResolveHelper() .isAccessible(member, member.getModifierList(), myPlace, myAccessClass, myCurrentFileResolveContext); if (accessible && member.hasModifierProperty(PsiModifier.PRIVATE) && myPlace instanceof PsiReferenceExpression && JavaVersionService.getInstance().isAtLeast(myPlace, JavaSdkVersion.JDK_1_7)) { accessible = !isAccessedThroughTypeParameterBound(); } } myAccessible = accessible; } else { accessible = Accessible; } return accessible; }
@Override public String getTooltip(MemberInfo memberInfo) { if (checkForProblems(memberInfo) == OK) return null; if (!(memberInfo.getMember() instanceof PsiField)) return CodeInsightBundle.message("generate.equals.hashcode.internal.error"); final PsiField field = (PsiField)memberInfo.getMember(); if (!JavaVersionService.getInstance().isAtLeast(field, JavaSdkVersion.JDK_1_5)) { final PsiType type = field.getType(); if (PsiAdapter.isNestedArray(type)) { return CodeInsightBundle .message("generate.equals.warning.equals.for.nested.arrays.not.supported"); } if (GenerateEqualsHelper.isArrayOfObjects(type)) { return CodeInsightBundle.message("generate.equals.warning.generated.equals.could.be.incorrect"); } } return null; }
@Override public boolean isAccessible(){ if(myAccessProblem == null){ boolean accessProblem = false; if (myPlace != null && myCandidate instanceof PsiMember) { final PsiMember member = (PsiMember)myCandidate; accessProblem = !JavaPsiFacade.getInstance(myPlace.getProject()).getResolveHelper() .isAccessible(member, member.getModifierList(), myPlace, myAccessClass, myCurrentFileResolveContext); if (!accessProblem && member.hasModifierProperty(PsiModifier.PRIVATE) && myPlace instanceof PsiReferenceExpression && JavaVersionService.getInstance().isAtLeast(myPlace, JavaSdkVersion.JDK_1_7)) { accessProblem = isAccessedThroughTypeParameterBound(); } } myAccessProblem = accessProblem ? Boolean.TRUE : Boolean.FALSE; } return !myAccessProblem.booleanValue(); }
@Override public boolean isAccessible() { Boolean Accessible = myAccessible; boolean accessible = true; if(Accessible == null) { if(myPlace != null && myCandidate instanceof PsiMember) { final PsiMember member = (PsiMember) myCandidate; accessible = JavaPsiFacade.getInstance(myPlace.getProject()).getResolveHelper().isAccessible(member, member.getModifierList(), myPlace, myAccessClass, myCurrentFileResolveContext); if(accessible && member.hasModifierProperty(PsiModifier.PRIVATE) && myPlace instanceof PsiReferenceExpression && JavaVersionService.getInstance().isAtLeast(myPlace, JavaSdkVersion .JDK_1_7)) { accessible = !isAccessedThroughTypeParameterBound(); } } myAccessible = accessible; } else { accessible = Accessible; } return accessible; }
@Override public String getTooltip(MemberInfo memberInfo) { if(checkForProblems(memberInfo) == OK) { return null; } if(!(memberInfo.getMember() instanceof PsiField)) { return CodeInsightBundle.message("generate.equals.hashcode.internal.error"); } final PsiField field = (PsiField) memberInfo.getMember(); if(!JavaVersionService.getInstance().isAtLeast(field, JavaSdkVersion.JDK_1_5)) { final PsiType type = field.getType(); if(PsiAdapter.isNestedArray(type)) { return CodeInsightBundle.message("generate.equals.warning.equals.for.nested.arrays.not.supported"); } if(GenerateEqualsHelper.isArrayOfObjects(type)) { return CodeInsightBundle.message("generate.equals.warning.generated.equals.could.be.incorrect"); } } return null; }
@Override public int checkForProblems(@NotNull MemberInfo member) { if(!(member.getMember() instanceof PsiField)) { return ERROR; } final PsiField field = (PsiField) member.getMember(); final PsiType type = field.getType(); if(!JavaVersionService.getInstance().isAtLeast(field, JavaSdkVersion.JDK_1_5)) { if(PsiAdapter.isNestedArray(type)) { return ERROR; } if(GenerateEqualsHelper.isArrayOfObjects(type)) { return WARNING; } } return OK; }
@Override public String getTooltip(MemberInfo memberInfo) { if(isMemberEnabled(memberInfo)) { return null; } if(!(memberInfo.getMember() instanceof PsiField)) { return CodeInsightBundle.message("generate.equals.hashcode.internal.error"); } final PsiField field = (PsiField) memberInfo.getMember(); final PsiType type = field.getType(); if(!(type instanceof PsiArrayType) || JavaVersionService.getInstance().isAtLeast(field, JavaSdkVersion.JDK_1_5)) { return null; } return CodeInsightBundle.message("generate.equals.hashcode.warning.hashcode.for.arrays.is.not.supported"); }
private boolean isTypeArgumentsApplicable(Computable<PsiSubstitutor> computable) { final PsiMethod psiMethod = getElement(); PsiTypeParameter[] typeParams = psiMethod.getTypeParameters(); if (myTypeArguments != null && typeParams.length != myTypeArguments.length && !PsiUtil.isLanguageLevel7OrHigher(psiMethod)){ return typeParams.length == 0 && JavaVersionService.getInstance().isAtLeast(psiMethod, JavaSdkVersion.JDK_1_7); } return GenericsUtil.isTypeArgumentsApplicable(typeParams, computable.compute(), getParent()); }
public static int getJavaVersion(@NotNull PsiElement element) { JavaSdkVersion sdkVersion = JavaVersionService.getInstance().getJavaSdkVersion(element); if (sdkVersion == null) { sdkVersion = JavaSdkVersion.fromLanguageLevel(PsiUtil.getLanguageLevel(element)); } int version = 0; switch (sdkVersion) { case JDK_1_0: case JDK_1_1: version = 1; break; case JDK_1_2: version = 2; break; case JDK_1_3: version = 3; break; case JDK_1_4: version = 4; break; case JDK_1_5: version = 5; break; case JDK_1_6: version = 6; break; case JDK_1_7: version = 7; break; case JDK_1_8: version = 8; break; case JDK_1_9: version = 9; break; } return version; }
@Override public void visitClass(PsiClass aClass) { // Don't go into classes (anonymous, locals). if (!aClass.hasModifierProperty(PsiModifier.ABSTRACT)) { final Module module = ModuleUtilCore.findModuleForPsiElement(aClass); final LanguageLevel effectiveLanguageLevel = module != null ? getEffectiveLanguageLevel(module) : null; if (effectiveLanguageLevel != null && !effectiveLanguageLevel.isAtLeast(LanguageLevel.JDK_1_8)) { final JavaSdkVersion version = JavaVersionService.getInstance().getJavaSdkVersion(aClass); if (version != null && version.isAtLeast(JavaSdkVersion.JDK_1_8)) { final List<PsiMethod> methods = new ArrayList<PsiMethod>(); for (HierarchicalMethodSignature methodSignature : aClass.getVisibleSignatures()) { final PsiMethod method = methodSignature.getMethod(); if (ourDefaultMethods.contains(getSignature(method))) { methods.add(method); } } if (!methods.isEmpty()) { PsiElement element2Highlight = aClass.getNameIdentifier(); if (element2Highlight == null) { element2Highlight = aClass; } myHolder.registerProblem(element2Highlight, methods.size() == 1 ? InspectionsBundle.message("inspection.1.8.problem.single.descriptor", methods.get(0).getName(), getJdkName(effectiveLanguageLevel)) : InspectionsBundle.message("inspection.1.8.problem.descriptor", methods.size(), getJdkName(effectiveLanguageLevel)), QuickFixFactory.getInstance().createImplementMethodsFix(aClass)); } } } } }
@Nullable static HighlightInfo checkAccessStaticFieldFromEnumConstructor(@NotNull PsiReferenceExpression expr, @NotNull JavaResolveResult result) { final PsiElement resolved = result.getElement(); if (!(resolved instanceof PsiField)) return null; if (!((PsiModifierListOwner)resolved).hasModifierProperty(PsiModifier.STATIC)) return null; if (expr.getParent() instanceof PsiSwitchLabelStatement) return null; final PsiMember constructorOrInitializer = PsiUtil.findEnclosingConstructorOrInitializer(expr); if (constructorOrInitializer == null) return null; if (constructorOrInitializer.hasModifierProperty(PsiModifier.STATIC)) return null; final PsiClass aClass = constructorOrInitializer instanceof PsiEnumConstantInitializer ? (PsiClass)constructorOrInitializer : constructorOrInitializer.getContainingClass(); if (aClass == null || !(aClass.isEnum() || aClass instanceof PsiEnumConstantInitializer)) return null; final PsiField field = (PsiField)resolved; if (aClass instanceof PsiEnumConstantInitializer) { if (field.getContainingClass() != aClass.getSuperClass()) return null; } else if (field.getContainingClass() != aClass) return null; if (!JavaVersionService.getInstance().isAtLeast(field, JavaSdkVersion.JDK_1_6)) { final PsiType type = field.getType(); if (type instanceof PsiClassType && ((PsiClassType)type).resolve() == aClass) return null; } if (PsiUtil.isCompileTimeConstant(field)) return null; String description = JavaErrorMessages.message( "illegal.to.access.static.member.from.enum.constructor.or.instance.initializer", HighlightMessageUtil.getSymbolName(resolved, result.getSubstitutor()) ); return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expr).descriptionAndTooltip(description).create(); }
@Override public boolean isMemberEnabled(MemberInfo member) { if (!(member.getMember() instanceof PsiField)) return false; final PsiField field = (PsiField)member.getMember(); final PsiType type = field.getType(); return JavaVersionService.getInstance().isAtLeast(field, JavaSdkVersion.JDK_1_5) || !PsiAdapter.isNestedArray(type); }
@Override public int checkForProblems(@NotNull MemberInfo member) { if (!(member.getMember() instanceof PsiField)) return ERROR; final PsiField field = (PsiField)member.getMember(); final PsiType type = field.getType(); if (!JavaVersionService.getInstance().isAtLeast(field, JavaSdkVersion.JDK_1_5)) { if (PsiAdapter.isNestedArray(type)) return ERROR; if (GenerateEqualsHelper.isArrayOfObjects(type)) return WARNING; } return OK; }
@Override public String getTooltip(MemberInfo memberInfo) { if (isMemberEnabled(memberInfo)) return null; if (!(memberInfo.getMember() instanceof PsiField)) return CodeInsightBundle.message("generate.equals.hashcode.internal.error"); final PsiField field = (PsiField)memberInfo.getMember(); final PsiType type = field.getType(); if (!(type instanceof PsiArrayType) || JavaVersionService.getInstance().isAtLeast(field, JavaSdkVersion.JDK_1_5)) return null; return CodeInsightBundle.message("generate.equals.hashcode.warning.hashcode.for.arrays.is.not.supported"); }
private static int preferVarargs(CandidateInfo info) { final int level = ((MethodCandidateInfo)info).getApplicabilityLevel(); if (level == MethodCandidateInfo.ApplicabilityLevel.FIXED_ARITY) { final PsiMethod psiMethod = (PsiMethod)info.getElement(); if (psiMethod != null && psiMethod.isVarArgs() && JavaVersionService.getInstance().isAtLeast(psiMethod, JavaSdkVersion.JDK_1_7)) { return level + 1; } } return level; }
public JavaCoreApplicationEnvironment(Disposable parentDisposable) { super(parentDisposable); registerFileType(JavaClassFileType.INSTANCE, "class"); registerFileType(JavaFileType.INSTANCE, "java"); registerFileType(ArchiveFileType.INSTANCE, "jar;zip"); addExplicitExtension(FileTypeFileViewProviders.INSTANCE, JavaClassFileType.INSTANCE, new ClassFileViewProviderFactory()); addExplicitExtension(BinaryFileStubBuilders.INSTANCE, JavaClassFileType.INSTANCE, new ClassFileStubBuilder()); addExplicitExtension(LanguageASTFactory.INSTANCE, JavaLanguage.INSTANCE, new CoreJavaASTFactory()); addExplicitExtension(LanguageParserDefinitions.INSTANCE, JavaLanguage.INSTANCE, new JavaParserDefinition()); addExplicitExtension(LanguageConstantExpressionEvaluator.INSTANCE, JavaLanguage.INSTANCE, new PsiExpressionEvaluator()); registerExtensionPoint(Extensions.getRootArea(), ClsStubBuilderFactory.EP_NAME, ClsStubBuilderFactory.class); registerExtensionPoint(Extensions.getRootArea(), PsiAugmentProvider.EP_NAME, PsiAugmentProvider.class); registerExtensionPoint(Extensions.getRootArea(), JavaMainMethodProvider.EP_NAME, JavaMainMethodProvider.class); addExtension(ClsStubBuilderFactory.EP_NAME, new DefaultClsStubBuilderFactory()); myApplication.registerService(PsiPackageImplementationHelper.class, new CorePsiPackageImplementationHelper()); myApplication.registerService(EmptySubstitutor.class, new EmptySubstitutorImpl()); myApplication.registerService(JavaDirectoryService.class, createJavaDirectoryService()); myApplication.registerService(JavaVersionService.class, new JavaVersionService()); addExplicitExtension(ItemPresentationProviders.INSTANCE, PsiPackage.class, new PackagePresentationProvider()); addExplicitExtension(ItemPresentationProviders.INSTANCE, PsiClass.class, new ClassPresentationProvider()); addExplicitExtension(ItemPresentationProviders.INSTANCE, PsiMethod.class, new MethodPresentationProvider()); addExplicitExtension(ItemPresentationProviders.INSTANCE, PsiField.class, new FieldPresentationProvider()); addExplicitExtension(ItemPresentationProviders.INSTANCE, PsiLocalVariable.class, new VariablePresentationProvider()); addExplicitExtension(ItemPresentationProviders.INSTANCE, PsiParameter.class, new VariablePresentationProvider()); }
public boolean isTypeArgumentsApplicable() { final PsiMethod psiMethod = getElement(); PsiTypeParameter[] typeParams = psiMethod.getTypeParameters(); if (myTypeArguments != null && typeParams.length != myTypeArguments.length && !PsiUtil.isLanguageLevel7OrHigher(psiMethod)){ return typeParams.length == 0 && JavaVersionService.getInstance().isAtLeast(psiMethod, JavaSdkVersion.JDK_1_7); } PsiSubstitutor substitutor = getSubstitutor(); return GenericsUtil.isTypeArgumentsApplicable(typeParams, substitutor, getParent()); }
@Nullable public static HighlightInfo checkAccessStaticFieldFromEnumConstructor(@NotNull PsiReferenceExpression expr, @NotNull JavaResolveResult result) { final PsiElement resolved = result.getElement(); if (!(resolved instanceof PsiField)) return null; if (!((PsiModifierListOwner)resolved).hasModifierProperty(PsiModifier.STATIC)) return null; final PsiMember constructorOrInitializer = PsiUtil.findEnclosingConstructorOrInitializer(expr); if (constructorOrInitializer == null) return null; if (constructorOrInitializer.hasModifierProperty(PsiModifier.STATIC)) return null; final PsiClass aClass = constructorOrInitializer.getContainingClass(); if (aClass == null || !(aClass.isEnum() || aClass instanceof PsiEnumConstantInitializer)) return null; final PsiField field = (PsiField)resolved; if (aClass instanceof PsiEnumConstantInitializer) { if (field.getContainingClass() != aClass.getSuperClass()) return null; } else if (field.getContainingClass() != aClass) return null; if (!JavaVersionService.getInstance().isAtLeast(field, JavaSdkVersion.JDK_1_6)) { final PsiType type = field.getType(); if (type instanceof PsiClassType && ((PsiClassType)type).resolve() == aClass) return null; } if (PsiUtil.isCompileTimeConstant(field)) return null; String description = JavaErrorMessages.message( "illegal.to.access.static.member.from.enum.constructor.or.instance.initializer", HighlightMessageUtil.getSymbolName(resolved, result.getSubstitutor()) ); return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expr).descriptionAndTooltip(description).create(); }
private static PsiClass getTopLevelClass(@NotNull PsiElement place, PsiClass memberClass) { PsiClass lastClass = null; Boolean isAtLeast17 = null; for(PsiElement placeParent = place; placeParent != null; placeParent = placeParent.getContext()) { if(placeParent instanceof PsiClass && !(placeParent instanceof PsiAnonymousClass)) { final boolean isTypeParameter = placeParent instanceof PsiTypeParameter; if(isTypeParameter && isAtLeast17 == null) { isAtLeast17 = JavaVersionService.getInstance().isAtLeast(placeParent, JavaSdkVersion.JDK_1_7); } if(!isTypeParameter || isAtLeast17) { PsiClass aClass = (PsiClass) placeParent; if(memberClass != null && aClass.isInheritor(memberClass, true)) { return aClass; } lastClass = aClass; } } } return lastClass; }
public JavaCoreApplicationEnvironment(Disposable parentDisposable) { super(parentDisposable); registerFileType(JavaClassFileType.INSTANCE, "class"); registerFileType(JavaFileType.INSTANCE, "java"); registerFileType(JarArchiveFileType.INSTANCE, "jar"); addExplicitExtension(FileTypeFileViewProviders.INSTANCE, JavaClassFileType.INSTANCE, new ClassFileViewProviderFactory()); addExplicitExtension(BinaryFileStubBuilders.INSTANCE, JavaClassFileType.INSTANCE, new ClassFileStubBuilder()); addExplicitExtension(LanguageParserDefinitions.INSTANCE, JavaLanguage.INSTANCE, new JavaParserDefinition()); addExplicitExtension(LanguageConstantExpressionEvaluator.INSTANCE, JavaLanguage.INSTANCE, new PsiExpressionEvaluator()); registerExtensionPoint(Extensions.getRootArea(), PsiAugmentProvider.EP_NAME, PsiAugmentProvider.class); registerExtensionPoint(Extensions.getRootArea(), JavaMainMethodProvider.EP_NAME, JavaMainMethodProvider.class); myApplication.registerService(PsiPackageImplementationHelper.class, new CorePsiPackageImplementationHelper()); myApplication.registerService(EmptySubstitutor.class, new EmptySubstitutorImpl()); myApplication.registerService(JavaDirectoryService.class, createJavaDirectoryService()); myApplication.registerService(JavaVersionService.class, new JavaVersionService()); addExplicitExtension(ItemPresentationProviders.INSTANCE, PsiJavaPackage.class, new PackagePresentationProvider()); addExplicitExtension(ItemPresentationProviders.INSTANCE, PsiClass.class, new ClassPresentationProvider()); addExplicitExtension(ItemPresentationProviders.INSTANCE, PsiMethod.class, new MethodPresentationProvider()); addExplicitExtension(ItemPresentationProviders.INSTANCE, PsiField.class, new FieldPresentationProvider()); addExplicitExtension(ItemPresentationProviders.INSTANCE, PsiLocalVariable.class, new VariablePresentationProvider()); addExplicitExtension(ItemPresentationProviders.INSTANCE, PsiParameter.class, new VariablePresentationProvider()); }
private boolean isTypeArgumentsApplicable(Computable<PsiSubstitutor> computable) { final PsiMethod psiMethod = getElement(); PsiTypeParameter[] typeParams = psiMethod.getTypeParameters(); if(myTypeArguments != null && typeParams.length != myTypeArguments.length && !PsiUtil.isLanguageLevel7OrHigher(psiMethod)) { return typeParams.length == 0 && JavaVersionService.getInstance().isAtLeast(psiMethod, JavaSdkVersion.JDK_1_7); } return GenericsUtil.isTypeArgumentsApplicable(typeParams, computable.compute(), getParent()); }
@Override public boolean isMemberEnabled(MemberInfo member) { if(!(member.getMember() instanceof PsiField)) { return false; } final PsiField field = (PsiField) member.getMember(); final PsiType type = field.getType(); return JavaVersionService.getInstance().isAtLeast(field, JavaSdkVersion.JDK_1_5) || !PsiAdapter.isNestedArray(type); }
private void prepare(HighlightInfoHolder holder, PsiFile file) { myHolder = holder; myFile = file; myLanguageLevel = PsiUtil.getLanguageLevel(file); myJavaSdkVersion = notNull(JavaVersionService.getInstance().getJavaSdkVersion(file), JavaSdkVersion.fromLanguageLevel(myLanguageLevel)); myJavaModule = myLanguageLevel.isAtLeast(LanguageLevel.JDK_1_9) ? ModuleHighlightUtil.getModuleDescriptor(file) : null; }
public static DiamondInferenceResult resolveInferredTypesNoCheck(final PsiNewExpression newExpression, final PsiElement context) { final Ref<MethodCandidateInfo> staticFactoryRef = new Ref<MethodCandidateInfo>(); final PsiSubstitutor inferredSubstitutor = ourDiamondGuard.doPreventingRecursion(context, false, new Computable<PsiSubstitutor>() { @Override public PsiSubstitutor compute() { final MethodCandidateInfo staticFactoryCandidateInfo = context == newExpression ? CachedValuesManager.getCachedValue(context, new CachedValueProvider<MethodCandidateInfo>() { @Nullable @Override public Result<MethodCandidateInfo> compute() { return new Result<MethodCandidateInfo>(getStaticFactoryCandidateInfo(newExpression, newExpression), PsiModificationTracker.MODIFICATION_COUNT); } }) : getStaticFactoryCandidateInfo(newExpression, context); staticFactoryRef.set(staticFactoryCandidateInfo); return staticFactoryCandidateInfo != null ? staticFactoryCandidateInfo.getSubstitutor() : null; } }); if (inferredSubstitutor == null) { return DiamondInferenceResult.NULL_RESULT; } final MethodCandidateInfo staticFactoryInfo = staticFactoryRef.get(); if (staticFactoryInfo == null) { LOG.error(inferredSubstitutor); return DiamondInferenceResult.NULL_RESULT; } final PsiMethod staticFactory = staticFactoryInfo.getElement(); final PsiTypeParameter[] parameters = staticFactory.getTypeParameters(); final PsiElement staticFactoryContext = staticFactory.getContext(); final PsiClass psiClass = PsiTreeUtil.getContextOfType(staticFactoryContext, PsiClass.class, false); if (psiClass == null) { LOG.error("failed for expression:" + newExpression); return DiamondInferenceResult.NULL_RESULT; } final PsiTypeParameter[] classParameters = psiClass.getTypeParameters(); final PsiJavaCodeReferenceElement classOrAnonymousClassReference = newExpression.getClassOrAnonymousClassReference(); LOG.assertTrue(classOrAnonymousClassReference != null); final DiamondInferenceResult result = new DiamondInferenceResult(classOrAnonymousClassReference.getReferenceName() + "<>"); if (PsiUtil.isRawSubstitutor(staticFactory, inferredSubstitutor)) { if (!JavaVersionService.getInstance().isAtLeast(newExpression, JavaSdkVersion.JDK_1_8) && PsiUtil.skipParenthesizedExprUp(newExpression.getParent()) instanceof PsiExpressionList) { for (PsiTypeParameter ignored : parameters) { result.addInferredType(PsiType.getJavaLangObject(newExpression.getManager(), GlobalSearchScope.allScope(newExpression.getProject()))); } } return result; } for (PsiTypeParameter parameter : parameters) { for (PsiTypeParameter classParameter : classParameters) { if (Comparing.strEqual(classParameter.getName(), parameter.getName())) { result.addInferredType(inferredSubstitutor.substitute(parameter)); break; } } } return result; }
public static PsiType captureReturnType(PsiMethodCallExpression call, PsiMethod method, PsiType ret, JavaResolveResult result, LanguageLevel languageLevel) { PsiSubstitutor substitutor = result.getSubstitutor(); PsiType substitutedReturnType = substitutor.substitute(ret); if (substitutedReturnType == null) { return TypeConversionUtil.erasure(ret); } if (InferenceSession.wasUncheckedConversionPerformed(call)) { // 18.5.2 // if unchecked conversion was necessary, then this substitution provides the parameter types of the invocation type, // while the return type and thrown types are given by the erasure of m's type (without applying θ'). return TypeConversionUtil.erasure(substitutedReturnType); } //15.12.2.6. Method Invocation Type // If unchecked conversion was necessary for the method to be applicable, // the parameter types of the invocation type are the parameter types of the method's type, // and the return type and thrown types are given by the erasures of the return type and thrown types of the method's type. if (!languageLevel.isAtLeast(LanguageLevel.JDK_1_8) && (method.hasTypeParameters() || JavaVersionService.getInstance().isAtLeast(call, JavaSdkVersion.JDK_1_8)) && result instanceof MethodCandidateInfo && ((MethodCandidateInfo)result).isApplicable()) { final PsiType[] args = call.getArgumentList().getExpressionTypes(); final boolean allowUncheckedConversion = false; final int applicabilityLevel = PsiUtil.getApplicabilityLevel(method, substitutor, args, languageLevel, allowUncheckedConversion, true); if (applicabilityLevel == MethodCandidateInfo.ApplicabilityLevel.NOT_APPLICABLE) { return TypeConversionUtil.erasure(substitutedReturnType); } } if (PsiUtil.isRawSubstitutor(method, substitutor)) { final PsiType returnTypeErasure = TypeConversionUtil.erasure(ret); if (Comparing.equal(TypeConversionUtil.erasure(substitutedReturnType), returnTypeErasure)) { return returnTypeErasure; } } return PsiImplUtil.normalizeWildcardTypeByPosition(substitutedReturnType, call); }
public JavaCoreApplicationEnvironment(@NotNull Disposable parentDisposable) { super(parentDisposable); registerFileType(JavaClassFileType.INSTANCE, "class"); registerFileType(JavaFileType.INSTANCE, "java"); registerFileType(ArchiveFileType.INSTANCE, "jar;zip"); registerFileType(PlainTextFileType.INSTANCE, "txt;sh;bat;cmd;policy;log;cgi;MF;jad;jam;htaccess"); addExplicitExtension(LanguageASTFactory.INSTANCE, PlainTextLanguage.INSTANCE, new PlainTextASTFactory()); addExplicitExtension(LanguageParserDefinitions.INSTANCE, PlainTextLanguage.INSTANCE, new PlainTextParserDefinition()); addExplicitExtension(FileTypeFileViewProviders.INSTANCE, JavaClassFileType.INSTANCE, new ClassFileViewProviderFactory()); addExplicitExtension(BinaryFileStubBuilders.INSTANCE, JavaClassFileType.INSTANCE, new ClassFileStubBuilder()); addExplicitExtension(LanguageASTFactory.INSTANCE, JavaLanguage.INSTANCE, new CoreJavaASTFactory()); addExplicitExtension(LanguageParserDefinitions.INSTANCE, JavaLanguage.INSTANCE, new JavaParserDefinition()); addExplicitExtension(LanguageConstantExpressionEvaluator.INSTANCE, JavaLanguage.INSTANCE, new PsiExpressionEvaluator()); addExtension(ContainerProvider.EP_NAME, new JavaContainerProvider()); myApplication.registerService(PsiPackageImplementationHelper.class, new CorePsiPackageImplementationHelper()); myApplication.registerService(EmptySubstitutor.class, new EmptySubstitutorImpl()); myApplication.registerService(JavaDirectoryService.class, createJavaDirectoryService()); myApplication.registerService(JavaVersionService.class, new JavaVersionService()); addExplicitExtension(ItemPresentationProviders.INSTANCE, PsiPackage.class, new PackagePresentationProvider()); addExplicitExtension(ItemPresentationProviders.INSTANCE, PsiClass.class, new ClassPresentationProvider()); addExplicitExtension(ItemPresentationProviders.INSTANCE, PsiMethod.class, new MethodPresentationProvider()); addExplicitExtension(ItemPresentationProviders.INSTANCE, PsiField.class, new FieldPresentationProvider()); addExplicitExtension(ItemPresentationProviders.INSTANCE, PsiLocalVariable.class, new VariablePresentationProvider()); addExplicitExtension(ItemPresentationProviders.INSTANCE, PsiParameter.class, new VariablePresentationProvider()); registerApplicationService(JavaCodeFoldingSettings.class, new JavaCodeFoldingSettingsBase()); addExplicitExtension(LanguageFolding.INSTANCE, JavaLanguage.INSTANCE, new JavaFoldingBuilderBase() { @Override protected boolean shouldShowExplicitLambdaType(PsiAnonymousClass anonymousClass, PsiNewExpression expression) { return false; } @Override protected boolean isBelowRightMargin(Project project, int lineLength) { return false; } }); registerApplicationExtensionPoint(SuperMethodsSearch.EP_NAME, QueryExecutor.class); addExtension(SuperMethodsSearch.EP_NAME, new MethodSuperSearcher()); }
public JavacQuirksInspectionVisitor(ProblemsHolder holder) { myHolder = holder; mySdkVersion = JavaVersionService.getInstance().getJavaSdkVersion(myHolder.getFile()); myLanguageLevel = PsiUtil.getLanguageLevel(myHolder.getFile()); }
@Override public boolean analyze(@NotNull final PsiFile file, final boolean updateWholeFile, @NotNull final HighlightInfoHolder holder, @NotNull final Runnable highlight) { myFile = file; myHolder = Holder.CHECK_ELEMENT_LEVEL ? new CheckLevelHighlightInfoHolder(file, holder) : holder; boolean success = true; try { myLanguageLevel = PsiUtil.getLanguageLevel(file); myJavaSdkVersion = ObjectUtils.notNull(JavaVersionService.getInstance().getJavaSdkVersion(file), JavaSdkVersion.fromLanguageLevel(myLanguageLevel)); if (updateWholeFile) { final Project project = file.getProject(); DaemonCodeAnalyzerEx daemonCodeAnalyzer = DaemonCodeAnalyzerEx.getInstanceEx(project); final FileStatusMap fileStatusMap = daemonCodeAnalyzer.getFileStatusMap(); final ProgressIndicator progress = ProgressManager.getInstance().getProgressIndicator(); if (progress == null) throw new IllegalStateException("Must be run under progress"); final RefCountHolder refCountHolder = RefCountHolder.get(file); myRefCountHolder = refCountHolder; final Document document = PsiDocumentManager.getInstance(project).getDocument(file); TextRange dirtyScope = ObjectUtils.notNull(document == null ? null : fileStatusMap.getFileDirtyScope(document, Pass.UPDATE_ALL), file.getTextRange()); success = refCountHolder.analyze(file, dirtyScope, progress, new Runnable(){ @Override public void run() { highlight.run(); progress.checkCanceled(); HighlightingSession highlightingSession = HighlightingSessionImpl.getHighlightingSession(file, progress); PostHighlightingVisitor highlightingVisitor = new PostHighlightingVisitor(file, document, refCountHolder); highlightingVisitor.collectHighlights(file, holder, progress); } }); } else { myRefCountHolder = null; highlight.run(); } } finally { myUninitializedVarProblems.clear(); myFinalVarProblems.clear(); mySingleImportedClasses.clear(); mySingleImportedFields.clear(); myReassignedParameters.clear(); myRefCountHolder = null; myFile = null; myHolder = null; myDuplicateMethods.clear(); } return success; }
public static DiamondInferenceResult resolveInferredTypesNoCheck(final PsiNewExpression newExpression, final PsiElement context) { final JavaResolveResult staticFactoryCandidateInfo = getStaticFactory(newExpression, context); if(staticFactoryCandidateInfo == null) { return DiamondInferenceResult.NULL_RESULT; } final PsiSubstitutor inferredSubstitutor = ourDiamondGuard.doPreventingRecursion(context, false, new Computable<PsiSubstitutor>() { @Override public PsiSubstitutor compute() { PsiSubstitutor substitutor = staticFactoryCandidateInfo.getSubstitutor(); return staticFactoryCandidateInfo instanceof MethodCandidateInfo && ((MethodCandidateInfo) staticFactoryCandidateInfo).getInferenceErrorMessage() != null ? null : substitutor; } }); if(inferredSubstitutor == null) { return DiamondInferenceResult.NULL_RESULT; } if(!(staticFactoryCandidateInfo instanceof MethodCandidateInfo)) { return DiamondInferenceResult.UNRESOLVED_CONSTRUCTOR; } //15.9.3 Choosing the Constructor and its Arguments //The return type and throws clause of cj are the same as the return type and throws clause determined for mj (§15.12.2.6) if(InferenceSession.wasUncheckedConversionPerformed(context)) { return DiamondInferenceResult.RAW_RESULT; } final PsiMethod staticFactory = ((MethodCandidateInfo) staticFactoryCandidateInfo).getElement(); final PsiTypeParameter[] parameters = staticFactory.getTypeParameters(); final PsiElement staticFactoryContext = staticFactory.getContext(); final PsiClass psiClass = PsiTreeUtil.getContextOfType(staticFactoryContext, PsiClass.class, false); if(psiClass == null) { LOG.error("failed for expression:" + newExpression); return DiamondInferenceResult.NULL_RESULT; } final PsiTypeParameter[] classParameters = psiClass.getTypeParameters(); final PsiJavaCodeReferenceElement classOrAnonymousClassReference = newExpression.getClassOrAnonymousClassReference(); LOG.assertTrue(classOrAnonymousClassReference != null); final DiamondInferenceResult result = new DiamondInferenceResult(classOrAnonymousClassReference.getReferenceName() + "<>"); if(PsiUtil.isRawSubstitutor(staticFactory, inferredSubstitutor)) { if(!JavaVersionService.getInstance().isAtLeast(newExpression, JavaSdkVersion.JDK_1_8) && PsiUtil.skipParenthesizedExprUp(newExpression.getParent()) instanceof PsiExpressionList) { for(PsiTypeParameter ignored : parameters) { result.addInferredType(PsiType.getJavaLangObject(newExpression.getManager(), GlobalSearchScope.allScope(newExpression.getProject()))); } } return result; } for(PsiTypeParameter parameter : parameters) { for(PsiTypeParameter classParameter : classParameters) { if(Comparing.strEqual(classParameter.getName(), parameter.getName())) { result.addInferredType(inferredSubstitutor.substitute(parameter)); break; } } } return result; }
public static int getJavaVersion(@NotNull PsiElement element) { JavaSdkVersion sdkVersion = JavaVersionService.getInstance().getJavaSdkVersion(element); if(sdkVersion == null) { sdkVersion = JavaSdkVersion.fromLanguageLevel(PsiUtil.getLanguageLevel(element)); } int version = 0; switch(sdkVersion) { case JDK_1_0: case JDK_1_1: version = 1; break; case JDK_1_2: version = 2; break; case JDK_1_3: version = 3; break; case JDK_1_4: version = 4; break; case JDK_1_5: version = 5; break; case JDK_1_6: version = 6; break; case JDK_1_7: version = 7; break; case JDK_1_8: version = 8; break; case JDK_1_9: version = 9; break; } return version; }
@Override public void visitClass(PsiClass aClass) { // Don't go into classes (anonymous, locals). if(!aClass.hasModifierProperty(PsiModifier.ABSTRACT)) { final Module module = ModuleUtilCore.findModuleForPsiElement(aClass); final LanguageLevel effectiveLanguageLevel = module != null ? getEffectiveLanguageLevel(module) : null; if(effectiveLanguageLevel != null && !effectiveLanguageLevel.isAtLeast(LanguageLevel.JDK_1_8)) { final JavaSdkVersion version = JavaVersionService.getInstance().getJavaSdkVersion(aClass); if(version != null && version.isAtLeast(JavaSdkVersion.JDK_1_8)) { final List<PsiMethod> methods = new ArrayList<PsiMethod>(); for(HierarchicalMethodSignature methodSignature : aClass.getVisibleSignatures()) { final PsiMethod method = methodSignature.getMethod(); if(ourDefaultMethods.contains(getSignature(method))) { methods.add(method); } } if(!methods.isEmpty()) { PsiElement element2Highlight = aClass.getNameIdentifier(); if(element2Highlight == null) { element2Highlight = aClass; } myHolder.registerProblem(element2Highlight, methods.size() == 1 ? InspectionsBundle .message("inspection.1.8.problem.single.descriptor", methods.get(0).getName(), getJdkName(effectiveLanguageLevel)) : InspectionsBundle.message ("inspection.1.8.problem.descriptor", methods.size(), getJdkName(effectiveLanguageLevel)), QuickFixFactory.getInstance() .createImplementMethodsFix(aClass)); } } } } }
@Nullable static HighlightInfo checkAccessStaticFieldFromEnumConstructor(@NotNull PsiReferenceExpression expr, @NotNull JavaResolveResult result) { final PsiElement resolved = result.getElement(); if(!(resolved instanceof PsiField)) { return null; } if(!((PsiModifierListOwner) resolved).hasModifierProperty(PsiModifier.STATIC)) { return null; } if(expr.getParent() instanceof PsiSwitchLabelStatement) { return null; } final PsiMember constructorOrInitializer = PsiUtil.findEnclosingConstructorOrInitializer(expr); if(constructorOrInitializer == null) { return null; } if(constructorOrInitializer.hasModifierProperty(PsiModifier.STATIC)) { return null; } final PsiClass aClass = constructorOrInitializer instanceof PsiEnumConstantInitializer ? (PsiClass) constructorOrInitializer : constructorOrInitializer.getContainingClass(); if(aClass == null || !(aClass.isEnum() || aClass instanceof PsiEnumConstantInitializer)) { return null; } final PsiField field = (PsiField) resolved; if(aClass instanceof PsiEnumConstantInitializer) { if(field.getContainingClass() != aClass.getSuperClass()) { return null; } } else if(field.getContainingClass() != aClass) { return null; } if(!JavaVersionService.getInstance().isAtLeast(field, JavaSdkVersion.JDK_1_6)) { final PsiType type = field.getType(); if(type instanceof PsiClassType && ((PsiClassType) type).resolve() == aClass) { return null; } } if(PsiUtil.isCompileTimeConstant((PsiVariable) field)) { return null; } String description = JavaErrorMessages.message("illegal.to.access.static.member.from.enum.constructor.or.instance.initializer", HighlightMessageUtil.getSymbolName(resolved, result .getSubstitutor())); return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expr).descriptionAndTooltip(description).create(); }