Skip to content

Commit

Permalink
Merge branch 'master' into stable
Browse files Browse the repository at this point in the history
  • Loading branch information
Pante committed May 17, 2021
2 parents 1ea9cf0 + 2d0f319 commit 39cb0b9
Show file tree
Hide file tree
Showing 19 changed files with 119 additions and 59 deletions.
5 changes: 0 additions & 5 deletions .dependabot/config.yml

This file was deleted.

8 changes: 8 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: 2
updates:
- package-ecosystem: maven
directory: "/"
schedule:
interval: daily
time: "21:00"
open-pull-requests-limit: 10
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1 +1,18 @@
## 1.1.0 - The Case for Cases (17/05/2021)

This release focuses on improving the quality of life for cases.

### Elementary
- Add `Cases.labels()`
- Change `Cases.get(String)` to `Cases.label(String)`
- Change `Cases.list()` to `Cases.all()`
- Change `@Case` annotation to use the annotated target's name if available as its label by default
- Fix missing annotations

### Satisfactory
- Fix missing annotations

### Utilitary
- Fix missing annotations

## 1.0.0 - Initial Launch! (22/04/2021)
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

**Please view the [stable branch](https://github.com/Pante/elementary/tree/stable) for a production version. Requires Java 11+. Read the [wiki](https://github.com/Pante/elementary/wiki) to get started.**

**Please read [The Problem With Annotation Processors](https://dzone.com/articles/the-problem-with-annotation-processors]) if you're interested in what problem Elementary solves.**

#### Maven Repository
```XML
<repository>
Expand Down Expand Up @@ -37,7 +39,8 @@ This project is heavily inspired by Google's [compile-testing](https://github.co
<dependency>
<groupId>com.karuslabs</groupId>
<artifactId>elementary</artifactId>
<version>1.0.0</version>
<version>1.1.0</version>
<scope>test</scope>
</dependency>
```

Expand All @@ -51,7 +54,7 @@ This project is heavily inspired by Google's [compile-testing](https://github.co
<dependency>
<groupId>com.karuslabs</groupId>
<artifactId>satisfactory</artifactId>
<version>1.0.0</version>
<version>1.1.0</version>
</dependency>
```

Expand All @@ -65,6 +68,6 @@ This project is heavily inspired by Google's [compile-testing](https://github.co
<dependency>
<groupId>com.karuslabs</groupId>
<artifactId>satisfactory</artifactId>
<version>1.0.0</version>
<version>1.1.0</version>
</dependency>
```
2 changes: 1 addition & 1 deletion elementary/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.karuslabs</groupId>
<artifactId>elementary-project</artifactId>
<version>1.0.0</version>
<version>1.1.0</version>
</parent>

<artifactId>elementary</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,13 @@
*/
package com.karuslabs.elementary;

import com.karuslabs.annotations.ValueType;

import java.util.*;
import javax.tools.*;

/**
* The diagnostic messages of a compilation's results.
*/
public @ValueType final class Diagnostics implements DiagnosticListener<JavaFileObject> {
public final class Diagnostics implements DiagnosticListener<JavaFileObject> {

/**
* All diagnostic messages.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,13 @@
*/
package com.karuslabs.elementary;

import com.karuslabs.annotations.ValueType;

import java.util.*;
import javax.tools.*;

/**
* The results of a compilation.
*/
public @ValueType final class Results {
public final class Results {

/**
* The source files which were compiled.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
*/
package com.karuslabs.elementary.file;

import com.karuslabs.annotations.Delegate;

import java.io.*;
import java.nio.file.Path;
import java.util.*;
Expand All @@ -31,7 +33,7 @@
/**
* A file manager that forwards all calls to an underlying {@code StandardJavaFileManager}.
*/
public abstract class ForwardingFileManager implements StandardJavaFileManager {
public abstract @Delegate class ForwardingFileManager implements StandardJavaFileManager {

private final StandardJavaFileManager manager;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,12 @@ public InputStream openInputStream() {
}

@Override
public Reader openReader(boolean ignoreEncodingErrors) {
public Reader openReader(@Ignored boolean ignoreEncodingErrors) {
return new StringReader(string);
}

@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) {
public CharSequence getCharContent(@Ignored boolean ignoreEncodingErrors) {
return string;
}

Expand Down
66 changes: 44 additions & 22 deletions elementary/src/main/java/com/karuslabs/elementary/junit/Cases.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

import java.util.*;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.element.Element;
import javax.lang.model.element.*;

import org.checkerframework.checker.nullness.qual.Nullable;

Expand All @@ -40,6 +40,8 @@ public class Cases implements Iterable<Element> {

private final RoundEnvironment environment;
private @Lazy List<Element> elements;
private @Lazy Map<String, List<Element>> labels;


/**
* Creates a {@code Cases} for the given annotation processing round.
Expand All @@ -57,7 +59,7 @@ public Cases(RoundEnvironment environment) {
*/
@Override
public Iterator<Element> iterator() {
return list().iterator();
return all().iterator();
}


Expand All @@ -68,18 +70,18 @@ public Iterator<Element> iterator() {
* @return the only element annotated with {@code @Case}; otherwise {@code null}
*/
public @Nullable Element one() {
return list().size() == 1 ? list().get(0) : null;
return all().size() == 1 ? all().get(0) : null;
}

/**
* Returns an elementif the annotation processing round contains exactly one
* Returns an element if the annotation processing round contains exactly one
* element annotated with {@code @Case} and the given label.
*
* @param label the label
* @return the only annotated element with the given label; else {@code null}
*/
public @Nullable Element one(String label) {
var elements = get(label);
var elements = label(label);
return elements.size() == 1 ? elements.get(0) : null;
}

Expand All @@ -91,7 +93,7 @@ public Iterator<Element> iterator() {
* @return the element at the given index
*/
public Element get(int index) {
return list().get(index);
return all().get(index);
}

/**
Expand All @@ -100,40 +102,60 @@ public Element get(int index) {
* @param label the label
* @return the annotated elements
*/
public List<Element> get(String label) {
var matches = new ArrayList<Element>();
for (var element : list()) {
if (element.getAnnotation(Case.class).value().equals(label)) {
matches.add(element);
}
}

return matches;
public List<Element> label(String label) {
return labels().getOrDefault(label, List.of());
}


/**
* Returns elements annotated with {@code @Case} in this annotation processing
* round.
*
* @return the annotated elements
*/
public List<Element> list() {
if (elements == null) {
public List<Element> all() {
initialize();
return elements;
}

/**
* Returns the labels of {@code @Case}s and the associated elements in this
* annotation processing round.
*
* @return the {@code @Case} labels and the associated elements
*/
public Map<String, List<Element>> labels() {
initialize();
return labels;
}


void initialize() {
if (elements == null || labels == null) {
elements = new ArrayList<>(environment.getElementsAnnotatedWith(Case.class));
labels = new HashMap<>();

for (var element : elements) {
var annotation = element.getAnnotation(Case.class);
var label = annotation.value().equals(Case.DEFAULT_LABEL) ? element.getSimpleName().toString() : annotation.value();

var elements = labels.get(label);
if (elements == null) {
labels.put(label, elements = new ArrayList<>());
}
elements.add(element);
}
}

return elements;
}


/**
* Returns the number of elements annotated with {@code @Case} in this annotation
* processing round.
*
* @return the number of elements
*/
public int count() {
return list().size();
return all().size();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public SourceVersion getSupportedSourceVersion() {
/**
* An annotation processing environment.
*/
static @ValueType final class Environment {
static final class Environment {
public final RoundEnvironment round;
public final Elements elements;
public final Types types;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,17 @@
@Retention(RUNTIME)
public @interface Case {

/**
* A default value for {@code @Case} used to denote that the annotated target's
* name should be used as its label.
*/
static final String DEFAULT_LABEL = "${DEFAULT_LABEL}";

/**
* An optional label for this case.
*
* @return a label
*/
String value() default "";
String value() default DEFAULT_LABEL;

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ void one() {
@Test
void one_label() {
assertNotNull(cases.one("a"));
assertNotNull(cases.one("A"));
}

@Test
Expand All @@ -59,14 +60,19 @@ void get_index() {
}

@Test
void get_label() {
assertEquals(1, cases.get("a").size());
void label() {
assertEquals(1, cases.label("a").size());
}


@Test
void list() {
assertEquals(2, cases.list().size());
void all() {
assertEquals(2, cases.all().size());
}

@Test
void labels() {
assertEquals(2, cases.labels().size());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ void lint_string_variable(Cases cases) { // Cases can also be obtained via Tools

@Test
void lint_method_that_returns_string(Cases cases) {
var second = cases.get(1);
var second = cases.get(1); // Alternatively, we can use cases.one("second")
assertFalse(lint.lint(second));
}

static class Sample {
@Case("first") String first;
@Case String second() { return ""; }
@Case("first") String something;
@Case String second() { return ""; } // The method/variable name is used as the label if none is specified
}

}
Expand Down
Loading

0 comments on commit 39cb0b9

Please sign in to comment.