org.gradle.configuration.DefaultImportsReader.java Source code

Java tutorial

Introduction

Here is the source code for org.gradle.configuration.DefaultImportsReader.java

Source

/*
 * 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.collect.Lists;
import com.google.common.io.LineProcessor;
import com.google.common.io.Resources;
import org.gradle.api.UncheckedIOException;

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

public class DefaultImportsReader implements ImportsReader {

    private static final String RESOURCE = "/default-imports.txt";
    private final String[] importPackages;

    public DefaultImportsReader() {
        try {
            URL url = getClass().getResource(RESOURCE);
            if (url == null) {
                throw new IllegalStateException("Could not load default imports resource: " + RESOURCE);
            }
            this.importPackages = Resources.asCharSource(url, Charsets.UTF_8)
                    .readLines(new LineProcessor<String[]>() {
                        private final List<String> packages = Lists.newLinkedList();

                        @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()]);
                        }
                    });
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }

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

}