org.gradle.configuration.DefaultImportsReader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gradle-api Show documentation
Show all versions of gradle-api Show documentation
Gradle 6.9.1 API redistribution.
/*
* 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
© 2015 - 2025 Weber Informatics LLC | Privacy Policy