{"id":289943,"date":"2018-09-26T19:00:02","date_gmt":"2018-09-26T15:00:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=289943"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=289943","title":{"rendered":"\u0418\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b \u0434\u043b\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u0430\u043d\u043d\u043e\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u0432 Java"},"content":{"rendered":"\n<div class=\"post__text post__text-html js-mediator-article\">\n<p>\u0412 \u0441\u0442\u0430\u0442\u044c\u0435 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u043e\u0431\u0437\u043e\u0440 \u0442\u0440\u0435\u0445 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0434\u043b\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u0430\u043d\u043d\u043e\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u0432 java \u043f\u0440\u043e\u0435\u043a\u0442\u0435. <\/p>\n<p>  <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/501\/122\/416\/50112241639d6082dcb5779d13b72174.png\" alt=\"image\"><\/p>\n<p><a name=\"habracut\"><\/a>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0422\u0440\u0435\u043d\u0438\u0440\u043e\u0432\u043e\u0447\u043d\u044b\u0435 \u043a\u043e\u0448\u043a\u0438<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"java\">@Retention(RetentionPolicy.RUNTIME) public @interface MyAnnotation {}  @MyAnnotation public class Bar {}  @MyAnnotation public class Foo {}<\/code><\/pre>\n<\/div>\n<\/div>\n<p>  <\/p>\n<h2 id=\"spring\">Spring<\/h2>\n<p>  <\/p>\n<p>\u0412 Spring \u0434\u043b\u044f \u044d\u0442\u0438\u0445 \u0446\u0435\u043b\u0435\u0439 \u0441\u043b\u0443\u0436\u0438\u0442 ClassPathScanningCandidateComponentProvider. <\/p>\n<p>  <\/p>\n<blockquote><p>\u041e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044c: \u043b\u0435\u0437\u0435\u0442 \u0432 ClassPath, \u0438\u0449\u0435\u0442 \u043a\u043b\u0430\u0441\u0441\u044b \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u044f\u044e\u0442 \u0437\u0430\u0434\u0430\u043d\u043d\u044b\u043c \u0443\u0441\u043b\u043e\u0432\u0438\u044f\u043c<\/p><\/blockquote>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0414\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438<\/b><\/p>\n<div class=\"spoiler_text\">\n<p>\u0438\u043c\u0435\u0435\u0442 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0434\u0440\u0443\u0433\u0438\u0445 \u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432 (\u0434\u043b\u044f \u0442\u0438\u043f\u0430, \u0434\u043b\u044f \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u0438 \u0442.\u0434.)<\/p>\n<\/div>\n<\/div>\n<p>  <\/p>\n<h4 id=\"primer\">\u041f\u0440\u0438\u043c\u0435\u0440<\/h4>\n<p>  <\/p>\n<pre><code class=\"java\">@Benchmark public void spring() {    ClassPathScanningCandidateComponentProvider scanner =                          new ClassPathScanningCandidateComponentProvider(false);    scanner.addIncludeFilter(new AnnotationTypeFilter(MyAnnotation.class));     List&lt;String&gt; collect = scanner       .findCandidateComponents(\"edu.pqdn.scanner\")       .stream()       .map(BeanDefinition::getBeanClassName)       .filter(Objects::nonNull)       .collect(Collectors.toList());     assertEquals(collect.size(), 2);    assertTrue(collect.contains(\"edu.pqdn.scanner.test.Bar\"));    assertTrue(collect.contains(\"edu.pqdn.scanner.test.Foo\")); }<\/code><\/pre>\n<p>  <\/p>\n<h2 id=\"reflectionshttpsgithubcomronmamoreflections\"><a href=\"https:\/\/github.com\/ronmamo\/reflections\">Reflections<\/a><\/h2>\n<p>  <\/p>\n<blockquote><p>\u041e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044c: \u043b\u0435\u0437\u0435\u0442 \u0432 ClassPath, \u0438\u0449\u0435\u0442 \u043a\u043b\u0430\u0441\u0441\u044b \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u044f\u044e\u0442 \u0437\u0430\u0434\u0430\u043d\u043d\u044b\u043c \u0443\u0441\u043b\u043e\u0432\u0438\u044f\u043c<\/p><\/blockquote>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0414\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438<\/b><\/p>\n<div class=\"spoiler_text\">\n<ul>\n<li>get all subtypes of some type<\/li>\n<li>get all types\/members annotated with some annotation<\/li>\n<li>get all resources matching a regular expression<\/li>\n<li>get all methods with specific signature including parameters, parameter annotations and return type<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">dependency<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code>&lt;dependency&gt;     &lt;groupId&gt;org.reflections&lt;\/groupId&gt;     &lt;artifactId&gt;reflections&lt;\/artifactId&gt;     &lt;version&gt;0.9.11&lt;\/version&gt; &lt;\/dependency&gt;<\/code><\/pre>\n<\/div>\n<\/div>\n<p>  <\/p>\n<h4 id=\"primer-1\">\u041f\u0440\u0438\u043c\u0435\u0440<\/h4>\n<p>  <\/p>\n<pre><code class=\"java\">@Benchmark public void reflection() {    Reflections reflections = new Reflections(\"edu.pqdn.scanner\");    Set&lt;Class&lt;?&gt;&gt; set = reflections.getTypesAnnotatedWith(MyAnnotation.class);     List&lt;String&gt; collect = set.stream()       .map(Class::getCanonicalName)       .collect(Collectors.toList());     assertEquals(collect.size(), 2);    assertTrue(collect.contains(\"edu.pqdn.scanner.test.Bar\"));    assertTrue(collect.contains(\"edu.pqdn.scanner.test.Foo\")); }<\/code><\/pre>\n<p>  <\/p>\n<h2 id=\"classindexhttpsgithubcomatteoclassindex\"><a href=\"https:\/\/github.com\/atteo\/classindex\">classindex<\/a><\/h2>\n<p>  <\/p>\n<blockquote><p>\u041e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044c: \u041d\u0415 \u043b\u0435\u0437\u0435\u0442 \u0432 ClassPath, \u0432\u043c\u0435\u0441\u0442\u043e \u044d\u0442\u043e \u043a\u043b\u0430\u0441\u0441\u044b \u0438\u043d\u0434\u0435\u043a\u0441\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u043d\u0430 \u044d\u0442\u0430\u043f\u0435 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438<\/p><\/blockquote>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">dependency<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code>&lt;dependency&gt;     &lt;groupId&gt;org.atteo.classindex&lt;\/groupId&gt;     &lt;artifactId&gt;classindex&lt;\/artifactId&gt;     &lt;version&gt;3.4&lt;\/version&gt; &lt;\/dependency&gt;<\/code><\/pre>\n<\/div>\n<\/div>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0422\u0440\u0435\u043d\u0438\u0440\u043e\u0432\u043e\u0447\u043d\u044b\u0435 \u043a\u043e\u0448\u043a\u0438<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"java\">@IndexMyAnnotated public @interface IndexerAnnotation {}  @IndexerMyAnnotation public class Bar {}  @IndexerMyAnnotation public class Foo {}<\/code><\/pre>\n<\/div>\n<\/div>\n<p>  <\/p>\n<h4 id=\"primer-2\">\u041f\u0440\u0438\u043c\u0435\u0440<\/h4>\n<p>  <\/p>\n<pre><code class=\"java\">@Benchmark public void indexer() {    Iterable&lt;Class&lt;?&gt;&gt; iterable = ClassIndex.getAnnotated(IndexerMyAnnotation.class);     List&lt;String&gt; list = StreamSupport.stream(iterable.spliterator(), false)       .map(aClass -&gt; aClass.getCanonicalName())       .collect(Collectors.toList());     assertEquals(list.size(), 2);    assertTrue(list.contains(\"edu.pqdn.scanner.classindexer.test.Bar\"));    assertTrue(list.contains(\"edu.pqdn.scanner.classindexer.test.Foo\")); }<\/code><\/pre>\n<p>  <\/p>\n<h2 id=\"jmh\">JMH<\/h2>\n<p>  <\/p>\n<pre><code>Benchmark                    Mode  Cnt  Score   Error  Units ScannerBenchmark.indexer     avgt   50  0,100 ? 0,001  ms\/op ScannerBenchmark.reflection  avgt   50  5,157 ? 0,047  ms\/op ScannerBenchmark.spring      avgt   50  4,706 ? 0,294  ms\/op<\/code><\/pre>\n<p>  <\/p>\n<h2 id=\"zaklyuchenie\">\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h2>\n<p>  <\/p>\n<p>\u041a\u0430\u043a \u0432\u0438\u0434\u043d\u043e indexer \u0441\u0430\u043c\u044b\u0439 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442, \u043e\u0434\u043d\u0430\u043a\u043e, \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0438 \u043f\u043e \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u043f\u043e\u0438\u0441\u043a \u0434\u043e\u043b\u0436\u043d\u044b \u0438\u043c\u0435\u0442\u044c \u0441\u0442\u0435\u0440\u0435\u043e\u0442\u0438\u043f @IndexAnnotated. <\/p>\n<p>  <\/p>\n<p>\u0414\u0440\u0443\u0433\u0438\u0435 \u0434\u0432\u0430 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430 \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u0437\u0430\u043c\u0435\u0442\u043d\u043e \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u0435\u0435, \u043e\u0434\u043d\u0430\u043a\u043e \u0434\u043b\u044f \u0438\u0445 \u0440\u0430\u0431\u043e\u0442\u044b \u043d\u0438\u043a\u0430\u043a\u043e\u0433\u043e \u0448\u0430\u043c\u0430\u043d\u0441\u0442\u0432\u0430 \u0441 \u043a\u043e\u0434\u043e\u043c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u044c \u043d\u0435 \u043d\u0430\u0434\u043e. \u041d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u043a \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u043d\u0438\u0432\u0435\u043b\u0438\u0440\u0443\u0435\u0442\u0441\u044f, \u0435\u0441\u043b\u0438 \u043f\u043e\u0438\u0441\u043a \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 \u0441\u0442\u0430\u0440\u0442\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f<\/p>\n<\/div>\n<p>        <script class=\"js-mediator-script\">!function(e){function t(t,n){if(!(n in e)){for(var r,a=e.document,i=a.scripts,o=i.length;o--;)if(-1!==i[o].src.indexOf(t)){r=i[o];break}if(!r){r=a.createElement(\"script\"),r.type=\"text\/javascript\",r.async=!0,r.defer=!0,r.src=t,r.charset=\"UTF-8\";var d=function(){var e=a.getElementsByTagName(\"script\")[0];e.parentNode.insertBefore(r,e)};\"[object Opera]\"==e.opera?a.addEventListener?a.addEventListener(\"DOMContentLoaded\",d,!1):e.attachEvent(\"onload\",d):d()}}}t(\"\/\/mediator.mail.ru\/script\/2820404\/\",\"_mediator\")}(window);<\/script>     <br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/post\/424511\/\"> https:\/\/habr.com\/post\/424511\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html js-mediator-article\">\n<p>\u0412 \u0441\u0442\u0430\u0442\u044c\u0435 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u043e\u0431\u0437\u043e\u0440 \u0442\u0440\u0435\u0445 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0434\u043b\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u0430\u043d\u043d\u043e\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u0432 java \u043f\u0440\u043e\u0435\u043a\u0442\u0435. <\/p>\n<p>  <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/501\/122\/416\/50112241639d6082dcb5779d13b72174.png\" alt=\"image\"><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-289943","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/289943","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=289943"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/289943\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=289943"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=289943"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=289943"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}