Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static dagger.internal.codegen.binding.SourceFiles.simpleVariableName;
import static dagger.internal.codegen.xprocessing.XElements.getSimpleName;
import static dagger.internal.codegen.xprocessing.XElements.hasAnyAnnotation;
import static dagger.internal.codegen.xprocessing.XElements.isPrivate;
import static dagger.internal.codegen.xprocessing.XTypeElements.isNested;
import static dagger.internal.codegen.xprocessing.XTypes.isDeclared;

Expand Down Expand Up @@ -254,6 +255,6 @@ private static boolean requiresEnclosingInstance(XTypeElement typeElement) {

private static boolean hasVisibleDefaultConstructor(XTypeElement typeElement) {
return typeElement.getConstructors().stream()
.anyMatch(constructor -> !constructor.isPrivate() && constructor.getParameters().isEmpty());
.anyMatch(constructor -> !isPrivate(constructor) && constructor.getParameters().isEmpty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package dagger.internal.codegen.validation;

import static dagger.internal.codegen.xprocessing.XElements.hasAnyAnnotation;
import static dagger.internal.codegen.xprocessing.XElements.isPrivate;
import static dagger.internal.codegen.xprocessing.XMethodElements.getEnclosingTypeElement;
import static dagger.internal.codegen.xprocessing.XMethodElements.hasTypeParameters;
import static dagger.internal.codegen.xprocessing.XTypes.isSubtype;
Expand Down Expand Up @@ -189,7 +190,7 @@ private void checkTypeParameters() {

/** Adds an error if the method is private. */
private void checkNotPrivate() {
if (method.isPrivate()) {
if (isPrivate(method)) {
report.addError(bindingMethods("cannot be private"));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static com.google.common.collect.Iterables.getOnlyElement;
import static dagger.internal.codegen.base.ComponentCreatorAnnotation.getCreatorAnnotations;
import static dagger.internal.codegen.base.Util.reentrantComputeIfAbsent;
import static dagger.internal.codegen.xprocessing.XElements.isPrivate;
import static dagger.internal.codegen.xprocessing.XMethodElements.hasTypeParameters;
import static dagger.internal.codegen.xprocessing.XTypeElements.getAllUnimplementedMethods;
import static dagger.internal.codegen.xprocessing.XTypeElements.hasTypeParameters;
Expand Down Expand Up @@ -197,7 +198,7 @@ private boolean validateTypeRequirements() {
report.addError(messages.generics());
isClean = false;
}
if (creator.isPrivate()) {
if (isPrivate(creator)) {
report.addError(messages.isPrivate());
isClean = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import static dagger.internal.codegen.binding.SourceFiles.membersInjectorNameForType;
import static dagger.internal.codegen.extension.DaggerStreams.toImmutableList;
import static dagger.internal.codegen.xprocessing.XElements.closestEnclosingTypeElement;
import static dagger.internal.codegen.xprocessing.XElements.isPrivate;
import static dagger.internal.codegen.xprocessing.XMethodElements.hasTypeParameters;
import static dagger.internal.codegen.xprocessing.XTypeElements.isEffectivelyPrivate;
import static dagger.internal.codegen.xprocessing.XTypes.isSubtype;
Expand Down Expand Up @@ -213,7 +214,7 @@ private ValidationReport validateConstructor(XConstructorElement constructorElem
"No @Inject or @AssistedInject annotation found: " + constructorElement);
}

if (constructorElement.isPrivate()) {
if (isPrivate(constructorElement)) {
builder.addError(
"Dagger does not support injection into private constructors", constructorElement);
}
Expand Down Expand Up @@ -328,7 +329,7 @@ private ValidationReport validateField(XFieldElement fieldElement) {
builder.addError("@Inject fields may not be final", fieldElement);
}

if (fieldElement.isPrivate()) {
if (isPrivate(fieldElement)) {
builder.addItem(
"Dagger does not support injection into private fields",
privateMemberDiagnosticKind,
Expand Down Expand Up @@ -361,7 +362,7 @@ private ValidationReport validateMethod(XMethodElement methodElement) {
builder.addError("Methods with @Inject may not be abstract", methodElement);
}

if (methodElement.isPrivate()) {
if (isPrivate(methodElement)) {
builder.addItem(
"Dagger does not support injection into private methods",
privateMemberDiagnosticKind,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import static dagger.internal.codegen.xprocessing.XAnnotations.getClassName;
import static dagger.internal.codegen.xprocessing.XElements.getSimpleName;
import static dagger.internal.codegen.xprocessing.XElements.hasAnyAnnotation;
import static dagger.internal.codegen.xprocessing.XElements.isPrivate;
import static dagger.internal.codegen.xprocessing.XTypeElements.hasTypeParameters;
import static dagger.internal.codegen.xprocessing.XTypeElements.isEffectivelyPrivate;
import static dagger.internal.codegen.xprocessing.XTypeElements.isEffectivelyPublic;
Expand Down Expand Up @@ -538,7 +539,7 @@ private void validateBindingMethodOverrides(

private void validateModuleVisibility(
XTypeElement moduleElement, ModuleKind moduleKind, ValidationReport.Builder reportBuilder) {
if (moduleElement.isPrivate() || moduleElement.isKtPrivate()) {
if (isPrivate(moduleElement)) {
reportBuilder.addError("Modules cannot be private.", moduleElement);
} else if (isEffectivelyPrivate(moduleElement)) {
reportBuilder.addError("Modules cannot be enclosed in private types.", moduleElement);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ public static boolean isPublic(XElement element) {
}

public static boolean isPrivate(XElement element) {
return asHasModifiers(element).isPrivate();
XHasModifiers modifiers = asHasModifiers(element);
return modifiers.isPrivate() || modifiers.isKtPrivate();
}

public static boolean isInternal(XElement element) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.Iterables.getOnlyElement;
import static dagger.internal.codegen.extension.DaggerStreams.toImmutableList;
import static dagger.internal.codegen.xprocessing.XElements.isPrivate;
import static java.util.stream.Collectors.joining;
import static kotlin.streams.jdk8.StreamsKt.asStream;

Expand Down Expand Up @@ -50,7 +51,7 @@ private enum Visibility {
/** Returns the visibility of the given {@link XTypeElement}. */
private static Visibility of(XTypeElement element) {
checkNotNull(element);
if (element.isPrivate()) {
if (isPrivate(element)) {
return Visibility.PRIVATE;
} else if (element.isPublic()) {
return Visibility.PUBLIC;
Expand Down Expand Up @@ -88,7 +89,7 @@ public static ImmutableList<XMethodElement> getAllUnimplementedMethods(XTypeElem
/** Returns all non-private, non-static methods in {@code type}. */
public static ImmutableList<XMethodElement> getAllNonPrivateInstanceMethods(XTypeElement type) {
return getAllMethods(type).stream()
.filter(method -> !method.isPrivate() && !method.isStatic())
.filter(method -> !isPrivate(method) && !method.isStatic())
.collect(toImmutableList());
}

Expand All @@ -110,7 +111,7 @@ private static boolean isAccessibleFrom(XMethodElement method, XTypeElement type
if (method.isPublic() || method.isProtected()) {
return true;
}
if (method.isPrivate()) {
if (isPrivate(method)) {
return false;
}
return method
Expand Down
29 changes: 10 additions & 19 deletions javatests/dagger/internal/codegen/PrivateTopLevelClassTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,25 +60,16 @@ public void testPrivateKotlinClass() {
.withProcessingOptions(compilerMode.processorOptions())
.compile(
subject -> {
switch (CompilerTests.backend(subject)) {
case JAVAC:
// TODO: b/539661501 - This should fail once this bug is fixed.
subject.hasErrorCount(0);
break;
case KSP:
subject.hasErrorCount(2);
subject
.hasErrorContaining("Dagger does not support injection into private classes")
.onSource(src)
.onLineContaining("private class Foo");
subject
.hasErrorContaining(
"Foo cannot be provided without an @Inject constructor or an"
+ " @Provides-annotated method")
.onSource(src)
.onLineContaining("interface TestComponent");
break;
}
subject.hasErrorCount(2);
subject
.hasErrorContaining("Dagger does not support injection into private classes")
.onSource(src)
.onLineContaining("private class Foo");
subject
.hasErrorContaining(
"Foo cannot be provided without an @Inject constructor or an"
+ " @Provides-annotated method")
.onSource(src);
});
}
}
Loading