From 4ab81db0b48934d54fd8c09a7772b9e16fe48c4e Mon Sep 17 00:00:00 2001 From: Josiah Noel <32279667+SentryMan@users.noreply.github.com> Date: Tue, 28 Jan 2025 00:13:34 -0500 Subject: [PATCH] Fix test client ruining main detection (#559) --- .../generator/client/ComponentReader.java | 58 +++++++++++-------- .../src/test/java/example/github/Dummy.java | 14 +++++ 2 files changed, 47 insertions(+), 25 deletions(-) create mode 100644 tests/test-client/src/test/java/example/github/Dummy.java diff --git a/http-generator-client/src/main/java/io/avaje/http/generator/client/ComponentReader.java b/http-generator-client/src/main/java/io/avaje/http/generator/client/ComponentReader.java index 086103ff..93f101bf 100644 --- a/http-generator-client/src/main/java/io/avaje/http/generator/client/ComponentReader.java +++ b/http-generator-client/src/main/java/io/avaje/http/generator/client/ComponentReader.java @@ -7,18 +7,18 @@ import static java.util.stream.Collectors.toList; import java.io.FileNotFoundException; -import java.io.LineNumberReader; -import java.io.Reader; +import java.io.IOException; +import java.net.URI; +import java.nio.file.Files; import java.nio.file.NoSuchFileException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; +import java.nio.file.Path; +import java.util.HashSet; import java.util.Map; +import java.util.Set; import javax.annotation.processing.FilerException; import javax.lang.model.element.Modifier; import javax.lang.model.element.TypeElement; -import javax.tools.FileObject; import javax.tools.StandardLocation; import io.avaje.http.generator.core.APContext; @@ -64,32 +64,40 @@ void read() { } } - private List loadMetaInf() { + private Set loadMetaInf() { + var set = new HashSet(); try { - final FileObject fileObject = filer().getResource(StandardLocation.CLASS_OUTPUT, "", Constants.META_INF_COMPONENT); - if (fileObject != null) { - final List lines = new ArrayList<>(); - final Reader reader = fileObject.openReader(true); - final LineNumberReader lineReader = new LineNumberReader(reader); - String line; - while ((line = lineReader.readLine()) != null) { - line = line.trim(); - if (!line.isEmpty()) { - lines.add(line); - } - } - return lines; - } + addLines(mainMetaInfURI(), set); + addLines(metaInfURI(), set); + } catch (final IOException e) { + logWarn("Error reading services file: " + e.getMessage()); + } + return set; + } + private static void addLines(URI uri, HashSet set) { + try (var lines = Files.lines(Path.of(uri))) { + lines.forEach(set::add); } catch (FileNotFoundException | NoSuchFileException e) { // logDebug("no services file yet"); - } catch (final FilerException e) { logDebug("FilerException reading services file"); - - } catch (final Exception e) { + } catch (Exception e) { logWarn("Error reading services file: " + e.getMessage()); } - return Collections.emptyList(); + } + + private static URI mainMetaInfURI() throws IOException { + return URI.create( + metaInfURI() + .toString() + .replaceFirst("java/test", "java/main") + .replaceFirst("test-classes", "classes")); + } + + private static URI metaInfURI() throws IOException { + return filer() + .getResource(StandardLocation.CLASS_OUTPUT, "", Constants.META_INF_COMPONENT) + .toUri(); } } diff --git a/tests/test-client/src/test/java/example/github/Dummy.java b/tests/test-client/src/test/java/example/github/Dummy.java new file mode 100644 index 00000000..2459269f --- /dev/null +++ b/tests/test-client/src/test/java/example/github/Dummy.java @@ -0,0 +1,14 @@ +package example.github; + +import java.util.List; + +import io.avaje.http.api.Client; +import io.avaje.http.api.Get; +import io.avaje.http.client.HttpException; + +@Client +public interface Dummy { + + @Get("users/{user}/repos") + List listRepos(String user, String other) throws HttpException; +}