Skip to content

Commit a7ccfb3

Browse files
authored
Exclude javax.inject and CouchbaseAnnotationProcessor to exit gracefully without. (#1934)
CouchbaseAnnotationProcessor will provide a warning and skip generation if javax.inject is not present.
1 parent 25aaac4 commit a7ccfb3

File tree

5 files changed

+73
-14
lines changed

5 files changed

+73
-14
lines changed

pom.xml

+16-8
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
<springdata.commons>3.3.0-SNAPSHOT</springdata.commons>
2424
<java-module-name>spring.data.couchbase</java-module-name>
2525
<hibernate.validator>7.0.1.Final</hibernate.validator>
26-
<mysema.querydsl>3.7.4</mysema.querydsl>
2726
<couchbase.encryption>3.1.0</couchbase.encryption>
2827
<jodatime>2.10.13</jodatime>
2928
<jackson-joda>2.13.4</jackson-joda>
@@ -49,6 +48,19 @@
4948
<artifactId>querydsl-apt</artifactId>
5049
<version>${querydsl}</version>
5150
<classifier>jakarta</classifier>
51+
<exclusions>
52+
<exclusion>
53+
<groupId>javax.inject</groupId>
54+
<artifactId>javax.inject</artifactId>
55+
</exclusion>
56+
</exclusions>
57+
</dependency>
58+
59+
<dependency>
60+
<groupId>javax.inject</groupId>
61+
<artifactId>javax.inject</artifactId>
62+
<version>1</version>
63+
<scope>test</scope>
5264
</dependency>
5365

5466
<dependency>
@@ -250,13 +262,6 @@
250262
<plugin>
251263
<groupId>org.apache.maven.plugins</groupId>
252264
<artifactId>maven-compiler-plugin</artifactId>
253-
<dependencies>
254-
<dependency>
255-
<groupId>com.querydsl</groupId>
256-
<artifactId>querydsl-apt</artifactId>
257-
<version>${querydsl}</version>
258-
</dependency>
259-
</dependencies>
260265
<configuration>
261266
<proc>none</proc>
262267
</configuration>
@@ -273,6 +278,9 @@
273278
<annotationProcessor>org.springframework.data.couchbase.repository.support.CouchbaseAnnotationProcessor</annotationProcessor>
274279
</annotationProcessors>
275280
<generatedTestSourcesDirectory>target/generated-test-sources</generatedTestSourcesDirectory>
281+
<compilerArgs>
282+
<arg>-Aquerydsl.logInfo=true</arg>
283+
</compilerArgs>
276284
</configuration>
277285
</execution>
278286
</executions>

src/main/java/org/springframework/data/couchbase/repository/support/CouchbaseAnnotationProcessor.java

+54-3
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,19 @@
1515
*/
1616
package org.springframework.data.couchbase.repository.support;
1717

18+
import static com.querydsl.apt.APTOptions.QUERYDSL_LOG_INFO;
19+
1820
import java.util.Collections;
21+
import java.util.Set;
1922

2023
import javax.annotation.processing.RoundEnvironment;
2124
import javax.annotation.processing.SupportedAnnotationTypes;
2225
import javax.annotation.processing.SupportedSourceVersion;
2326
import javax.lang.model.SourceVersion;
27+
import javax.lang.model.element.TypeElement;
2428
import javax.tools.Diagnostic;
2529

2630
import org.springframework.data.couchbase.core.mapping.Document;
27-
import org.springframework.lang.Nullable;
2831

2932
import com.querydsl.apt.AbstractQuerydslProcessor;
3033
import com.querydsl.apt.Configuration;
@@ -49,9 +52,9 @@ public class CouchbaseAnnotationProcessor extends AbstractQuerydslProcessor {
4952
* @see com.querydsl.apt.AbstractQuerydslProcessor#createConfiguration(javax.annotation.processing.RoundEnvironment)
5053
*/
5154
@Override
52-
protected Configuration createConfiguration(@Nullable RoundEnvironment roundEnv) {
55+
protected Configuration createConfiguration(/*@Nullable */RoundEnvironment roundEnv) {
5356

54-
processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Running " + getClass().getSimpleName());
57+
processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Running override createConfiguration() " + getClass().getSimpleName());
5558

5659
DefaultConfiguration configuration = new DefaultConfiguration(processingEnv, roundEnv, Collections.emptySet(),
5760
QueryEntities.class, Document.class, QuerySupertype.class, QueryEmbeddable.class, QueryEmbedded.class,
@@ -60,4 +63,52 @@ protected Configuration createConfiguration(@Nullable RoundEnvironment roundEnv)
6063

6164
return configuration;
6265
}
66+
67+
@Override
68+
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
69+
setLogInfo();
70+
logInfo("Running override process() " + getClass().getSimpleName() +" isOver: "+roundEnv.processingOver() +" annotations: "+annotations.size());
71+
72+
if (roundEnv.processingOver() || annotations.size() == 0) {
73+
return ALLOW_OTHER_PROCESSORS_TO_CLAIM_ANNOTATIONS;
74+
}
75+
76+
if (roundEnv.getRootElements() == null || roundEnv.getRootElements().isEmpty()) {
77+
logInfo("No sources to process");
78+
return ALLOW_OTHER_PROCESSORS_TO_CLAIM_ANNOTATIONS;
79+
}
80+
81+
Configuration conf = createConfiguration(roundEnv);
82+
try {
83+
conf.getTypeMappings();
84+
} catch (NoClassDefFoundError cnfe ){
85+
logWarn( cnfe +" add a dependency on javax.inject:javax.inject to create querydsl classes");
86+
return ALLOW_OTHER_PROCESSORS_TO_CLAIM_ANNOTATIONS;
87+
}
88+
return super.process(annotations, roundEnv);
89+
90+
}
91+
92+
private boolean shouldLogInfo;
93+
94+
private void setLogInfo() {
95+
boolean hasProperty = processingEnv.getOptions().containsKey(QUERYDSL_LOG_INFO);
96+
if (hasProperty) {
97+
String val = processingEnv.getOptions().get(QUERYDSL_LOG_INFO);
98+
shouldLogInfo = Boolean.parseBoolean(val);
99+
}
100+
}
101+
102+
private void logInfo(String message) {
103+
if (shouldLogInfo) {
104+
System.out.println("[NOTE] "+message); // maven compiler swallows messages to messager
105+
processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, message);
106+
}
107+
}
108+
109+
private void logWarn(String message) {
110+
System.err.println("[WARNING] "+message); // maven compiler swallows messages to messager
111+
processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING, message);
112+
}
63113
}
114+

