View Javadoc
1   /**
2    * Copyright (c) 2013-2022, jcabi.com
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met: 1) Redistributions of source code must retain the above
8    * copyright notice, this list of conditions and the following
9    * disclaimer. 2) Redistributions in binary form must reproduce the above
10   * copyright notice, this list of conditions and the following
11   * disclaimer in the documentation and/or other materials provided
12   * with the distribution. 3) Neither the name of the jcabi.com nor
13   * the names of its contributors may be used to endorse or promote
14   * products derived from this software without specific prior written
15   * permission.
16   *
17   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
19   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28   * OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  package com.jcabi.github;
31  
32  import com.google.common.base.Function;
33  import com.google.common.base.Predicate;
34  import com.google.common.collect.Iterables;
35  import java.lang.reflect.Method;
36  import java.lang.reflect.Modifier;
37  import java.util.Arrays;
38  import org.junit.rules.TestRule;
39  import org.junit.runner.Description;
40  import org.junit.runners.model.Statement;
41  import org.reflections.Reflections;
42  import org.reflections.scanners.ResourcesScanner;
43  import org.reflections.scanners.SubTypesScanner;
44  import org.reflections.util.ClasspathHelper;
45  import org.reflections.util.ConfigurationBuilder;
46  import org.reflections.util.FilterBuilder;
47  
48  /**
49   * Utility class which provides convenient methods for annotations check etc.
50   * @author Paul Polishchuk (ppol@ua.fm)
51   * @version $Id: 6ca6f211bdeca480b89ff8acc75fd93966146431 $
52   * @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
53   */
54  public final class ClasspathRule implements TestRule {
55  
56      /**
57       * Provides all classes in package 'com.jcabi.github'.
58       * @return Classes
59       */
60      public Iterable<Class<?>> allTypes() {
61          return Iterables.filter(
62              new Reflections(
63                  new ConfigurationBuilder()
64                      .setScanners(
65                          new SubTypesScanner(false),
66                          new ResourcesScanner()
67                  )
68                      .setUrls(
69                          ClasspathHelper.forClassLoader(
70                              ClasspathHelper.contextClassLoader(),
71                              ClasspathHelper.staticClassLoader()
72                          )
73                      ).filterInputsBy(
74                      new FilterBuilder().include(
75                          FilterBuilder.prefix("com.jcabi.github")
76                      )
77                      )
78              ).getSubTypesOf(Object.class),
79              new Predicate<Class<?>>() {
80                  @Override
81                  public boolean apply(final Class<?> input) {
82                      final String name = input.getName();
83                      // @checkstyle BooleanExpressionComplexityCheck (6 lines)
84                      return !name.endsWith("Test")
85                          && !name.endsWith("ITCase")
86                          && !name.endsWith("ClasspathRule")
87                          && !name.endsWith("RepoRule")
88                          && (input.getEnclosingClass() == null
89                          || name.endsWith("Smart"));
90                  }
91              }
92          );
93      }
94  
95      @Override
96      public Statement apply(final Statement statement,
97          final Description description) {
98          return new Statement() {
99              @Override
100             // @checkstyle IllegalThrowsCheck (1 line)
101             public void evaluate() throws Throwable {
102                 statement.evaluate();
103             }
104         };
105     }
106 
107     /**
108      * Provides all public methods from classes in package 'com.jcabi.github'.
109      * @return Methods
110      */
111     public Iterable<Method> allPublicMethods() {
112         return Iterables.concat(
113             Iterables.transform(
114                 this.allTypes(),
115                 new Function<Class<?>, Iterable<Method>>() {
116                     @Override
117                     public Iterable<Method> apply(final Class<?> input) {
118                         return Iterables.filter(
119                             Arrays.asList(input.getDeclaredMethods()),
120                             new Predicate<Method>() {
121                                 @Override
122                                 public boolean apply(final Method method) {
123                                     return Modifier.isPublic(
124                                         method.getModifiers()
125                                     );
126                                 }
127                             }
128                         );
129                     }
130                 }
131             )
132         );
133     }
134 }