net.sourceforge.jaulp.io.annotations.ImportResourcesUtils.java Source code

Java tutorial

Introduction

Here is the source code for net.sourceforge.jaulp.io.annotations.ImportResourcesUtils.java

Source

/**
 * Copyright (C) 2007 Asterios Raptis
 *
 * 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.
 */
package net.sourceforge.jaulp.io.annotations;

import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

import net.sourceforge.jaulp.lang.AnnotationUtils;

import org.apache.commons.lang.ArrayUtils;

/**
 * The Class ImportResourcesUtils contains helper methods for ImportResource objects.
 */
public class ImportResourcesUtils {

    /**
     * Gets a map with ImportResource objects and the corresponding to the found class from the
     * given package Name. The search is made recursive. The key from an entry of the map is the
     * class where the ImportResource objects found and the value is an Array of the ImportResource
     * objects that contains in the class.
     * 
     * @param packageName
     *            the package name
     * @return the import resources
     * @throws ClassNotFoundException
     *             the class not found exception
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     */
    public static Map<Class<?>, ImportResource[]> getImportResources(String packageName)
            throws ClassNotFoundException, IOException {
        final Map<Class<?>, ImportResource[]> resourcesMap = new LinkedHashMap<>();

        final Class<ImportResources> importResourcesClass = ImportResources.class;
        final Class<ImportResource> importResourceClass = ImportResource.class;
        final Set<Class<?>> importResourcesClasses = AnnotationUtils.getAllAnnotatedClasses(packageName,
                importResourcesClass);
        final Set<Class<?>> importResourceClasses = AnnotationUtils.getAllAnnotatedClasses(packageName,
                importResourceClass);
        importResourcesClasses.addAll(importResourceClasses);
        for (Class<?> annotatedClass : importResourcesClasses) {
            final ImportResources importResources = annotatedClass.getAnnotation(ImportResources.class);
            ImportResource[] importResourcesArray = null;
            ImportResource[] importResourceArray = null;
            if (importResources != null) {
                importResourcesArray = importResources.resources();
            }

            final ImportResource importResource = annotatedClass.getAnnotation(ImportResource.class);
            if (importResource != null) {
                importResourceArray = new ImportResource[1];
                importResourceArray[0] = importResource;
            }
            ImportResource[] array = (ImportResource[]) ArrayUtils.addAll(importResourceArray,
                    importResourcesArray);
            Arrays.sort(array, new ImportResourceComparator());
            resourcesMap.put(annotatedClass, array);

        }
        return resourcesMap;
    }
}