{"record":{"id":"083a6d7c29caea2e","repo":"gradle/gradle","slug":"can-not-use-getcredentials-method-when-not-using","errorCode":null,"errorMessage":"Can not use getCredentials() method when not using PasswordCredentials; please use getCredentials(Class)","messagePattern":"Can not use getCredentials\\(\\) method when not using PasswordCredentials; please use getCredentials\\(Class\\)","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"platforms/software/dependency-management/src/main/java/org/gradle/api/internal/artifacts/repositories/AuthenticationSupporter.java","lineNumber":62,"sourceCode":"    private final ProviderFactory providerFactory;\n\n    private final Property<Credentials> credentials;\n    private boolean usesCredentials = false;\n\n    public AuthenticationSupporter(Instantiator instantiator, ObjectFactory objectFactory, AuthenticationContainer authenticationContainer, ProviderFactory providerFactory) {\n        this.instantiator = instantiator;\n        this.authenticationContainer = authenticationContainer;\n        this.credentials = objectFactory.property(Credentials.class);\n        this.providerFactory = providerFactory;\n    }\n\n    public PasswordCredentials getCredentials() {\n        if (!usesCredentials()) {\n            return setCredentials(PasswordCredentials.class);\n        } else if (credentials.get() instanceof PasswordCredentials) {\n            return Cast.uncheckedCast(credentials.get());\n        } else {\n            throw new IllegalStateException(\"Can not use getCredentials() method when not using PasswordCredentials; please use getCredentials(Class)\");\n        }\n    }\n\n    public <T extends Credentials> T getCredentials(Class<T> credentialsType) {\n        if (!usesCredentials()) {\n            return setCredentials(credentialsType);\n        } else if (credentialsType.isInstance(credentials.get())) {\n            return Cast.uncheckedCast(credentials.get());\n        } else {\n            throw new IllegalArgumentException(String.format(\"Given credentials type '%s' does not match actual type '%s'\", credentialsType.getName(), getCredentialsPublicType(credentials.get().getClass()).getName()));\n        }\n    }\n\n    public void credentials(Action<? super PasswordCredentials> action) {\n        if (usesCredentials() && !(credentials.get() instanceof PasswordCredentials)) {\n            throw new IllegalStateException(\"Can not use credentials(Action) method when not using PasswordCredentials; please use credentials(Class, Action)\");\n        }\n        credentials(PasswordCredentials.class, action);","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/gradle/gradle/blob/534f27719b66953f95cc907aae7f2c1b12f5482d/platforms/software/dependency-management/src/main/java/org/gradle/api/internal/artifacts/repositories/AuthenticationSupporter.java#L44-L80","documentation":"The parameterless getCredentials() on Maven/Ivy repositories is a convenience hard-wired to PasswordCredentials: on first call it installs PasswordCredentials, otherwise it returns them. If the repository already uses a different credentials type (e.g. AwsCredentials configured earlier), the instanceof check fails and IllegalStateException directs you to the typed variant getCredentials(Class).","triggerScenarios":"repo.credentials(AwsCredentials.class) { ... } (or repo.getCredentials(AwsCredentials.class)) followed by repo.getCredentials() — for example generic code that introspects repository credentials on an S3 repository.","commonSituations":"Generic repository-auditing plugins calling the no-arg getter on every repository; builds mixing AWS and password credentials; migrations from password-only setups where the no-arg getter used to work.","solutions":["Use the typed getter matching the configured type: repo.getCredentials(AwsCredentials.class).","In generic code, inspect the current credentials via repo.getCredentials(Credentials.class) — the base type always matches — and branch with instanceof.","If only username/password are needed everywhere, standardize repositories on PasswordCredentials."],"exampleFix":"// before\nrepo.credentials(AwsCredentials.class) { accessKey = 'key'; secretKey = 'secret' }\ndef user = repo.credentials.username // implicit getCredentials() -> IllegalStateException\n// after\ndef aws = repo.getCredentials(AwsCredentials.class)\nprintln aws.accessKey","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"static boolean usesPasswordCredentials(MavenArtifactRepository repo) {\n    return repo.getCredentials(Credentials.class) instanceof PasswordCredentials\n}","tryCatchPattern":null,"preventionTips":["In generic code, always fetch getCredentials(Credentials.class) and branch with instanceof","Never assume the no-arg getCredentials() works on every repository","Pick one credentials style per repository and stay consistent"],"tags":["gradle","repositories","credentials","aws","api-misuse"],"backgroundTag":"credentials-type-mismatch","analyzedSha":"534f27719b66953f95cc907aae7f2c1b12f5482d","analyzedAt":"2026-08-22T08:09:12.375Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}