Skip to content

Commit

Permalink
Merge branch 'main' into introduce-image-reference-and-recipe
Browse files Browse the repository at this point in the history
  • Loading branch information
jevanlingen authored Dec 27, 2024
2 parents f65cf8c + b91499c commit d6590f6
Show file tree
Hide file tree
Showing 64 changed files with 538 additions and 539 deletions.
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
Loading

0 comments on commit d6590f6

Please sign in to comment.