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
@@ -1,113 +1,33 @@
package io.avaje.recordbuilder.internal;

import static java.util.stream.Collectors.toSet;
import static io.avaje.recordbuilder.internal.APContext.logWarn;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;

public final class ModuleReader {

private static final Pattern regex = Pattern.compile("provides\\s+(.*?)\\s+with");

/** Keeps Track of found services by SPI and implementation set */
private final Map<String, Set<String>> foundServices = new HashMap<>();

private final Map<String, Set<String>> missingServicesMap = new HashMap<>();

private boolean staticWarning;
private boolean inProvides = false;

private static boolean coreWarning;

public ModuleReader(Map<String, Set<String>> services) {
services.forEach(this::add);
}

private void add(String k, Set<String> v) {
missingServicesMap.put(
ProcessorUtils.shortType(k).replace("$", "."),
v.stream().map(ProcessorUtils::shortType).collect(toSet()));
}

public void read(BufferedReader reader) throws IOException {
String line;
while ((line = reader.readLine()) != null) {
// retrieve service from provides statement
readLine(line);
}
}

void readLine(String line) {
String service = null;
if (line.contains("provides")) {
inProvides = true;
final var matcher = regex.matcher(line);
if (matcher.find()) {
service = ProcessorUtils.shortType(matcher.group(1)).replace("$", ".");
}
}

// if not part of a provides statement skip
if (!inProvides || line.isBlank()) {
if (!staticWarning && line.contains("io.avaje.recordbuilder") && !line.contains("static")) {
staticWarning = true;
}
if (line.contains("io.avaje.recordbuilder.core")) {
coreWarning = true;
}
return;
}
import javax.lang.model.element.ModuleElement;

processLine(line, service);

// provides statement has ended
if (line.contains(";")) {
inProvides = false;
}
}

/** as service implementations are discovered, remove from missing strings map */
private void processLine(String line, String service) {
final Set<String> missingServiceImpls = missingServicesMap.get(service);
final Set<String> foundServiceImpls =
foundServices.computeIfAbsent(service, k -> new HashSet<>());
if (!foundServiceImpls.containsAll(missingServiceImpls)) {
parseServices(line, missingServiceImpls, foundServiceImpls);
}
missingServiceImpls.removeAll(foundServiceImpls);
}

/**
* as service implementations are discovered, add to found strings set for a given service
*
* @param input the line to check
* @param missingServiceImpls the services we're looking for
* @param foundServiceImpls where we'll store the results if we have a match
*/
private static void parseServices(
String input, Set<String> missingServiceImpls, Set<String> foundServiceImpls) {

for (final var impl : missingServiceImpls) {
if (input.contains(impl)) {
foundServiceImpls.add(impl);
}
}
}

public boolean staticWarning() {
return staticWarning;
}

public boolean coreWarning() {
return coreWarning;
}

public Map<String, Set<String>> missing() {
return missingServicesMap;
public final class ModuleReader {
private ModuleReader() {}

private static ModuleElement module;

public static void read(BufferedReader reader) {
reader
.lines()
.forEach(
line -> {
if (line.isBlank()) {
return;
}

if (line.contains("io.avaje.recordbuilder") && !line.contains("static")) {
logWarn(
"`requires io.avaje.recordbuilder` should be `requires static io.avaje.recordbuilder`",
module);
}
if (line.contains("io.avaje.recordbuilder.core")) {
logWarn("io.avaje.recordbuilder.core should not be used", module);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ String fields(Map<String, String> defaultsMap) {
final String dt = defaultsMap.get(type.mainType());
if (dt != null) {
importTypes.add(dt);
defaultVal = " = new " + dt + "<>()";

defaultVal = " = new " + ProcessorUtils.shortType(dt) + "<>()";
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.avaje.recordbuilder.internal;

import static io.avaje.recordbuilder.internal.APContext.createSourceFile;
import static io.avaje.recordbuilder.internal.APContext.elements;
import static io.avaje.recordbuilder.internal.APContext.*;
import static io.avaje.recordbuilder.internal.APContext.logError;
import static io.avaje.recordbuilder.internal.APContext.typeElement;
import static java.util.stream.Collectors.joining;
Expand Down Expand Up @@ -82,6 +82,14 @@ public boolean process(Set<? extends TypeElement> tes, RoundEnvironment roundEnv
.map(APContext::asTypeElement)
.forEach(this::readElement);

if (roundEnv.processingOver()) {
try (var reader = getModuleInfoReader()) {

ModuleReader.read(reader);
} catch (IOException e) {
// Can't read module, it's whatever
}
}
return false;
}

Expand Down