Skip to content

Commit

Permalink
ignore private methods and non-settable properties (#27)
Browse files Browse the repository at this point in the history
Stricter filtering on what is and isn't settable.
  • Loading branch information
felixdesouza committed Apr 14, 2023
1 parent df9d3e2 commit 40aefda
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public final class AutoParallelizableProcessor extends AbstractProcessor {
private static final Set<String> SETTABLE_PROPERTY_CLASSES = Set.of(
"org.gradle.api.provider.Property",
"org.gradle.api.provider.HasMultipleValues",
"org.gradle.api.provider.MapProperty");
"org.gradle.api.provider.MapProperty",
"org.gradle.api.file.ConfigurableFileCollection");

@Override
public Set<String> getSupportedAnnotationTypes() {
Expand Down Expand Up @@ -312,11 +313,14 @@ private void handleParamsLikeElement(
.filter(element -> element.getKind().equals(ElementKind.METHOD))
.map(ExecutableElement.class::cast)
.forEach(possibleMethod -> {
if (possibleMethod.getModifiers().contains(Modifier.DEFAULT)) {
if (possibleMethod.getModifiers().contains(Modifier.DEFAULT)
|| possibleMethod.getModifiers().contains(Modifier.PRIVATE)
|| !possibleMethod.getParameters().isEmpty()
|| (!isNested(possibleMethod) && !returnsSettableProperty(possibleMethod))) {
return;
}

if (isNested(possibleMethod) && doesNotReturnASettableProperty(possibleMethod)) {
if (isNested(possibleMethod) && !returnsSettableProperty(possibleMethod)) {
TypeElement nestedParamsLikeElement = MoreTypes.asTypeElement(possibleMethod.getReturnType());
String newContextSuffix = possibleMethod.getSimpleName().toString() + "()";
handleParamsLikeElement(
Expand All @@ -342,12 +346,12 @@ private void handleParamsLikeElement(
});
}

private boolean doesNotReturnASettableProperty(ExecutableElement method) {
private boolean returnsSettableProperty(ExecutableElement method) {
Set<TypeElement> settablePropertyElements = SETTABLE_PROPERTY_CLASSES.stream()
.map(className -> processingEnv.getElementUtils().getTypeElement(className))
.collect(Collectors.toSet());
Set<TypeElement> returnTypeHierarchyElements = allSuperTypeElements(method.getReturnType());
return Sets.intersection(settablePropertyElements, returnTypeHierarchyElements)
return !Sets.intersection(settablePropertyElements, returnTypeHierarchyElements)
.isEmpty();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* (c) Copyright 2023 Palantir Technologies Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package app;

import com.palantir.gradle.autoparallelizable.AutoParallelizable;
import org.gradle.api.provider.ListProperty;
import org.gradle.api.provider.MapProperty;
import org.gradle.api.provider.Property;
import org.gradle.api.provider.SetProperty;
import org.gradle.api.tasks.Nested;

@AutoParallelizable
public final class Abstract {
public abstract class AbstractTask extends AbstractTaskImpl {
public AbstractTask() {
setDescription("lol");
}
}

interface Nested {
public Property<String> getString();
}

abstract static class AbstractParams {
public abstract Property<String> getSettableNonNestedString();

public abstract Property<String> getStringWithParameters(String parameter);

private Property<String> getPrivateProperty() {
return null;
}

public abstract Nested getNonNestedNonSettableProperty();
}

interface Params {
@org.gradle.api.tasks.Nested
AbstractParams getAbstractParams();
}

static void action(Params params) {
System.out.println("Hello "
+ params.getAbstractParams().getSettableNonNestedString().get());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package app;

import javax.annotation.processing.Generated;
import javax.inject.Inject;
import org.gradle.api.DefaultTask;
import org.gradle.api.tasks.TaskAction;
import org.gradle.workers.WorkerExecutor;

@Generated("com.palantir.gradle.autoparallelizable.AutoParallelizableProcessor")
abstract class AbstractTaskImpl extends DefaultTask implements Abstract.Params {
@Inject
protected abstract WorkerExecutor getWorkerExecutor();

@TaskAction
public final void execute() {
getWorkerExecutor().noIsolation().submit(AbstractWorkAction.class, params -> {
params.getAbstractParams()
.getSettableNonNestedString()
.set(this.getAbstractParams().getSettableNonNestedString());
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package app;

import javax.annotation.processing.Generated;
import org.gradle.workers.WorkAction;

@Generated("com.palantir.gradle.autoparallelizable.AutoParallelizableProcessor")
abstract class AbstractWorkAction implements WorkAction<AbstractWorkParams> {
@SuppressWarnings("RedundantModifier")
public AbstractWorkAction() {}

@Override
public final void execute() {
Abstract.action(getParameters());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package app;

import javax.annotation.processing.Generated;
import org.gradle.workers.WorkParameters;

@Generated("com.palantir.gradle.autoparallelizable.AutoParallelizableProcessor")
interface AbstractWorkParams extends WorkParameters, Abstract.Params {}
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-27.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: Stricter filtering on what is and isn't settable.
links:
- https://github.com/palantir/auto-parallelizable/pull/27

0 comments on commit 40aefda

Please sign in to comment.