Add some useful implementations of ClassHolderSource and ResourceReader

This commit is contained in:
Alexey Andreev 2017-11-16 18:30:25 +03:00
parent 44e6feef0c
commit 3f487f8a7d
4 changed files with 137 additions and 0 deletions

View File

@ -62,6 +62,13 @@ public class ClasspathResourceMapper implements Mapper<String, ClassHolder>, Cla
this.classLoader = classLoader;
}
public ClasspathResourceMapper(Properties properties, Mapper<String, ClassHolder> innerMapper) {
this.innerMapper = innerMapper;
Map<String, Transformation> transformationMap = new HashMap<>();
loadProperties(properties, transformationMap);
transformations.addAll(transformationMap.values());
}
private void loadProperties(Properties properties, Map<String, Transformation> cache) {
for (String propertyName : properties.stringPropertyNames()) {
if (propertyName.startsWith(PACKAGE_PREFIX)) {

View File

@ -0,0 +1,45 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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.teavm.parsing;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.teavm.model.ClassHolder;
import org.teavm.model.ClassHolderSource;
public class CompositeClassHolderSource implements ClassHolderSource {
private List<ClassHolderSource> innerSources;
private Map<String, ClassHolder> cache = new HashMap<>();
public CompositeClassHolderSource(List<ClassHolderSource> innerSources) {
this.innerSources = new ArrayList<>(innerSources);
}
@Override
public ClassHolder get(String name) {
return cache.computeIfAbsent(name, n -> {
for (ClassHolderSource innerSource : innerSources) {
ClassHolder cls = innerSource.get(n);
if (cls != null) {
return cls;
}
}
return null;
});
}
}

View File

@ -0,0 +1,46 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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.teavm.parsing;
import java.io.File;
import java.util.Properties;
import org.teavm.model.ClassHolder;
import org.teavm.model.ClassHolderSource;
import org.teavm.parsing.resource.DirectoryResourceReader;
import org.teavm.parsing.resource.MapperClassHolderSource;
import org.teavm.parsing.resource.ResourceClassHolderMapper;
public class DirectoryClasspathClassHolderSource implements ClassHolderSource {
private MapperClassHolderSource innerClassSource;
private ClasspathResourceMapper classPathMapper;
public DirectoryClasspathClassHolderSource(File baseDir, Properties properties) {
DirectoryResourceReader reader = new DirectoryResourceReader(baseDir);
ResourceClassHolderMapper rawMapper = new ResourceClassHolderMapper(reader);
classPathMapper = new ClasspathResourceMapper(properties, rawMapper);
innerClassSource = new MapperClassHolderSource(classPathMapper);
}
public DirectoryClasspathClassHolderSource(File baseDir) {
this(baseDir, new Properties());
}
@Override
public ClassHolder get(String name) {
return innerClassSource.get(name);
}
}

View File

@ -0,0 +1,39 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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.teavm.parsing.resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class DirectoryResourceReader implements ResourceReader {
private File baseDir;
public DirectoryResourceReader(File baseDir) {
this.baseDir = baseDir;
}
@Override
public boolean hasResource(String name) {
return new File(baseDir, name).isFile();
}
@Override
public InputStream openResource(String name) throws IOException {
return new FileInputStream(new File(baseDir, name));
}
}