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

org.apache.commons.weaver.Inflater Maven / Gradle / Ivy

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you 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.
 */
package org.apache.commons.weaver;

import java.lang.annotation.Annotation;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.weaver.model.ScanResult;
import org.apache.commons.weaver.model.WeavableClass;
import org.apache.commons.weaver.model.WeavableConstructor;
import org.apache.commons.weaver.model.WeavableConstructorParameter;
import org.apache.commons.weaver.model.WeavableField;
import org.apache.commons.weaver.model.WeavableMethod;
import org.apache.commons.weaver.model.WeavableMethodParameter;
import org.apache.commons.weaver.model.WeavablePackage;
import org.apache.xbean.finder.AnnotationFinder.ClassInfo;
import org.apache.xbean.finder.AnnotationFinder.FieldInfo;
import org.apache.xbean.finder.AnnotationFinder.Info;
import org.apache.xbean.finder.AnnotationFinder.MethodInfo;
import org.apache.xbean.finder.AnnotationFinder.PackageInfo;
import org.apache.xbean.finder.AnnotationFinder.ParameterInfo;
import org.apache.xbean.finder.Parameter;

/**
 * Adds all classfile annotations to a ScanResult.
 */
class Inflater {
    private class InfoMatcher {
        final Class type;

        InfoMatcher(final Class type) {
            super();
            this.type = type;
        }

        boolean test(final Info info) {
            return type.isInstance(info);
        }

    }

    private class MethodMatcher extends InfoMatcher {
        final boolean isCtor;

        MethodMatcher(final boolean isCtor) {
            super(MethodInfo.class);
            this.isCtor = isCtor;
        }

        @Override
        boolean test(final Info info) {
            return super.test(info) && ((MethodInfo) info).isConstructor() == isCtor;
        }
    }

    private class ParameterMatcher extends InfoMatcher {
        final boolean isCtor;

        ParameterMatcher(final boolean isCtor) {
            super(ParameterInfo.class);
            this.isCtor = isCtor;
        }

        @Override
        boolean test(final Info info) {
            return super.test(info) && ((ParameterInfo) info).getDeclaringMethod().isConstructor() == isCtor;
        }
    }

    final Map> packageAnnotations;
    final Map> classAnnotations;
    final Map> fieldAnnotations;
    final Map> ctorAnnotations;
    final Map> methodAnnotations;
    final Map> ctorParameterAnnotations;
    final Map> methodParameterAnnotations;

    Inflater(final Map> annotationMap) {
        super();

        this.packageAnnotations = subMap(annotationMap, new InfoMatcher(PackageInfo.class));
        this.classAnnotations = subMap(annotationMap, new InfoMatcher(ClassInfo.class));
        this.fieldAnnotations = subMap(annotationMap, new InfoMatcher(FieldInfo.class));
        this.ctorAnnotations = subMap(annotationMap, new MethodMatcher(true));
        this.methodAnnotations = subMap(annotationMap, new MethodMatcher(false));
        this.ctorParameterAnnotations = subMap(annotationMap, new ParameterMatcher(true));
        this.methodParameterAnnotations = subMap(annotationMap, new ParameterMatcher(false));
    }

    static  Map> subMap(final Map> source,
        final InfoMatcher matcher) {
        final HashMap> result = new HashMap>();
        for (final Map.Entry> entry : source.entrySet()) {
            if (matcher.test(entry.getKey())) {
                @SuppressWarnings("unchecked")
                final I key = (I) entry.getKey();
                result.put(key, entry.getValue());
            }
        }
        return result;
    }

    ScanResult inflate(final ScanResult scanResult) {
        for (final WeavablePackage pkg : scanResult.getPackages()) {
            for (final Map.Entry> entry : packageAnnotations.entrySet()) {
                if (entry.getKey().getName().equals(pkg.getTarget().getName())) {
                    pkg.addAnnotations(entry.getValue());
                }
            }
            for (final WeavableClass cls : pkg.getClasses()) {
                for (final Map.Entry> entry : classAnnotations.entrySet()) {
                    if (entry.getKey().getName().equals(cls.getTarget().getName())) {
                        cls.addAnnotations(entry.getValue());
                    }
                }
                for (final WeavableField fld : cls.getFields()) {
                    for (final Map.Entry> entry : fieldAnnotations.entrySet()) {
                        try {
                            if (entry.getKey().get().equals(fld.getTarget())) {
                                fld.addAnnotations(entry.getValue());
                            }
                        } catch (final ClassNotFoundException cnfe) {
                            continue;
                        }
                    }
                }
                for (final WeavableConstructor ctor : cls.getConstructors()) {
                    for (final Map.Entry> entry : ctorAnnotations.entrySet()) {
                        try {
                            if (entry.getKey().get().equals(ctor.getTarget())) {
                                ctor.addAnnotations(entry.getValue());
                            }
                        } catch (final ClassNotFoundException cnfe) {
                            continue;
                        }
                    }
                    for (final WeavableConstructorParameter param : ctor.getParameters()) {
                        for (final Map.Entry> entry : ctorParameterAnnotations
                            .entrySet()) {
                            try {
                                final Parameter parameter = entry.getKey().get();
                                if (parameter.getDeclaringExecutable().equals(ctor.getTarget())
                                    && param.getTarget().intValue() == parameter.getIndex()) {
                                    param.addAnnotations(entry.getValue());
                                }
                            } catch (final ClassNotFoundException cnfe) {
                                continue;
                            }
                        }
                    }
                }
                for (final WeavableMethod methd : cls.getMethods()) {
                    for (final Map.Entry> entry : methodAnnotations.entrySet()) {
                        try {
                            if (entry.getKey().get().equals(methd.getTarget())) {
                                methd.addAnnotations(entry.getValue());
                            }
                        } catch (final ClassNotFoundException cnfe) {
                            continue;
                        }
                    }
                    for (final WeavableMethodParameter param : methd.getParameters()) {
                        for (final Map.Entry> entry : methodParameterAnnotations
                            .entrySet()) {
                            try {
                                final Parameter parameter = entry.getKey().get();
                                if (parameter.getDeclaringExecutable().equals(methd.getTarget())
                                    && param.getTarget().intValue() == parameter.getIndex()) {
                                    param.addAnnotations(entry.getValue());
                                }
                            } catch (final ClassNotFoundException cnfe) {
                                continue;
                            }
                        }
                    }
                }
            }
        }
        return scanResult;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy