Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Organize project into a reusable library #43

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
199 changes: 199 additions & 0 deletions src/main/java/net/revelc/code/apilyzer/Apilyzer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.revelc.code.apilyzer;

import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;
import net.revelc.code.apilyzer.problems.Problem;
import net.revelc.code.apilyzer.problems.ProblemReporter;
import net.revelc.code.apilyzer.util.ClassUtils;

/**
* The entry point to this library.
*/
public class Apilyzer {

private final ProblemReporter problemReporter;
private final PatternSet allowsPs;
private final boolean ignoreDeprecated;
private final PublicApi publicApi;

/**
* Analyze a given public API definition to ensure it exposes only types available in itself and
* in an allowed set of external APIs.
*/
public Apilyzer(PublicApi publicApi, List<String> allows, boolean ignoreDeprecated,
Consumer<Problem> problemConsumer) {
this.problemReporter = new ProblemReporter(problemConsumer);
this.allowsPs = new PatternSet(allows);
this.ignoreDeprecated = ignoreDeprecated;
this.publicApi = publicApi;
}

private boolean allowedExternalApi(String fqName) {
// TODO make default allows configurable?
if (fqName.startsWith("java.")) {
return true;
}
return allowsPs.anyMatch(fqName);
}

private boolean deprecatedToIgnore(AnnotatedElement element) {
return ignoreDeprecated && element.isAnnotationPresent(Deprecated.class);
}

private boolean isOk(Class<?> clazz) {

while (clazz.isArray()) {
clazz = clazz.getComponentType();
}

if (clazz.isPrimitive()) {
return true;
}

String fqName = clazz.getName();
return publicApi.contains(fqName) || allowedExternalApi(fqName);
}

private boolean checkClass(Class<?> clazz, Set<Class<?>> innerChecked) {

boolean ok = true;

if (deprecatedToIgnore(clazz)) {
return true;
}

// TODO check generic type parameters

for (Field field : ClassUtils.getFields(clazz)) {

if (deprecatedToIgnore(field)) {
continue;
}

if (!field.getDeclaringClass().getName().equals(clazz.getName())
&& isOk(field.getDeclaringClass())) {
continue;
}

if (!isOk(field.getType())) {
problemReporter.field(clazz, field);
ok = false;
}
}

Constructor<?>[] constructors = clazz.getConstructors();
for (Constructor<?> constructor : constructors) {

if (constructor.isSynthetic()) {
continue;
}

if (deprecatedToIgnore(constructor)) {
continue;
}

Class<?>[] params = constructor.getParameterTypes();
for (Class<?> param : params) {
if (!isOk(param)) {
problemReporter.constructorParameter(clazz, param);
ok = false;
}
}

Class<?>[] exceptions = constructor.getExceptionTypes();
for (Class<?> exception : exceptions) {
if (!isOk(exception)) {
problemReporter.constructorException(clazz, exception);
ok = false;
}
}
}

for (Method method : ClassUtils.getMethods(clazz)) {

if (method.isSynthetic() || method.isBridge()) {
continue;
}

if (deprecatedToIgnore(method)) {
continue;
}

if (!method.getDeclaringClass().getName().equals(clazz.getName())
&& isOk(method.getDeclaringClass())) {
continue;
}

if (!isOk(method.getReturnType())) {
problemReporter.methodReturn(clazz, method);
ok = false;
}

Class<?>[] params = method.getParameterTypes();
for (Class<?> param : params) {
if (!isOk(param)) {
problemReporter.methodParameter(clazz, method, param);
ok = false;
}
}

Class<?>[] exceptions = method.getExceptionTypes();
for (Class<?> exception : exceptions) {
if (!isOk(exception)) {
problemReporter.methodException(clazz, method, exception);
ok = false;
}
}
}

for (Class<?> class1 : ClassUtils.getInnerClasses(clazz)) {

if (innerChecked.contains(class1)) {
continue;
}

innerChecked.add(class1);

if (deprecatedToIgnore(class1)) {
continue;
}

if (publicApi.excludes(class1)) {
// this inner class is explicitly excluded from API so do not check it
continue;
}

if (!isOk(class1) && !checkClass(class1, innerChecked)) {
problemReporter.innerClass(clazz, class1);
ok = false;
}
}

return ok;
}

public void check() {
publicApi.classStream().forEach(c -> checkClass(c, new HashSet<Class<?>>()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,29 @@
* limitations under the License.
*/

package net.revelc.code.apilyzer.maven.plugin;
package net.revelc.code.apilyzer;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
* A set of patterns to match classes on the class path.
*/
class PatternSet {
private final List<Pattern> patterns;

PatternSet(List<String> regexs) {
if (regexs.size() == 0) {
patterns = Collections.emptyList();
} else {
patterns = new ArrayList<>();
for (String regex : regexs) {
patterns.add(Pattern.compile(regex));
}
}
patterns = regexs.isEmpty() ? Collections.emptyList()
: regexs.stream().map(Pattern::compile).collect(Collectors.toList());
}

boolean matchesAny(String input) {
for (Pattern pattern : patterns) {
if (pattern.matcher(input).matches()) {
return true;
}
}

return false;
boolean anyMatch(String input) {
return patterns.stream().anyMatch(p -> p.matcher(input).matches());
}

public int size() {
return patterns.size();
boolean isEmpty() {
return patterns.isEmpty();
}
}
Loading