src/test/java/org/springframework/data/couchbase/core/CouchbaseTemplateKeyValueIntegrationTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1329,7 +1329,7 @@ void sampleScan() {
13291329
MutationToken mt = couchbaseTemplate.getCouchbaseClientFactory().getDefaultCollection()
13301330
.upsert(id, JsonObject.create().put("id", id)).mutationToken().get();
13311331
Stream<User> users = couchbaseTemplate.rangeScan(User.class).consistentWith(MutationState.from(mt))
1332-
/*.withSort(ScanSort.ASCENDING)*/.samplingScan(5l, null);
1332+
/*.withSort(ScanSort.ASCENDING)*/.samplingScan(5l, (Long)null);
13331333
List<User> usersList = users.toList();
13341334
assertEquals(5, usersList.size(), "number in sample");
13351335
for (User u : usersList) {

src/test/java/org/springframework/data/couchbase/core/query/QueryCriteriaTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ void testNestedOrCriteria() {
9090
@Test
9191
void testNestedNotIn() {
9292
QueryCriteria c = where(i("name")).is("Bubba").or(where(i("age")).gt(12).and(i("country")).is("Austria"))
93-
.and(where(i("state")).notIn(new String[] { "Alabama", "Florida" }));
93+
.and(where(i("state")).notIn( "Alabama", "Florida" ));
9494
JsonArray parameters = JsonArray.create();
9595
assertEquals(" ( (`name` = $1) or (`age` > $2 and `country` = $3)) and (not( (`state` in $4) ))",
9696
c.export(new int[1], parameters, null));

src/test/java/org/springframework/data/couchbase/domain/BigAirlineRepository.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
*/
3131
@Repository
3232
public interface BigAirlineRepository extends CouchbaseRepository<BigAirline, String>,
33-
QuerydslPredicateExecutor<BigAirline>, DynamicProxyable<BigAirlineRepository> {
33+
DynamicProxyable<BigAirlineRepository> {
3434

3535
@Query("#{#n1ql.selectEntity} where #{#n1ql.filter} and (name = $1)")
3636
List<Airline> getByName(@Param("airline_name") String airlineName);

0 commit comments

Comments
 (0)