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

org.gradle.configuration.DefaultImportsReader Maven / Gradle / Ivy

There is a newer version: 8.11.1
Show newest version
/*
 * Copyright 2009 the original author or authors.
 *
 * 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 org.gradle.configuration;

import com.google.common.base.Charsets;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableMap;
import com.google.common.io.LineProcessor;
import com.google.common.io.Resources;
import org.apache.commons.lang.StringUtils;
import org.gradle.api.UncheckedIOException;

import java.io.IOException;
import java.net.URL;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class DefaultImportsReader implements ImportsReader {

    public static final String RESOURCE = "/default-imports.txt";
    private static final String MAPPING_RESOURCE = "/api-mapping.txt";
    private final String[] importPackages;
    private final Map> simpleNameToFQCN;

    public DefaultImportsReader() {
        this(DefaultImportsReader.class.getResource(RESOURCE));
    }

    public DefaultImportsReader(URL url) {
        try {
            this.importPackages = generateImportPackages(url);
            this.simpleNameToFQCN = generateSimpleNameToFQCN();
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }

    @Override
    public String[] getImportPackages() {
        return importPackages;
    }

    @Override
    public Map> getSimpleNameToFullClassNamesMapping() {
        return simpleNameToFQCN;
    }

    /**
     * @implNote Logic is duplicated in {@link gradlebuild.integrationtests.action.AnnotationGeneratorWorkAction}.
     * Please keep this code in sync.
     */
    private static String[] generateImportPackages(URL url) throws IOException {
        if (url == null) {
            throw new IllegalStateException("Could not load default imports resource: " + RESOURCE);
        }
        return Resources.asCharSource(url, Charsets.UTF_8).readLines(new LineProcessor() {
            private final List packages = new LinkedList<>();

            @Override
            public boolean processLine(@SuppressWarnings("NullableProblems") String line) throws IOException {
                packages.add(line.substring(7, line.length() - 2));
                return true;
            }

            @Override
            public String[] getResult() {
                return packages.toArray(new String[packages.size()]);
            }
        });
    }

    private static Map> generateSimpleNameToFQCN() throws IOException {
        URL url = DefaultImportsReader.class.getResource(MAPPING_RESOURCE);
        if (url == null) {
            throw new IllegalStateException("Could not load default imports resource: " + MAPPING_RESOURCE);
        }
        return Resources.asCharSource(url, Charsets.UTF_8).readLines(new LineProcessor>>() {
            private final ImmutableMap.Builder> builder = ImmutableMap.builder();

            @Override
            public boolean processLine(String line) throws IOException {
                boolean process = !StringUtils.isEmpty(line);
                if (process) {
                    String[] split = line.split(":");
                    if (split.length == 2) {
                        String simpleName = split[0];
                        List fqcns = Splitter.on(';').omitEmptyStrings().splitToList(split[1]);
                        builder.put(simpleName, fqcns);
                    } else {
                        process = false;
                    }
                }
                return process;
            }

            @Override
            public Map> getResult() {
                return builder.build();
            }
        });
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy