Skip to content

Commit

Permalink
Merge branch 'main' into for-each-final-var-JavaTemplate-issue
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek authored Dec 27, 2024
2 parents 4a4f9d9 + b91499c commit af8665d
Show file tree
Hide file tree
Showing 74 changed files with 789 additions and 593 deletions.
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.11.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.12-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionSha256Sum=f397b287023acdba1e9f6fc5ea72d22dd63669d59ed4a289a29b1a76eee151c6
distributionSha256Sum=7a00d51fb93147819aab76024feece20b6b84e420694101f276be952e08bef03
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public Collection<Reference> findMatches(Reference.Matcher matcher, Reference.Ki
private List<Reference> findMatchesInternal(Reference.Matcher matcher, Reference.@Nullable Kind kind) {
List<Reference> list = new ArrayList<>();
for (Reference ref : references) {
if ((kind == null || ref.getKind().equals(kind)) && ref.matches(matcher) ) {
if ((kind == null || ref.getKind() == kind) && ref.matches(matcher) ) {
list.add(ref);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ private Collection<Map<String, Object>> loadResources(ResourceType resourceType)
for (Object resource : yaml.loadAll(yamlSource)) {
if (resource instanceof Map) {
@SuppressWarnings("unchecked") Map<String, Object> resourceMap = (Map<String, Object>) resource;
if (resourceType.equals(ResourceType.fromSpec((String) resourceMap.get("type")))) {
if (resourceType == ResourceType.fromSpec((String) resourceMap.get("type"))) {
resources.add(resourceMap);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,18 @@ public class ReflectionUtils {
* Cache for {@link Class#getDeclaredMethods()} plus equivalent default methods
* from Java 8 based interfaces, allowing for fast iteration.
*/
private static final Map<Class<?>, Method[]> declaredMethodsCache = new ConcurrentHashMap<>(256);
private static final Map<Class<?>, Method[]> DECLARED_METHODS_CACHE = new ConcurrentHashMap<>(256);

public static boolean isClassAvailable(String fullyQualifiedClassName) {
try {
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
ClassLoader classLoader = contextClassLoader == null ? ReflectionUtils.class.getClassLoader() : contextClassLoader;
Class.forName(fullyQualifiedClassName, false, classLoader);
return true;
} catch (ClassNotFoundException e) {
return false;
}
}

public static @Nullable Method findMethod(Class<?> clazz, String name, Class<?>... paramTypes) {
Class<?> searchType = clazz;
Expand All @@ -54,7 +65,7 @@ public class ReflectionUtils {
}

private static Method[] getDeclaredMethods(Class<?> clazz) {
Method[] result = declaredMethodsCache.get(clazz);
Method[] result = DECLARED_METHODS_CACHE.get(clazz);
if (result == null) {
try {
Method[] declaredMethods = clazz.getDeclaredMethods();
Expand All @@ -70,7 +81,7 @@ private static Method[] getDeclaredMethods(Class<?> clazz) {
} else {
result = declaredMethods;
}
declaredMethodsCache.put(clazz, (result.length == 0 ? EMPTY_METHOD_ARRAY : result));
DECLARED_METHODS_CACHE.put(clazz, (result.length == 0 ? EMPTY_METHOD_ARRAY : result));
} catch (Throwable ex) {
throw new IllegalStateException("Failed to introspect Class [" + clazz.getName() +
"] from ClassLoader [" + clazz.getClassLoader() + "]", ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -720,14 +720,4 @@ public static String formatUriForPropertiesFile(String uri) {
public static boolean hasLineBreak(@Nullable String s) {
return s != null && LINE_BREAK.matcher(s).find();
}

public static boolean containsWhitespace(String s) {
for (int i = 0; i < s.length(); ++i) {
if (Character.isWhitespace(s.charAt(i))) {
return true;
}
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.UUID;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -92,6 +93,9 @@ public InputStream getInputStream(ExecutionContext ctx) {
try {
Path localArchive = cache.compute(uri, () -> {
//noinspection resource
if ("file".equals(uri.getScheme())) {
return Files.newInputStream(Paths.get(uri));
}
HttpSender.Response response = httpSender.send(httpSender.get(uri.toString()).build());
if (response.isSuccessful()) {
return response.getBody();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* 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
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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 org.openrewrite.internal;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class ReflectionUtilsTest {

@Test
void classAvailable() {
boolean result = ReflectionUtils.isClassAvailable("org.openrewrite.internal.ReflectionUtilsTest");
assertTrue(result);
}

@Test
void classNotAvailable() {
boolean result = ReflectionUtils.isClassAvailable("org.openrewrite.internal.ReflectionUtilsTest2");
assertFalse(result);
}

@Test
void classNotAvailableWhenFQNOmitted() {
boolean result = ReflectionUtils.isClassAvailable("ReflectionUtilsTest");
assertFalse(result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
import org.openrewrite.ExecutionContext;
import org.openrewrite.HttpSenderExecutionContextView;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.PrintOutputCapture;
import org.openrewrite.test.MockHttpSender;

import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Paths;
import java.util.concurrent.*;
Expand All @@ -40,10 +42,6 @@ class RemoteArchiveTest {
void gradleWrapper(String version) throws Exception {
URL distributionUrl = requireNonNull(RemoteArchiveTest.class.getClassLoader().getResource("gradle-" + version + "-bin.zip"));
ExecutionContext ctx = new InMemoryExecutionContext();
RemoteExecutionContextView.view(ctx).setArtifactCache(new LocalRemoteArtifactCache(
Paths.get(System.getProperty("user.home") + "/.rewrite/remote/gradleWrapper")));
HttpSenderExecutionContextView.view(ctx)
.setLargeFileHttpSender(new MockHttpSender(distributionUrl::openStream));

RemoteArchive remoteArchive = Remote
.builder(
Expand All @@ -58,10 +56,9 @@ void gradleWrapper(String version) throws Exception {

@Test
void gradleWrapperDownloadFails() throws Exception {
URL distributionUrl = requireNonNull(RemoteArchiveTest.class.getClassLoader().getResource("gradle-7.4.2-bin.zip"));
URL distributionUrl = new URL("http://example");
ExecutionContext ctx = new InMemoryExecutionContext();
RemoteExecutionContextView.view(ctx).setArtifactCache(new LocalRemoteArtifactCache(
Paths.get(System.getProperty("user.home") + "/.rewrite/remote/gradleWrapperDownloadFails")));

HttpSenderExecutionContextView.view(ctx)
.setLargeFileHttpSender(new MockHttpSender(408));

Expand Down Expand Up @@ -116,6 +113,21 @@ void gradleWrapperConcurrent(String version) throws Exception {
executorService.shutdown();
}

@Test
void printingRemoteArchive() throws URISyntaxException {
URL zipUrl = requireNonNull(RemoteArchiveTest.class.getClassLoader().getResource("zipfile.zip"));

RemoteArchive remoteArchive = Remote
.builder(
Paths.get("content.txt"),
zipUrl.toURI()
)
.build("content.txt");

String printed = remoteArchive.printAll(new PrintOutputCapture<>(0, PrintOutputCapture.MarkerPrinter.DEFAULT));
assertThat(printed).isEqualTo("this is a zipped file");
}

private Long getInputStreamSize(InputStream is) {
BlackHoleOutputStream out = new BlackHoleOutputStream();
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,8 @@ void gradleWrapperProperties() throws Exception {

@Test
void gradleWrapperDownloadFails() throws Exception {
URL distributionUrl = requireNonNull(RemoteFileTest.class.getClassLoader().getResource("gradle-wrapper.properties"));
URL distributionUrl = new URL("http://example.com");
ExecutionContext ctx = new InMemoryExecutionContext();
RemoteExecutionContextView.view(ctx).setArtifactCache(new LocalRemoteArtifactCache(
Paths.get(System.getProperty("user.home") + "/.rewrite/remote/gradleWrapperDownloadFails")));
HttpSenderExecutionContextView.view(ctx)
.setLargeFileHttpSender(new MockHttpSender(408));

Expand Down
Binary file added rewrite-core/src/test/resources/zipfile.zip
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ private boolean matchesComparator(@Nullable String managedVersion, String reques
if (managedVersion == null) {
return false;
}
if (comparator.equals(Comparator.ANY)) {
if (comparator == Comparator.ANY) {
return true;
}
if (!isExact(managedVersion)) {
Expand All @@ -301,11 +301,11 @@ private boolean matchesComparator(@Nullable String managedVersion, String reques
int comparison = new LatestIntegration(null)
.compare(null, managedVersion, requestedVersion);
if (comparison < 0) {
return comparator.equals(Comparator.LT) || comparator.equals(Comparator.LTE);
return comparator == Comparator.LT || comparator == Comparator.LTE;
} else if (comparison > 0) {
return comparator.equals(Comparator.GT) || comparator.equals(Comparator.GTE);
return comparator == Comparator.GT || comparator == Comparator.GTE;
} else {
return comparator.equals(Comparator.EQ) || comparator.equals(Comparator.LTE) || comparator.equals(Comparator.GTE);
return comparator == Comparator.EQ || comparator == Comparator.LTE || comparator == Comparator.GTE;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* 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
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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 org.openrewrite.gradle.plugins;

import org.jspecify.annotations.Nullable;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Preconditions;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.gradle.IsBuildGradle;
import org.openrewrite.gradle.IsSettingsGradle;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.tree.J;

import static org.openrewrite.Preconditions.or;

public class RemoveDevelocityConfiguration extends Recipe {
@Override
public String getDisplayName() {
return "Remove Develocity configuration";
}

@Override
public String getDescription() {
return "Remove Develocity configuration from a Gradle build.";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(
or(new IsBuildGradle<>(), new IsSettingsGradle<>()),
new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.@Nullable MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
if ("develocity".equals(method.getSimpleName()) ||
"gradleEnterprise".equals(method.getSimpleName())) {
return null;
}
return super.visitMethodInvocation(method, ctx);
}
}
);
}
}
15 changes: 15 additions & 0 deletions rewrite-gradle/src/main/resources/META-INF/rewrite/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,18 @@ recipeList:
key: org.gradle.parallel
value: true
filePattern: gradle.properties
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.gradle.plugins.RemoveDevelocity
displayName: Remove Develocity
description: Remove the Develocity plugin and configuration from the Gradle build and settings files.
recipeList:
- org.openrewrite.gradle.plugins.RemoveBuildPlugin:
pluginId: com.gradle.develocity
- org.openrewrite.gradle.plugins.RemoveSettingsPlugin:
pluginId: com.gradle.develocity
- org.openrewrite.gradle.plugins.RemoveBuildPlugin:
pluginId: com.gradle.enterprise
- org.openrewrite.gradle.plugins.RemoveSettingsPlugin:
pluginId: com.gradle.enterprise
- org.openrewrite.gradle.plugins.RemoveDevelocityConfiguration
Loading

0 comments on commit af8665d

Please sign in to comment.