Search in sources :

Example 1 with Function1

use of kotlin.jvm.functions.Function1 in project kotlin by JetBrains.

the class DescriptorResolverUtils method resolveOverrides.

@NotNull
private static <D extends CallableMemberDescriptor> Collection<D> resolveOverrides(@NotNull Name name, @NotNull Collection<D> membersFromSupertypes, @NotNull Collection<D> membersFromCurrent, @NotNull ClassDescriptor classDescriptor, @NotNull final ErrorReporter errorReporter, final boolean isStaticContext) {
    final Set<D> result = new LinkedHashSet<D>();
    OverridingUtil.generateOverridesInFunctionGroup(name, membersFromSupertypes, membersFromCurrent, classDescriptor, new NonReportingOverrideStrategy() {

        @Override
        @SuppressWarnings("unchecked")
        public void addFakeOverride(@NotNull CallableMemberDescriptor fakeOverride) {
            OverridingUtil.resolveUnknownVisibilityForMember(fakeOverride, new Function1<CallableMemberDescriptor, Unit>() {

                @Override
                public Unit invoke(@NotNull CallableMemberDescriptor descriptor) {
                    errorReporter.reportCannotInferVisibility(descriptor);
                    return Unit.INSTANCE;
                }
            });
            result.add((D) fakeOverride);
        }

        @Override
        public void conflict(@NotNull CallableMemberDescriptor fromSuper, @NotNull CallableMemberDescriptor fromCurrent) {
        // nop
        }

        @Override
        public void setOverriddenDescriptors(@NotNull CallableMemberDescriptor member, @NotNull Collection<? extends CallableMemberDescriptor> overridden) {
            // do not set overridden descriptors for declared static fields and methods from java
            if (isStaticContext && member.getKind() != CallableMemberDescriptor.Kind.FAKE_OVERRIDE) {
                return;
            }
            super.setOverriddenDescriptors(member, overridden);
        }
    });
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Function1(kotlin.jvm.functions.Function1) NonReportingOverrideStrategy(org.jetbrains.kotlin.resolve.NonReportingOverrideStrategy) NotNull(org.jetbrains.annotations.NotNull) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with Function1

use of kotlin.jvm.functions.Function1 in project kotlin by JetBrains.

the class MoveKotlinNestedClassesToUpperLevelDialog method doAction.

@Override
protected void doAction() {
    PsiElement target;
    try {
        target = getTargetContainerWithValidation();
        if (target == null)
            return;
    } catch (ConfigurationException e) {
        CommonRefactoringUtil.showErrorMessage(MoveInnerImpl.REFACTORING_NAME, e.getMessage(), HelpID.MOVE_INNER_UPPER, project);
        return;
    }
    KotlinRefactoringSettings settings = KotlinRefactoringSettings.getInstance();
    settings.MOVE_TO_UPPER_LEVEL_SEARCH_FOR_TEXT = searchForTextOccurrencesCheckBox.isSelected();
    settings.MOVE_TO_UPPER_LEVEL_SEARCH_IN_COMMENTS = searchInCommentsCheckBox.isSelected();
    KotlinMoveTarget moveTarget;
    if (target instanceof PsiDirectory) {
        final PsiDirectory targetDir = (PsiDirectory) target;
        final FqName targetPackageFqName = getTargetPackageFqName();
        if (targetPackageFqName == null)
            return;
        String innerClassName = innerClass.getName();
        if (innerClassName == null)
            return;
        final String targetFileName = KotlinNameSuggester.INSTANCE.suggestNameByName(innerClassName, new Function1<String, Boolean>() {

            @Override
            public Boolean invoke(String s) {
                return targetDir.findFile(s + "." + KotlinFileType.EXTENSION) == null;
            }
        }) + "." + KotlinFileType.EXTENSION;
        moveTarget = new KotlinMoveTargetForDeferredFile(targetPackageFqName, targetDir, null, new Function1<KtFile, KtFile>() {

            @Override
            public KtFile invoke(@NotNull KtFile originalFile) {
                return KotlinRefactoringUtilKt.createKotlinFile(targetFileName, targetDir, targetPackageFqName.asString());
            }
        });
    } else {
        //noinspection ConstantConditions
        moveTarget = new KotlinMoveTargetForExistingElement((KtElement) target);
    }
    String outerInstanceParameterName = passOuterClassCheckBox.isSelected() ? getParameterName() : null;
    String newClassName = getClassName();
    MoveDeclarationsDelegate delegate = new MoveDeclarationsDelegate.NestedClass(newClassName, outerInstanceParameterName);
    MoveDeclarationsDescriptor moveDescriptor = new MoveDeclarationsDescriptor(CollectionsKt.listOf(innerClass), moveTarget, delegate, isSearchInComments(), isSearchInNonJavaFiles(), true, false, null, isOpenInEditor());
    saveOpenInEditorOption();
    invokeRefactoring(new MoveKotlinDeclarationsProcessor(project, moveDescriptor, Mover.Default.INSTANCE));
}
Also used : FqName(org.jetbrains.kotlin.name.FqName) Function1(kotlin.jvm.functions.Function1) NotNull(org.jetbrains.annotations.NotNull) ConfigurationException(com.intellij.openapi.options.ConfigurationException) KotlinRefactoringSettings(org.jetbrains.kotlin.idea.refactoring.KotlinRefactoringSettings)

Example 3 with Function1

use of kotlin.jvm.functions.Function1 in project kotlin by JetBrains.

the class AbstractQuickFixTest method configureLocalInspectionTools.

@NotNull
@Override
protected LocalInspectionTool[] configureLocalInspectionTools() {
    if (KotlinTestUtils.isAllFilesPresentTest(getTestName(false)))
        return super.configureLocalInspectionTools();
    String testRoot = KotlinTestUtils.getTestsRoot(this.getClass());
    String configFileText = FilesKt.readText(new File(testRoot, getTestName(true) + ".kt"), Charset.defaultCharset());
    List<String> toolsStrings = InTextDirectivesUtils.findListWithPrefixes(configFileText, "TOOL:");
    if (toolsStrings.isEmpty())
        return super.configureLocalInspectionTools();
    return ArrayUtil.toObjectArray(CollectionsKt.map(toolsStrings, new Function1<String, LocalInspectionTool>() {

        @Override
        public LocalInspectionTool invoke(String toolFqName) {
            try {
                Class<?> aClass = Class.forName(toolFqName);
                return (LocalInspectionTool) aClass.newInstance();
            } catch (Exception e) {
                throw new IllegalArgumentException("Failed to create inspection for key '" + toolFqName + "'", e);
            }
        }
    }), LocalInspectionTool.class);
}
Also used : Function1(kotlin.jvm.functions.Function1) KtFile(org.jetbrains.kotlin.psi.KtFile) File(java.io.File) LocalInspectionTool(com.intellij.codeInspection.LocalInspectionTool) IOException(java.io.IOException) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with Function1

use of kotlin.jvm.functions.Function1 in project kotlin by JetBrains.

the class BodyResolver method resolveFunctionBody.

private void resolveFunctionBody(@NotNull DataFlowInfo outerDataFlowInfo, @NotNull BindingTrace trace, @NotNull KtDeclarationWithBody function, @NotNull FunctionDescriptor functionDescriptor, @NotNull LexicalScope scope, @Nullable Function1<LexicalScope, DataFlowInfo> beforeBlockBody, // Creates wrapper scope for header resolution if necessary (see resolveSecondaryConstructorBody)
@Nullable Function1<LexicalScope, LexicalScope> headerScopeFactory) {
    PreliminaryDeclarationVisitor.Companion.createForDeclaration(function, trace);
    LexicalScope innerScope = FunctionDescriptorUtil.getFunctionInnerScope(scope, functionDescriptor, trace, overloadChecker);
    List<KtParameter> valueParameters = function.getValueParameters();
    List<ValueParameterDescriptor> valueParameterDescriptors = functionDescriptor.getValueParameters();
    LexicalScope headerScope = headerScopeFactory != null ? headerScopeFactory.invoke(innerScope) : innerScope;
    valueParameterResolver.resolveValueParameters(valueParameters, valueParameterDescriptors, headerScope, outerDataFlowInfo, trace);
    // Synthetic "field" creation
    if (functionDescriptor instanceof PropertyAccessorDescriptor && functionDescriptor.getExtensionReceiverParameter() == null) {
        PropertyAccessorDescriptor accessorDescriptor = (PropertyAccessorDescriptor) functionDescriptor;
        KtProperty property = (KtProperty) function.getParent();
        final SyntheticFieldDescriptor fieldDescriptor = new SyntheticFieldDescriptor(accessorDescriptor, property);
        innerScope = new LexicalScopeImpl(innerScope, functionDescriptor, true, null, LexicalScopeKind.PROPERTY_ACCESSOR_BODY, LocalRedeclarationChecker.DO_NOTHING.INSTANCE, new Function1<LexicalScopeImpl.InitializeHandler, Unit>() {

            @Override
            public Unit invoke(LexicalScopeImpl.InitializeHandler handler) {
                handler.addVariableDescriptor(fieldDescriptor);
                return Unit.INSTANCE;
            }
        });
        // Check parameter name shadowing
        for (KtParameter parameter : function.getValueParameters()) {
            if (SyntheticFieldDescriptor.NAME.equals(parameter.getNameAsName())) {
                trace.report(Errors.ACCESSOR_PARAMETER_NAME_SHADOWING.on(parameter));
            }
        }
    }
    DataFlowInfo dataFlowInfo = null;
    if (beforeBlockBody != null) {
        dataFlowInfo = beforeBlockBody.invoke(headerScope);
    }
    if (function.hasBody()) {
        expressionTypingServices.checkFunctionReturnType(innerScope, function, functionDescriptor, dataFlowInfo != null ? dataFlowInfo : outerDataFlowInfo, null, trace);
    }
    assert functionDescriptor.getReturnType() != null;
}
Also used : SyntheticFieldDescriptor(org.jetbrains.kotlin.descriptors.impl.SyntheticFieldDescriptor) Function1(kotlin.jvm.functions.Function1) DataFlowInfo(org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo)

Example 5 with Function1

use of kotlin.jvm.functions.Function1 in project kotlin by JetBrains.

the class LoadBuiltinsTest method testBuiltIns.

public void testBuiltIns() throws Exception {
    RecursiveDescriptorComparator.Configuration configuration = RecursiveDescriptorComparator.RECURSIVE_ALL.includeMethodsOfKotlinAny(false).withRenderer(DescriptorRenderer.Companion.withOptions(new Function1<DescriptorRendererOptions, Unit>() {

        @Override
        public Unit invoke(DescriptorRendererOptions options) {
            options.setWithDefinedIn(false);
            options.setOverrideRenderingPolicy(OverrideRenderingPolicy.RENDER_OPEN_OVERRIDE);
            options.setVerbose(true);
            options.setAnnotationArgumentsRenderingPolicy(AnnotationArgumentsRenderingPolicy.UNLESS_EMPTY);
            options.setModifiers(DescriptorRendererModifier.ALL);
            return Unit.INSTANCE;
        }
    }));
    PackageFragmentProvider packageFragmentProvider = createBuiltInsPackageFragmentProvider();
    List<KtFile> files = KotlinTestUtils.loadToJetFiles(getEnvironment(), ContainerUtil.concat(allFilesUnder("core/builtins/native"), allFilesUnder("core/builtins/src")));
    ModuleDescriptor module = LazyResolveTestUtilsKt.createResolveSessionForFiles(getEnvironment().getProject(), files, false).getModuleDescriptor();
    for (FqName packageFqName : CollectionsKt.listOf(BUILT_INS_PACKAGE_FQ_NAME, COLLECTIONS_PACKAGE_FQ_NAME, RANGES_PACKAGE_FQ_NAME)) {
        PackageFragmentDescriptor fromLazyResolve = CollectionsKt.single(module.getPackage(packageFqName).getFragments());
        if (fromLazyResolve instanceof LazyPackageDescriptor) {
            PackageFragmentDescriptor deserialized = CollectionsKt.single(packageFragmentProvider.getPackageFragments(packageFqName));
            RecursiveDescriptorComparator.validateAndCompareDescriptors(fromLazyResolve, deserialized, configuration, new File("compiler/testData/builtin-classes/default/" + packageFqName.asString().replace('.', '-') + ".txt"));
        }
    }
}
Also used : RecursiveDescriptorComparator(org.jetbrains.kotlin.test.util.RecursiveDescriptorComparator) ModuleDescriptor(org.jetbrains.kotlin.descriptors.ModuleDescriptor) PackageFragmentProvider(org.jetbrains.kotlin.descriptors.PackageFragmentProvider) BuiltInsPackageFragmentProviderKt.createBuiltInPackageFragmentProvider(org.jetbrains.kotlin.builtins.BuiltInsPackageFragmentProviderKt.createBuiltInPackageFragmentProvider) FqName(org.jetbrains.kotlin.name.FqName) Function1(kotlin.jvm.functions.Function1) LazyPackageDescriptor(org.jetbrains.kotlin.resolve.lazy.descriptors.LazyPackageDescriptor) KtFile(org.jetbrains.kotlin.psi.KtFile) PackageFragmentDescriptor(org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor) KtFile(org.jetbrains.kotlin.psi.KtFile) File(java.io.File)

Aggregations

Function1 (kotlin.jvm.functions.Function1)7 NotNull (org.jetbrains.annotations.NotNull)4 File (java.io.File)2 FqName (org.jetbrains.kotlin.name.FqName)2 KtFile (org.jetbrains.kotlin.psi.KtFile)2 LocalInspectionTool (com.intellij.codeInspection.LocalInspectionTool)1 ConfigurationException (com.intellij.openapi.options.ConfigurationException)1 Pair (com.intellij.openapi.util.Pair)1 IOException (java.io.IOException)1 LinkedHashSet (java.util.LinkedHashSet)1 CompilerMessage (org.codehaus.plexus.compiler.CompilerMessage)1 BuiltInsPackageFragmentProviderKt.createBuiltInPackageFragmentProvider (org.jetbrains.kotlin.builtins.BuiltInsPackageFragmentProviderKt.createBuiltInPackageFragmentProvider)1 CompilerMessageLocation (org.jetbrains.kotlin.cli.common.messages.CompilerMessageLocation)1 ModuleDescriptor (org.jetbrains.kotlin.descriptors.ModuleDescriptor)1 PackageFragmentDescriptor (org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor)1 PackageFragmentProvider (org.jetbrains.kotlin.descriptors.PackageFragmentProvider)1 EmptyPackageFragmentDescriptor (org.jetbrains.kotlin.descriptors.impl.EmptyPackageFragmentDescriptor)1 PackageFragmentDescriptorImpl (org.jetbrains.kotlin.descriptors.impl.PackageFragmentDescriptorImpl)1 SyntheticFieldDescriptor (org.jetbrains.kotlin.descriptors.impl.SyntheticFieldDescriptor)1 KotlinRefactoringSettings (org.jetbrains.kotlin.idea.refactoring.KotlinRefactoringSettings)1