All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.spricom.dessert.partitioning.ClazzPredicates Maven / Gradle / Ivy

The newest version!
package de.spricom.dessert.partitioning;

/*-
 * #%L
 * Dessert Dependency Assertion Library for Java
 * %%
 * Copyright (C) 2017 - 2023 Hans Jörg Heßmann
 * %%
 * 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
 * 
 *      http://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.
 * #L%
 */

import de.spricom.dessert.classfile.ClassFile;
import de.spricom.dessert.slicing.Clazz;
import de.spricom.dessert.slicing.Slice;
import de.spricom.dessert.util.AnnotationPattern;
import de.spricom.dessert.util.Predicate;
import de.spricom.dessert.util.Predicates;

public final class ClazzPredicates {

    /**
     * This is a catch-all predicate that can be used to collect
     * anything that does not match another predicate.
     */
    public static final Predicate EACH = Predicates.any();

    public static final Predicate PUBLIC = new Predicate() {
        @Override
        public boolean test(Clazz clazz) {
            return clazz.getClassFile().isPublic();
        }
    };

    public static final Predicate FINAL = new Predicate() {
        @Override
        public boolean test(Clazz clazz) {
            return clazz.getClassFile().isFinal();
        }
    };

    public static final Predicate SUPER = new Predicate() {
        @Override
        public boolean test(Clazz clazz) {
            return clazz.getClassFile().isSuper();
        }
    };

    public static final Predicate INTERFACE = new Predicate() {
        @Override
        public boolean test(Clazz clazz) {
            return clazz.getClassFile().isInterface();
        }
    };

    public static final Predicate ABSTRACT = new Predicate() {
        @Override
        public boolean test(Clazz clazz) {
            return clazz.getClassFile().isAbstract();
        }
    };

    public static final Predicate SYNTHETIC = new Predicate() {
        @Override
        public boolean test(Clazz clazz) {
            return clazz.getClassFile().isSynthetic();
        }
    };

    public static final Predicate ANNOTATION = new Predicate() {
        @Override
        public boolean test(Clazz clazz) {
            return clazz.getClassFile().isAnnotation();
        }
    };

    public static final Predicate ENUM = new Predicate() {
        @Override
        public boolean test(Clazz clazz) {
            return clazz.getClassFile().isEnum();
        }
    };

    public static final Predicate DEPRECATED = new Predicate() {
        @Override
        public boolean test(Clazz clazz) {
            return clazz.getClassFile().isDeprecated();
        }
    };

    public static final Predicate INNER_TYPE = new Predicate() {
        @Override
        public boolean test(Clazz clazz) {
            if (clazz.getClassFile() != null) {
                return clazz.getClassFile().isInnerClass();
            }
            // Not correct, if classname contains $, but sufficient for the rare case where there is no ClassFile
            return clazz.getName().lastIndexOf('$') != -1;
        }
    };

    public static Predicate matchesName(final String regex) {
        return new Predicate() {
            @Override
            public boolean test(Clazz clazz) {
                return clazz.getName().matches(regex);
            }
        };
    }

    public static Predicate matchesSimpleName(final String regex) {
        return new Predicate() {
            @Override
            public boolean test(Clazz clazz) {
                return clazz.getSimpleName().matches(regex);
            }
        };
    }

    public static Predicate implementsInterface(final String interfaceName) {
        return new Predicate() {
            @Override
            public boolean test(Clazz clazz) {
                for (String name : clazz.getClassFile().getInterfaces()) {
                    if (name.equals(interfaceName)) {
                        return true;
                    }
                }
                return false;
            }
        };
    }

    public static Predicate hostedBy(final Clazz host) {
        return new Predicate() {
            private final Slice nest = host.getNest();

            @Override
            public boolean test(Clazz clazz) {
                return nest.contains(clazz);
            }
        };
    }

    public static Predicate matchesAnnotation(final AnnotationPattern annotationPattern) {
        return matchesClassFile(new AnnotationMatcher(annotationPattern));
    }

    public static Predicate matchesClassFile(final Predicate classFilePredicate) {
        return new Predicate() {
            @Override
            public boolean test(Clazz clazz) {
                return classFilePredicate.test(clazz.getClassFile());
            }
        };
    }

    public static Predicate matches(final Predicate> classPredicate) {
        return new Predicate() {
            @Override
            public boolean test(Clazz clazz) {
                return classPredicate.test(clazz.getClassImpl());
            }
        };
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy