forked from microbean/microbean-construct
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessor.java
More file actions
143 lines (114 loc) · 4.02 KB
/
Copy pathProcessor.java
File metadata and controls
143 lines (114 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* -*- mode: Java; c-basic-offset: 2; indent-tabs-mode: nil; coding: utf-8-unix -*-
*
* Copyright © 2024–2025 microBean™.
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.microbean.construct;
import java.lang.System.Logger;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Consumer;
import javax.annotation.processing.Completion;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import static java.lang.System.getLogger;
import static java.lang.System.Logger.Level.DEBUG;
final class Processor implements AutoCloseable, javax.annotation.processing.Processor {
private static final Logger LOGGER = getLogger(Processor.class.getName());
private final Consumer<? super ProcessingEnvironment> cpe;
// run() method invoked under lock
private final Runnable r;
private final Lock lock;
private final Condition c;
// @GuardedBy("lock")
private boolean closed;
Processor(final Consumer<? super ProcessingEnvironment> cpe,
final Runnable r) {
super();
this.cpe = Objects.requireNonNull(cpe, "cpe");
this.r = r == null ? Processor::sink : r;
this.lock = new ReentrantLock();
this.c = this.lock.newCondition();
}
/*
* Instance methods.
*/
@Override // AutoCloseable
public final void close() {
this.lock.lock();
try {
if (!this.closed) {
this.closed = true;
this.c.signal();
}
} finally {
this.lock.unlock();
}
}
/**
* Initializes this {@link Processor}.
*
* @param pe a {@link ProcessingEnvironment}; must not be {@code null}
*
* @deprecated This method should be called only by a Java compiler in accordance with annotation processing
* contracts.
*/
@Deprecated // to be called only by a Java compiler in accordance with annotation processing contracts
@Override // Processor;
public final void init(final ProcessingEnvironment pe) {
this.lock.lock();
try {
if (this.closed) {
this.closed = false;
}
this.cpe.accept(pe);
while (!this.closed) {
this.c.awaitUninterruptibly();
}
} finally {
this.r.run();
this.lock.unlock();
}
}
@Override // Processor
public final Iterable<? extends Completion> getCompletions(final Element element,
final AnnotationMirror annotation,
final ExecutableElement member,
final String userText) {
return List.of();
}
@Override // Processor
public final Set<String> getSupportedAnnotationTypes() {
return Set.of();
}
@Override // Processor
public final Set<String> getSupportedOptions() {
return Set.of();
}
@Override // Processor
public final SourceVersion getSupportedSourceVersion() {
return SourceVersion.latestSupported();
}
@Override // Processor
public final boolean process(final Set<? extends TypeElement> annotations, final RoundEnvironment roundEnvironment) {
return false;
}
private static final void sink() {}
}