-
Notifications
You must be signed in to change notification settings - Fork 57
/
Predicates.java
585 lines (536 loc) · 23.7 KB
/
Predicates.java
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
package com.redhat.ceylon.compiler.java.runtime.metamodel;
import java.util.Arrays;
import ceylon.language.Annotated;
import ceylon.language.Iterator;
import ceylon.language.Sequential;
import ceylon.language.meta.declaration.AnnotatedDeclaration;
import ceylon.language.meta.model.ClassOrInterface;
import ceylon.language.meta.model.Type;
import ceylon.language.meta.model.nothingType_;
import com.redhat.ceylon.compiler.java.Util;
import com.redhat.ceylon.compiler.java.language.EnumeratedTypeError;
import com.redhat.ceylon.compiler.java.metadata.Ceylon;
import com.redhat.ceylon.compiler.java.runtime.model.TypeDescriptor;
import com.redhat.ceylon.model.typechecker.model.Constructor;
import com.redhat.ceylon.model.typechecker.model.Declaration;
/**
* Factory methods for constructing predicates on things.
*/
class Predicates {
private Predicates() {}
/**
* A predicate
*/
static interface Predicate<T> {
/** Returns true if the predicate matches/accepts the given thing */
boolean accept(T candidate);
}
/**
* A predicate that {@linkplain Predicate#accept(Object) accepts}
* nothing, always returning {@code false}.
*/
private static final Predicate<?> FALSE = new Predicate<Object>() {
/** Returns {@code false}, always. */
@Override
public boolean accept(Object candidate) {
return false;
}
@Override
public String toString() {
return "false";
}
};
@SuppressWarnings("unchecked")
static <T> Predicate<T> false_() {
return (Predicate<T>)FALSE;
}
/**
* A predicate that {@linkplain Predicate#accept(Object) accepts}
* everything, always returning {@code true}.
*/
static final Predicate<?> TRUE = new Predicate<Object>() {
/** Returns {@code true}, always. */
@Override
public boolean accept(Object candidate) {
return true;
}
@Override
public String toString() {
return "true";
}
};
@SuppressWarnings("unchecked")
static <T> Predicate<T> true_() {
return (Predicate<T>)TRUE;
}
/**
* A predicate to look things up by name
*/
private static final class NamePredicate implements Predicate<Declaration> {
private String name;
NamePredicate(String name){
this.name = name;
}
@Override
public boolean accept(Declaration candidate) {
return candidate.getName().equals(name);
}
}
/**
* Returns a new name predicate
*/
static NamePredicate isDeclarationNamed(String name){
return new NamePredicate(name);
}
/**
* The logical conjunction of a number of other predicates.
*/
private static final class And<T> implements Predicate<T> {
private final Predicate<T>[] others;
private And(Predicate<T>[] others) {
this.others = others;
}
/**
* Returns {@code false} iff all the other predicates
* {@linkplain Predicate#accept(Object) accept()}
* the given declaration.
*/
@Override
public boolean accept(T candidate) {
for (Predicate<T> other : others) {
if (!other.accept(candidate)) {
return false;
}
}
return true;
}
@Override
public String toString() {
return "(and " + Arrays.toString(others) +")";
}
}
/**
* Returns a predicate that is the logical conjunction of a number of
* other predicates.
*/
@SafeVarargs
static <T> Predicate<T> and(Predicate<T>... others) {
if (others.length == 1) {
return others[0];
}
for (Predicate<T> pred : others) {
if (pred == FALSE) {
return false_();
}
}
return new And<T>(others);
}
/**
* The logical disjunction of a number of other predicates.
*/
private static final class Or<T> implements Predicate<T> {
private final Predicate<T>[] others;
public Or(Predicate<T>[] others) {
this.others = others;
}
/**
* Returns {@code false} iff at least one of the other predicates
* {@linkplain Predicate#accept(Object) accept()}s
* the given declaration.
*/
@Override
public boolean accept(T candidate) {
for (Predicate<T> other : others) {
if (other.accept(candidate)) {
return true;
}
}
return false;
}
@Override
public String toString() {
return "(or " + Arrays.toString(others) +")";
}
}
/**
* Returns a predicate that is the logical disjunction of a number of
* other predicates.
*/
@SafeVarargs
static <T> Predicate<T> or(Predicate<T>... others) {
if (others.length == 1) {
return others[0];
}
for (Predicate<T> pred : others) {
if (pred == TRUE) {
return true_();
}
}
return new Or<T>(others);
}
/** Predicate on Declarations that accepts Value */
private static final Predicate<Declaration> DECLARATION_IS_REFERENCE = new Predicate<Declaration>() {
@Override
public boolean accept(Declaration declaration) {
return declaration instanceof com.redhat.ceylon.model.typechecker.model.Value
&& !((com.redhat.ceylon.model.typechecker.model.Value)declaration).isTransient();
}
@Override
public String toString() {
return "(kind = Value)";
}
};
/** Predicate on Declarations that accepts Value */
private static final Predicate<Declaration> DECLARATION_IS_VALUE = new Predicate<Declaration>() {
@Override
public boolean accept(Declaration declaration) {
return declaration instanceof com.redhat.ceylon.model.typechecker.model.Value
&& !(((com.redhat.ceylon.model.typechecker.model.Value)declaration).getTypeDeclaration() instanceof Constructor);
}
@Override
public String toString() {
return "(kind = Value)";
}
};
/** Predicate on Declarations that accepts Function */
private static final Predicate<Declaration> DECLARATION_IS_FUNCTION = new Predicate<Declaration>() {
@Override
public boolean accept(Declaration declaration) {
return declaration instanceof com.redhat.ceylon.model.typechecker.model.Function
&& !(((com.redhat.ceylon.model.typechecker.model.Function)declaration).getTypeDeclaration() instanceof Constructor);
}
@Override
public String toString() {
return "(kind = Function)";
}
};
/** Predicate on Declarations that accepts Function and Value */
private static final Predicate<Declaration> DECLARATION_IS_FUNCTION_OR_VALUE = new Predicate<Declaration>() {
@Override
public boolean accept(Declaration declaration) {
return (declaration instanceof com.redhat.ceylon.model.typechecker.model.Value
|| declaration instanceof com.redhat.ceylon.model.typechecker.model.Function)
&& !(((com.redhat.ceylon.model.typechecker.model.FunctionOrValue)declaration).getTypeDeclaration() instanceof Constructor)
;
}
@Override
public String toString() {
return "(kind = Value || kind = Function)";
}
};
/** Predicate on Declarations that accepts Class */
private static final Predicate<Declaration> DECLARATION_IS_CLASS = new Predicate<Declaration>() {
@Override
public boolean accept(Declaration declaration) {
return declaration instanceof com.redhat.ceylon.model.typechecker.model.Class;
}
@Override
public String toString() {
return "(kind = Class)";
}
};
/** Predicate on Declarations that accepts Constructor */
private static final Predicate<Declaration> DECLARATION_IS_CONSTRUCTOR = new Predicate<Declaration>() {
@Override
public boolean accept(Declaration declaration) {
return declaration instanceof com.redhat.ceylon.model.typechecker.model.FunctionOrValue
&& ((com.redhat.ceylon.model.typechecker.model.FunctionOrValue)declaration).getTypeDeclaration() instanceof Constructor;
}
@Override
public String toString() {
return "(kind = Constructor)";
}
};
private static final Predicate<Declaration> DECLARATION_IS_CALLABLE_CONSTRUCTOR = new Predicate<Declaration>() {
@Override
public boolean accept(Declaration declaration) {
return declaration instanceof com.redhat.ceylon.model.typechecker.model.Function
&& ((com.redhat.ceylon.model.typechecker.model.Function)declaration).getTypeDeclaration() instanceof Constructor;
}
@Override
public String toString() {
return "(kind = CallableConstructor)";
}
};
private static final Predicate<Declaration> DECLARATION_IS_VALUE_CONSTRUCTOR = new Predicate<Declaration>() {
@Override
public boolean accept(Declaration declaration) {
return declaration instanceof com.redhat.ceylon.model.typechecker.model.Value
&& ((com.redhat.ceylon.model.typechecker.model.Value)declaration).getTypeDeclaration() instanceof Constructor;
}
@Override
public String toString() {
return "(kind = ValueConstructor)";
}
};
/** Predicate on Declarations that accepts Interface */
private static final Predicate<Declaration> DECLARATION_IS_INTERFACE = new Predicate<Declaration>() {
@Override
public boolean accept(Declaration declaration) {
return declaration instanceof com.redhat.ceylon.model.typechecker.model.Interface;
}
@Override
public String toString() {
return "(kind = Interface)";
}
};
/** Predicate on Declarations that accepts ClassOrInterface */
private static final Predicate<Declaration> DECLARATION_IS_CLASS_OR_INTERFACE = new Predicate<Declaration>() {
@Override
public boolean accept(Declaration declaration) {
return declaration instanceof com.redhat.ceylon.model.typechecker.model.ClassOrInterface;
}
@Override
public String toString() {
return "(kind = ClassOrInterface)";
}
};
/** Predicate on Declarations that accepts Alias */
private static final Predicate<Declaration> DECLARATION_IS_ALIAS = new Predicate<Declaration>() {
@Override
public boolean accept(Declaration declaration) {
return declaration instanceof com.redhat.ceylon.model.typechecker.model.TypeAlias;
}
@Override
public String toString() {
return "(kind = Alias)";
}
};
/**
* Returns a Predicate for Declarations being of the given kind
* (Class, Interface, Function, Value etc)
* @param kind A TypeDescriptor for the sought declaration kind.
*/
static Predicate<Declaration> isDeclarationOfKind(TypeDescriptor kind) {
if (kind instanceof TypeDescriptor.Class) {
Class<?> declarationClass = (Class<?>)((TypeDescriptor.Class) kind).getKlass();
if (declarationClass == ceylon.language.meta.declaration.ReferenceDeclaration.class) {
return DECLARATION_IS_REFERENCE;
} else if (declarationClass == ceylon.language.meta.declaration.ValueDeclaration.class) {
return DECLARATION_IS_VALUE;
} else if (declarationClass == ceylon.language.meta.declaration.FunctionDeclaration.class) {
return DECLARATION_IS_FUNCTION;
} else if (declarationClass == ceylon.language.meta.declaration.FunctionOrValueDeclaration.class) {
return DECLARATION_IS_FUNCTION_OR_VALUE;
} else if (declarationClass == ceylon.language.meta.declaration.ClassDeclaration.class) {
return DECLARATION_IS_CLASS;
} else if (declarationClass == ceylon.language.meta.declaration.InterfaceDeclaration.class) {
return DECLARATION_IS_INTERFACE;
} else if (declarationClass == ceylon.language.meta.declaration.ClassOrInterfaceDeclaration.class) {
return DECLARATION_IS_CLASS_OR_INTERFACE;
} else if (declarationClass == ceylon.language.meta.declaration.AliasDeclaration.class) {
return DECLARATION_IS_ALIAS;
} else if (declarationClass == ceylon.language.meta.declaration.ConstructorDeclaration.class) {
return DECLARATION_IS_CONSTRUCTOR;
} else if (declarationClass == ceylon.language.meta.declaration.CallableConstructorDeclaration.class) {
return DECLARATION_IS_CALLABLE_CONSTRUCTOR;
} else if (declarationClass == ceylon.language.meta.declaration.ValueConstructorDeclaration.class) {
return DECLARATION_IS_VALUE_CONSTRUCTOR;
} else if (declarationClass == ceylon.language.meta.declaration.NestableDeclaration.class) {
return true_();
}
throw new EnumeratedTypeError("Supposedly exhaustive switch was not exhaustive");
} else if (kind instanceof TypeDescriptor.Union) {
TypeDescriptor[] members = ((TypeDescriptor.Union)kind).getMembers();
@SuppressWarnings("unchecked")
Predicate<Declaration>[] preds = new Predicate[members.length];
int ii = 0;
for (TypeDescriptor member : members) {
preds[ii++] = isDeclarationOfKind(member);
}
return or(preds);
} else if (kind instanceof TypeDescriptor.Intersection) {
TypeDescriptor[] members = ((TypeDescriptor.Intersection)kind).getMembers();
@SuppressWarnings("unchecked")
Predicate<Declaration>[] preds = new Predicate[members.length];
int ii = 0;
for (TypeDescriptor member : members) {
preds[ii++] = isDeclarationOfKind(member);
}
return and(preds);
} else if (kind == TypeDescriptor.NothingType) {
return false_();
}
throw new EnumeratedTypeError("Supposedly exhaustive switch was not exhaustive");
}
/**
* A Predicate on the Declarations having a given annotation
*/
private static class AnnotatedWith<A extends java.lang.annotation.Annotation>
implements Predicate<Declaration> {
private final TypeDescriptor annotation;
private final ClassOrInterface<A> at;
public AnnotatedWith(TypeDescriptor annotation,
ClassOrInterface<A> at) {
this.annotation = annotation;
this.at = at;
}
/** Acceps the Declaration if the required annotation is present */
@Override
public boolean accept(Declaration memberModel) {
AnnotatedDeclaration member = Metamodel.getOrCreateMetamodel(memberModel);
Sequential<? extends A> annotations = Metamodel.<A>annotations(annotation, member);
return !annotations.getEmpty();
}
@Override
public String toString() {
return "(having-annotation " + at +")";
}
}
/**
* Returns a predicate for Declarations having the given annotation
*/
static <Kind extends ceylon.language.meta.declaration.Declaration, A extends java.lang.annotation.Annotation>
Predicate<Declaration> isDeclarationAnnotatedWith(TypeDescriptor annotation) {
Type<?> at = Metamodel.getAppliedMetamodel(annotation);
return Predicates.<A>isDeclarationAnnotatedWith(annotation, at);
}
@SuppressWarnings("unchecked")
private static <A extends java.lang.annotation.Annotation> Predicate<Declaration>
isDeclarationAnnotatedWith(TypeDescriptor annotation, Type<?> at) {
if (at instanceof nothingType_) {
return false_();
} else if (at instanceof ClassOrInterface) {
return new AnnotatedWith<A>(annotation, (ClassOrInterface<A>)at);
} else if (at instanceof AppliedUnionType) {
Sequential<? extends Type<?>> caseTypes = ((AppliedUnionType<?>)at).getCaseTypes();
return or(mapTypesToDeclarationAnnotatedWith(annotation, caseTypes));
} else if (at instanceof AppliedIntersectionType) {
Sequential<? extends Type<?>> satisfiedTypes = ((AppliedIntersectionType<?>)at).getSatisfiedTypes();
return and(mapTypesToDeclarationAnnotatedWith(annotation, satisfiedTypes));
} else {
throw new EnumeratedTypeError("Supposedly exhaustive switch was not exhaustive: "+at);
}
}
private static Predicate<Declaration>[] mapTypesToDeclarationAnnotatedWith(TypeDescriptor annotation,
Sequential<? extends Type<?>> caseTypes) {
@SuppressWarnings("unchecked")
Predicate<Declaration>[] preds = new Predicate[Util.toInt(caseTypes.getSize())];
int ii = 0;
Iterator<? extends Type<?>> iterator = caseTypes.iterator();
Object element = iterator.next();
while (element instanceof Type) {
preds[ii++] = isDeclarationAnnotatedWith(annotation, (Type<?>)element);
element = iterator.next();
}
return preds;
}
/**
* Returns a Predicate on Annotations that are of the given type
* @param $reifiedAnnotation
* @return
*/
static <A extends java.lang.annotation.Annotation> Predicate<A> isAnnotationOfType(TypeDescriptor $reifiedAnnotation) {
Type<?> at = Metamodel.getAppliedMetamodel($reifiedAnnotation);
return isAnnotationOfType($reifiedAnnotation, at);
}
private static <A extends java.lang.annotation.Annotation> Predicate<A> isAnnotationOfType(
TypeDescriptor $reifiedAnnotation, Type<?> at)
throws EnumeratedTypeError {
if (at instanceof nothingType_) {
return false_();
} else if (at instanceof AppliedUnionType) {
Sequential<? extends Type<?>> caseTypes = ((AppliedUnionType<?>)at).getCaseTypes();
return or(Predicates.<A>mapTypesToIsAnnotationOfType($reifiedAnnotation, caseTypes));
} else if (at instanceof AppliedIntersectionType) {
Sequential<? extends Type<?>> satisfiedTypes = ((AppliedIntersectionType<?>)at).getSatisfiedTypes();
return and(Predicates.<A>mapTypesToIsAnnotationOfType($reifiedAnnotation, satisfiedTypes));
} else if (at instanceof ClassOrInterface) {
// TODO What if reified is Annotation, or ContrainedAnnotation, or OptionalAnnotation, or SequencedAnnotation?
@SuppressWarnings({ "rawtypes", "unchecked" })
AnnotationPredicate<A> predicate = annotationPredicate($reifiedAnnotation, (ClassOrInterface)at);
return predicate;
} else {
throw new EnumeratedTypeError("Supposedly exhaustive switch was not exhaustive: "+at);
}
}
private static <A extends java.lang.annotation.Annotation> Predicate<A>[] mapTypesToIsAnnotationOfType(TypeDescriptor $reifiedAnnotation,
Sequential<? extends Type<?>> caseTypes) {
@SuppressWarnings("unchecked")
Predicate<A>[] preds = new Predicate[Util.toInt(caseTypes.getSize())];
int ii = 0;
Iterator<? extends Type<?>> iterator = caseTypes.iterator();
Object element = iterator.next();
while (element instanceof Type) {
preds[ii++] = isAnnotationOfType($reifiedAnnotation, (Type<?>)element);
element = iterator.next();
}
return preds;
}
static interface AnnotationPredicate<A extends java.lang.annotation.Annotation> extends Predicates.Predicate<A> {
// TODO Is this a worthwhile optimization to make?
/**
* Whether we should instantiate the given Java annotation into a
* Ceylon annotation. If this returns true
* {@link #accept(ceylon.language.meta.model.Annotation)}
* will still be called to determine whether the Ceylon annotation
* meets the acceptance criteria.
*/
public boolean shouldInstantiate(Class<? extends java.lang.annotation.Annotation> jAnnotationType);
/**
* Whether the given Ceylon annotation should be
* added to the results
*/
public boolean accept(A cAnnotation);
}
private static <A extends java.lang.annotation.Annotation,
Value extends java.lang.annotation.Annotation, Values, ProgramElement extends Annotated>
AnnotationPredicate<A> annotationPredicate(
final TypeDescriptor $reifiedValues,
ClassOrInterface<? extends java.lang.annotation.Annotation> annotationType) {
final Class<?> refAnnotationClass = Metamodel.getReflectedAnnotationClass(annotationType);
final Class<?> refAnnotationType;
final Class<?> refAnnotationWrapperType;
if (java.lang.annotation.Annotation.class == refAnnotationClass
|| ceylon.language.Annotation.class == refAnnotationClass
|| ceylon.language.ConstrainedAnnotation.class == refAnnotationClass
|| ceylon.language.OptionalAnnotation.class == refAnnotationClass
|| ceylon.language.SequencedAnnotation.class == refAnnotationClass) {
return new AnnotationPredicate<A>() {
@Override
public boolean shouldInstantiate(
Class<? extends java.lang.annotation.Annotation> jAnnotationType) {
return true;
}
@Override
public boolean accept(A cAnnotation) {
return Metamodel.isReified(cAnnotation, $reifiedValues);
}
};
} else {
if (refAnnotationClass.getAnnotation(Ceylon.class) == null) {
// Java annotation
refAnnotationType = refAnnotationClass;
refAnnotationWrapperType = null;
}else{
try {
refAnnotationType = Class.forName(refAnnotationClass.getName()+"$annotation$",
false, refAnnotationClass.getClassLoader());
} catch (ClassNotFoundException e) {
throw Metamodel.newModelError("Class not found: " + refAnnotationClass.getName()+"$annotation$");
}
Class<?> c;
try {
c = Class.forName(refAnnotationClass.getName()+"$annotations$",
false, refAnnotationClass.getClassLoader());
} catch (ClassNotFoundException e) {
c = null;
}
refAnnotationWrapperType = c;
}
}
return new AnnotationPredicate<A>() {
@Override
public boolean shouldInstantiate(
Class<? extends java.lang.annotation.Annotation> jAnnotationType) {
return refAnnotationType == null || refAnnotationType.isAssignableFrom(jAnnotationType)
|| (refAnnotationWrapperType != null
&& refAnnotationWrapperType.isAssignableFrom(jAnnotationType));
}
@Override
public boolean accept(A cAnnotation) {
return refAnnotationClass.isInstance(cAnnotation);
}
};
}
}