/
AnnotationParser.java
1014 lines (902 loc) · 30.5 KB
/
AnnotationParser.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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// ========================================================================
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.annotations;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.eclipse.jetty.util.JavaVersion;
import org.eclipse.jetty.util.Loader;
import org.eclipse.jetty.util.MultiException;
import org.eclipse.jetty.util.MultiReleaseJarFile;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.TypeUtil;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.resource.Resource;
import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
/**
* AnnotationParser
* <p>
* Use asm to scan classes for annotations. A SAX-style parsing is done.
* Handlers are registered which will be called back when various types of
* entity are encountered, eg a class, a method, a field.
* <p>
* Handlers are not called back in any particular order and are assumed
* to be order-independent.
* <p>
* As a registered Handler will be called back for each annotation discovered
* on a class, a method, a field, the Handler should test to see if the annotation
* is one that it is interested in.
* <p>
* For the servlet spec, we are only interested in annotations on classes, methods and fields,
* so the callbacks for handling finding a class, a method a field are themselves
* not fully implemented.
*/
public class AnnotationParser
{
private static final Logger LOG = Log.getLogger(AnnotationParser.class);
private static final int ASM_VERSION = asmVersion();
/**
* Map of classnames scanned and the first location from which scan occurred
*/
protected Map<String, Resource> _parsedClassNames = new ConcurrentHashMap<>();
private final int _javaPlatform;
private final int _asmVersion;
/**
* Determine the runtime version of asm.
*
* @return the {@link org.objectweb.asm.Opcodes} ASM value matching the runtime version of asm.
*/
private static int asmVersion()
{
// Find the highest available ASM version on the runtime/classpath, because
// if we run with a lower than available ASM version, against a class with
// new language features we'll get an UnsupportedOperationException, even if
// the ASM version supports the new language features.
// Also, if we run with a higher than available ASM version, we'll get
// an IllegalArgumentException from org.objectweb.asm.ClassVisitor.
// So must find exactly the maximum ASM api version available.
Optional<Integer> asmVersion = Arrays.stream(Opcodes.class.getFields()).sequential()
.filter((f) -> f.getName().matches("ASM[0-9]+"))
.map((f) -> f.getName().substring(3))
.map(Integer::parseInt)
.max(Integer::compareTo);
if (!asmVersion.isPresent())
throw new IllegalStateException("Invalid " + Opcodes.class.getName());
int asmFieldId = asmVersion.get();
try
{
String fieldName = "ASM" + asmFieldId;
if (LOG.isDebugEnabled())
LOG.debug("Using ASM API from {}.{}", Opcodes.class.getName(), fieldName);
return (int)Opcodes.class.getField(fieldName).get(null);
}
catch (Throwable e)
{
throw new IllegalStateException(e);
}
}
/**
* Convert internal name to simple name
*
* @param name the internal name
* @return the simple name
*/
public static String normalize(String name)
{
if (name == null)
return null;
if (name.startsWith("L") && name.endsWith(";"))
name = name.substring(1, name.length() - 1);
if (name.endsWith(".class"))
name = name.substring(0, name.length() - ".class".length());
return StringUtil.replace(name, '/', '.');
}
/**
* Convert internal names to simple names.
*
* @param list the list of internal names
* @return the list of simple names
*/
public static String[] normalize(String[] list)
{
if (list == null)
return null;
String[] normalList = new String[list.length];
int i = 0;
for (String s : list)
{
normalList[i++] = normalize(s);
}
return normalList;
}
/**
* Immutable information gathered by parsing class header.
*/
public class ClassInfo
{
final Resource _containingResource;
final String _className;
final int _version;
final int _access;
final String _signature;
final String _superName;
final String[] _interfaces;
public ClassInfo(Resource resource, String className, int version, int access, String signature, String superName, String[] interfaces)
{
super();
_containingResource = resource;
_className = className;
_version = version;
_access = access;
_signature = signature;
_superName = superName;
_interfaces = interfaces;
}
public String getClassName()
{
return _className;
}
public int getVersion()
{
return _version;
}
public int getAccess()
{
return _access;
}
public String getSignature()
{
return _signature;
}
public String getSuperName()
{
return _superName;
}
public String[] getInterfaces()
{
return _interfaces;
}
public Resource getContainingResource()
{
return _containingResource;
}
}
/**
* Immutable information gathered by parsing a method on a class.
*/
public class MethodInfo
{
final ClassInfo _classInfo;
final String _methodName;
final int _access;
final String _desc;
final String _signature;
final String[] _exceptions;
public MethodInfo(ClassInfo classInfo, String methodName, int access, String desc, String signature, String[] exceptions)
{
super();
_classInfo = classInfo;
_methodName = methodName;
_access = access;
_desc = desc;
_signature = signature;
_exceptions = exceptions;
}
public ClassInfo getClassInfo()
{
return _classInfo;
}
public String getMethodName()
{
return _methodName;
}
public int getAccess()
{
return _access;
}
public String getDesc()
{
return _desc;
}
public String getSignature()
{
return _signature;
}
public String[] getExceptions()
{
return _exceptions;
}
}
/**
* Immutable information gathered by parsing a field on a class.
*/
public class FieldInfo
{
final ClassInfo _classInfo;
final String _fieldName;
final int _access;
final String _fieldType;
final String _signature;
final Object _value;
public FieldInfo(ClassInfo classInfo, String fieldName, int access, String fieldType, String signature, Object value)
{
super();
_classInfo = classInfo;
_fieldName = fieldName;
_access = access;
_fieldType = fieldType;
_signature = signature;
_value = value;
}
public ClassInfo getClassInfo()
{
return _classInfo;
}
public String getFieldName()
{
return _fieldName;
}
public int getAccess()
{
return _access;
}
public String getFieldType()
{
return _fieldType;
}
public String getSignature()
{
return _signature;
}
public Object getValue()
{
return _value;
}
}
/**
* Signature for all handlers that respond to parsing class files.
*/
public interface Handler
{
void handle(ClassInfo classInfo);
void handle(MethodInfo methodInfo);
void handle(FieldInfo fieldInfo);
void handle(ClassInfo info, String annotationName);
void handle(MethodInfo info, String annotationName);
void handle(FieldInfo info, String annotationName);
}
/**
* Convenience base class to provide no-ops for all Handler methods.
*/
public abstract static class AbstractHandler implements Handler
{
@Override
public void handle(ClassInfo classInfo)
{
}
@Override
public void handle(MethodInfo methodInfo)
{
}
@Override
public void handle(FieldInfo fieldInfo)
{
}
@Override
public void handle(ClassInfo info, String annotationName)
{
}
@Override
public void handle(MethodInfo info, String annotationName)
{
}
@Override
public void handle(FieldInfo info, String annotationName)
{
}
}
/**
* ASM Visitor for parsing a method. We are only interested in the annotations on methods.
*/
public class MyMethodVisitor extends MethodVisitor
{
final MethodInfo _mi;
final Set<? extends Handler> _handlers;
public MyMethodVisitor(final Set<? extends Handler> handlers,
final ClassInfo classInfo,
final int access,
final String name,
final String methodDesc,
final String signature,
final String[] exceptions,
final int asmVersion)
{
super(asmVersion);
_handlers = handlers;
_mi = new MethodInfo(classInfo, name, access, methodDesc, signature, exceptions);
}
/**
* We are only interested in finding the annotations on methods.
*/
@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible)
{
String annotationName = normalize(desc);
for (Handler h : _handlers)
{
h.handle(_mi, annotationName);
}
return null;
}
}
/**
* An ASM visitor for parsing Fields.
* We are only interested in visiting annotations on Fields.
*/
public class MyFieldVisitor extends FieldVisitor
{
final FieldInfo _fieldInfo;
final Set<? extends Handler> _handlers;
public MyFieldVisitor(final Set<? extends Handler> handlers,
final ClassInfo classInfo,
final int access,
final String fieldName,
final String fieldType,
final String signature,
final Object value,
final int asmVersion)
{
super(asmVersion);
_handlers = handlers;
_fieldInfo = new FieldInfo(classInfo, fieldName, access, fieldType, signature, value);
}
/**
* Parse an annotation found on a Field.
*/
@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible)
{
String annotationName = normalize(desc);
for (Handler h : _handlers)
{
h.handle(_fieldInfo, annotationName);
}
return null;
}
}
/**
* ASM visitor for a class.
*/
public class MyClassVisitor extends ClassVisitor
{
final int _asmVersion;
final Resource _containingResource;
final Set<? extends Handler> _handlers;
ClassInfo _ci;
public MyClassVisitor(Set<? extends Handler> handlers, Resource containingResource, int asmVersion)
{
super(asmVersion);
_asmVersion = asmVersion;
_handlers = handlers;
_containingResource = containingResource;
}
@Override
public void visit(final int version,
final int access,
final String name,
final String signature,
final String superName,
final String[] interfaces)
{
_ci = new ClassInfo(_containingResource, normalize(name), version, access, signature, normalize(superName), normalize(interfaces));
for (Handler h : _handlers)
{
h.handle(_ci);
}
}
/**
* Visit an annotation on a Class
*/
@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible)
{
String annotationName = normalize(desc);
for (Handler h : _handlers)
{
h.handle(_ci, annotationName);
}
return null;
}
/**
* Visit a method to extract its annotations
*/
@Override
public MethodVisitor visitMethod(final int access,
final String name,
final String methodDesc,
final String signature,
final String[] exceptions)
{
return new MyMethodVisitor(_handlers, _ci, access, name, methodDesc, signature, exceptions, _asmVersion);
}
/**
* Visit a field to extract its annotations
*/
@Override
public FieldVisitor visitField(final int access,
final String fieldName,
final String fieldType,
final String signature,
final Object value)
{
return new MyFieldVisitor(_handlers, _ci, access, fieldName, fieldType, signature, value, _asmVersion);
}
}
public AnnotationParser()
{
this(JavaVersion.VERSION.getPlatform());
}
/**
* @param javaPlatform The target java version or 0 for the current runtime.
*/
public AnnotationParser(int javaPlatform)
{
_asmVersion = ASM_VERSION;
if (javaPlatform == 0)
javaPlatform = JavaVersion.VERSION.getPlatform();
_javaPlatform = javaPlatform;
}
public AnnotationParser(int javaPlatform, int asmVersion)
{
if (javaPlatform == 0)
javaPlatform = JavaVersion.VERSION.getPlatform();
_javaPlatform = javaPlatform;
if (asmVersion == 0)
asmVersion = ASM_VERSION;
_asmVersion = asmVersion;
}
/**
* Add a class as having been parsed.
*
* @param classname the name of the class
* @param location the fully qualified location of the class
*/
public void addParsedClass(String classname, Resource location)
{
Resource existing = _parsedClassNames.putIfAbsent(classname, location);
if (existing != null)
LOG.warn("{} scanned from multiple locations: {}, {}", classname, existing, location);
}
/**
* Get the locations of the given classname. There may be more than one
* location if there are duplicates of the same class.
*
* @param classname the name of the class
* @return an immutable list of locations
* @deprecated List of duplicate locations no longer stored
*/
@Deprecated
public List<String> getParsedLocations(String classname)
{
return Collections.emptyList();
}
/**
* Parse a given class
*
* @param handlers the set of handlers to find class
* @param className the class name to parse
* @throws Exception if unable to parse
*/
public void parse(Set<? extends Handler> handlers, String className) throws Exception
{
if (className == null)
return;
String classRef = TypeUtil.toClassReference(className);
URL resource = Loader.getResource(classRef);
if (resource != null)
{
Resource r = Resource.newResource(resource);
addParsedClass(className, r);
try (InputStream is = r.getInputStream())
{
scanClass(handlers, null, is);
}
}
}
/**
* Parse the given class, optionally walking its inheritance hierarchy
*
* @param handlers the handlers to look for class in
* @param clazz the class to look for
* @param visitSuperClasses if true, also visit super classes for parse
* @throws Exception if unable to parse class
*/
public void parse(Set<? extends Handler> handlers, Class<?> clazz, boolean visitSuperClasses) throws Exception
{
Class<?> cz = clazz;
while (cz != Object.class)
{
String nameAsResource = TypeUtil.toClassReference(cz);
URL resource = Loader.getResource(nameAsResource);
if (resource != null)
{
Resource r = Resource.newResource(resource);
addParsedClass(clazz.getName(), r);
try (InputStream is = r.getInputStream())
{
scanClass(handlers, null, is);
}
}
if (visitSuperClasses)
cz = cz.getSuperclass();
else
break;
}
}
/**
* Parse the given classes
*
* @param handlers the set of handlers to look for class in
* @param classNames the class name
* @throws Exception if unable to parse
*/
public void parse(Set<? extends Handler> handlers, String[] classNames) throws Exception
{
if (classNames == null)
return;
parse(handlers, Arrays.asList(classNames));
}
/**
* Parse the given classes
*
* @param handlers the set of handlers to look for class in
* @param classNames the class names
* @throws Exception if unable to parse
*/
public void parse(Set<? extends Handler> handlers, List<String> classNames) throws Exception
{
MultiException me = new MultiException();
for (String className : classNames)
{
try
{
String classRef = TypeUtil.toClassReference(className);
URL resource = Loader.getResource(classRef);
if (resource != null)
{
Resource r = Resource.newResource(resource);
addParsedClass(className, r);
try (InputStream is = r.getInputStream())
{
scanClass(handlers, null, is);
}
}
}
catch (Exception e)
{
me.add(new RuntimeException("Error scanning class " + className, e));
}
}
me.ifExceptionThrow();
}
/**
* Parse all classes in a directory
*
* @param handlers the set of handlers to look for classes in
* @param root the resource directory to look for classes
* @throws Exception if unable to parse
*/
protected void parseDir(Set<? extends Handler> handlers, Resource root) throws Exception
{
if (!root.isDirectory() || !root.exists() || root.getName().startsWith("."))
return;
if (LOG.isDebugEnabled())
LOG.debug("Scanning dir {}", root);
File rootFile = root.getFile();
MultiException me = new MultiException();
Collection<Resource> resources = root.getAllResources();
if (resources != null)
{
for (Resource r : resources)
{
if (r.isDirectory())
continue;
File file = r.getFile();
if (isValidClassFileName((file == null ? null : file.getName())))
{
Path classpath = rootFile.toPath().relativize(file.toPath());
String str = classpath.toString();
str = str.substring(0, str.lastIndexOf(".class"));
str = StringUtil.replace(str, File.separatorChar, '.');
try
{
if (LOG.isDebugEnabled())
LOG.debug("Scanning class {}", r);
addParsedClass(str, r);
try (InputStream is = r.getInputStream())
{
scanClass(handlers, Resource.newResource(file.getParentFile()), is);
}
}
catch (Exception ex)
{
if (LOG.isDebugEnabled())
LOG.debug("Error scanning file " + file, ex);
me.add(new RuntimeException("Error scanning file " + file, ex));
}
}
else
{
if (LOG.isDebugEnabled())
LOG.debug("Skipping scan on invalid file {}", file);
}
}
}
me.ifExceptionThrow();
}
/**
* Parse classes in the supplied classloader.
* Only class files in jar files will be scanned.
*
* @param handlers the handlers to look for classes in
* @param loader the classloader for the classes
* @param visitParents if true, visit parent classloaders too
* @param nullInclusive if true, an empty pattern means all names match, if false, none match
* @throws Exception if unable to parse
*/
@Deprecated
public void parse(final Set<? extends Handler> handlers, ClassLoader loader, boolean visitParents, boolean nullInclusive) throws Exception
{
throw new UnsupportedOperationException();
}
/**
* Parse classes in the supplied uris.
*
* @param handlers the handlers to look for classes in
* @param uris the uris for the jars
* @throws Exception if unable to parse
*/
public void parse(final Set<? extends Handler> handlers, final URI[] uris) throws Exception
{
if (uris == null)
return;
MultiException me = new MultiException();
for (URI uri : uris)
{
try
{
parse(handlers, uri);
}
catch (Exception e)
{
me.add(new RuntimeException("Problem parsing classes from " + uri, e));
}
}
me.ifExceptionThrow();
}
/**
* Parse a particular uri
*
* @param handlers the handlers to look for classes in
* @param uri the uri for the jar
* @throws Exception if unable to parse
*/
public void parse(final Set<? extends Handler> handlers, URI uri) throws Exception
{
if (uri == null)
return;
parse(handlers, Resource.newResource(uri));
}
/**
* Parse a resource
*
* @param handlers the handlers to look for classes in
* @param r the resource to parse
* @throws Exception if unable to parse
*/
public void parse(final Set<? extends Handler> handlers, Resource r) throws Exception
{
if (r == null)
return;
if (r.exists() && r.isDirectory())
{
parseDir(handlers, r);
return;
}
String fullname = r.toString();
if (fullname.endsWith(".jar"))
{
parseJar(handlers, r);
return;
}
if (fullname.endsWith(".class"))
{
try (InputStream is = r.getInputStream())
{
scanClass(handlers, null, is);
return;
}
}
if (LOG.isDebugEnabled())
LOG.warn("Resource not scannable for classes: {}", r);
}
/**
* Parse a resource that is a jar file.
*
* @param handlers the handlers to look for classes in
* @param jarResource the jar resource to parse
* @throws Exception if unable to parse
*/
protected void parseJar(Set<? extends Handler> handlers, Resource jarResource) throws Exception
{
if (jarResource == null)
return;
if (jarResource.toString().endsWith(".jar"))
{
if (LOG.isDebugEnabled())
LOG.debug("Scanning jar {}", jarResource);
MultiException me = new MultiException();
try (MultiReleaseJarFile jarFile = new MultiReleaseJarFile(jarResource.getFile(), _javaPlatform, false))
{
jarFile.stream().forEach(e ->
{
try
{
parseJarEntry(handlers, jarResource, e);
}
catch (Exception ex)
{
me.add(new RuntimeException("Error scanning entry " + e.getName() + " from jar " + jarResource, ex));
}
});
}
me.ifExceptionThrow();
}
}
/**
* Parse a single entry in a jar file
*
* @param handlers the handlers to look for classes in
* @param entry the entry in the potentially MultiRelease jar resource to parse
* @param jar the jar file
* @throws Exception if unable to parse
*/
protected void parseJarEntry(Set<? extends Handler> handlers, Resource jar, MultiReleaseJarFile.VersionedJarEntry entry)
throws Exception
{
if (jar == null || entry == null)
return;
//skip directories
if (entry.isDirectory())
return;
String name = entry.getName();
//check file is a valid class file name
if (isValidClassFileName(name) && isValidClassFilePath(name))
{
String shortName = StringUtil.replace(name, '/', '.').substring(0, name.length() - 6);
addParsedClass(shortName, Resource.newResource("jar:" + jar.getURI() + "!/" + entry.getNameInJar()));
if (LOG.isDebugEnabled())
LOG.debug("Scanning class from jar {}!/{}", jar, entry);
try (InputStream is = entry.getInputStream())
{
scanClass(handlers, jar, is);
}
}
}
/**
* Use ASM on a class
*
* @param handlers the handlers to look for classes in
* @param containingResource the dir or jar that the class is contained within, can be null if not known
* @param is the input stream to parse
* @throws IOException if unable to parse
*/
protected void scanClass(Set<? extends Handler> handlers, Resource containingResource, InputStream is) throws IOException
{
ClassReader reader = new ClassReader(is);
reader.accept(new MyClassVisitor(handlers, containingResource, _asmVersion), ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
}
/**
* Remove any parsed class names.
*/
public void resetParsedClasses()
{
_parsedClassNames.clear();
}
/**
* Check that the given path represents a valid class file name.
* The check is fairly cursory, checking that:
* <ul>
* <li> the name ends with .class</li>
* <li> it isn't a dot file or in a hidden directory </li>
* <li> the name of the class at least begins with a valid identifier for a class name </li>
* </ul>
*
* @param name the class file name
* @return whether the class file name is valid
*/
public boolean isValidClassFileName(String name)
{
//no name cannot be valid
if (name == null || name.length() == 0)
return false;
//skip anything that is not a class file
String lc = name.toLowerCase(Locale.ENGLISH);
if (!lc.endsWith(".class"))
{
if (LOG.isDebugEnabled())
LOG.debug("Not a class: {}", name);
return false;
}
if (lc.equals("module-info.class"))
{
if (LOG.isDebugEnabled())
LOG.debug("Skipping module-info.class");
return false;
}
//skip any classfiles that are not a valid java identifier
int c0 = 0;
int ldir = name.lastIndexOf('/', name.length() - 6);
c0 = (ldir > -1 ? ldir + 1 : c0);
if (!Character.isJavaIdentifierStart(name.charAt(c0)))
{
if (LOG.isDebugEnabled())
LOG.debug("Not a java identifier: {}", name);
return false;
}
return true;
}
/**
* Check that the given path does not contain hidden directories
*
* @param path the class file path
* @return whether the class file path is valid
*/
public boolean isValidClassFilePath(String path)
{
//no path is not valid