1 /**
2 * Copyright (c) 2013-2023, 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.collect.Iterables;
33 import java.lang.reflect.Method;
34 import java.lang.reflect.Modifier;
35 import java.util.Arrays;
36 import org.junit.rules.TestRule;
37 import org.junit.runner.Description;
38 import org.junit.runners.model.Statement;
39 import org.reflections.Reflections;
40 import org.reflections.scanners.ResourcesScanner;
41 import org.reflections.scanners.SubTypesScanner;
42 import org.reflections.util.ClasspathHelper;
43 import org.reflections.util.ConfigurationBuilder;
44 import org.reflections.util.FilterBuilder;
45
46 /**
47 * Utility class which provides convenient methods for annotations check etc.
48 * @author Paul Polishchuk (ppol@ua.fm)
49 * @version $Id: c0b978df0d380c99a4c5f3049112ae57b993eabd $
50 * @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
51 */
52 public final class ClasspathRule implements TestRule {
53
54 /**
55 * Provides all classes in package 'com.jcabi.github'.
56 * @return Classes
57 */
58 public Iterable<Class<?>> allTypes() {
59 return Iterables.filter(
60 new Reflections(
61 new ConfigurationBuilder()
62 .setScanners(
63 new SubTypesScanner(false),
64 new ResourcesScanner()
65 )
66 .setUrls(
67 ClasspathHelper.forClassLoader(
68 ClasspathHelper.contextClassLoader(),
69 ClasspathHelper.staticClassLoader()
70 )
71 ).filterInputsBy(
72 new FilterBuilder().include(
73 FilterBuilder.prefix("com.jcabi.github")
74 )
75 )
76 ).getSubTypesOf(Object.class),
77 input -> {
78 final String name = input.getName();
79 // @checkstyle BooleanExpressionComplexityCheck (6 lines)
80 return !name.endsWith("Test")
81 && !name.endsWith("ITCase")
82 && !name.endsWith("ClasspathRule")
83 && !name.endsWith("RepoRule")
84 && (input.getEnclosingClass() == null
85 || name.endsWith("Smart"));
86 }
87 );
88 }
89
90 @Override
91 public Statement apply(final Statement statement,
92 final Description description) {
93 return new Statement() {
94 @Override
95 // @checkstyle IllegalThrowsCheck (1 line)
96 public void evaluate() throws Throwable {
97 statement.evaluate();
98 }
99 };
100 }
101
102 /**
103 * Provides all public methods from classes in package 'com.jcabi.github'.
104 * @return Methods
105 */
106 public Iterable<Method> allPublicMethods() {
107 return Iterables.concat(
108 Iterables.transform(
109 this.allTypes(),
110 input -> Iterables.filter(
111 Arrays.asList(input.getDeclaredMethods()),
112 method -> Modifier.isPublic(
113 method.getModifiers()
114 )
115 )
116 )
117 );
118 }
119 }