Skip to content

Commit

Permalink
Add AppVeyor, begin refactor to use STs, use an actual logging framew…
Browse files Browse the repository at this point in the history
…ork,

change the default private cotr policy
  • Loading branch information
dmssargent committed Jul 30, 2018
1 parent 6ae70c2 commit a963b2c
Show file tree
Hide file tree
Showing 27 changed files with 1,544 additions and 1,013 deletions.
14 changes: 14 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: 0.2.{build}

branches:
only:
- master

build_script:
- sh: ./gradlew assemble

test_script:
- sh: ./gradlew test

before_deploy:
- sh: ./gradlew distZip
5 changes: 3 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ apply plugin: 'application'


sourceCompatibility = 1.8
mainClassName = "me.davidsargent.stubjars.Main"
mainClassName = "davidsar.gent.stubjars.Main"
repositories {
mavenCentral()
jcenter()
}

jar {
Expand All @@ -24,5 +24,6 @@ tasks.withType(AbstractArchiveTask) {

dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile 'ch.qos.logback:logback-classic:1.3.+'
compile 'org.jetbrains:annotations:15.0'
}
89 changes: 89 additions & 0 deletions src/main/java/davidsar/gent/stubjars/JarFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright 2018 David Sargent
*
* 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 davidsar.gent.stubjars;

import davidsar.gent.stubjars.components.JarClass;
import davidsar.gent.stubjars.utils.Streams;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class JarFile {
private static Map<File, JarFile> jarFiles = new HashMap<>();
private final File jar;

private JarFile(File jar) {
this.jar = jar;
}

static JarFile forFile(@NotNull File jar) {
if (jarFiles.containsKey(jar)) return jarFiles.get(jar);

JarFile jarFile = new JarFile(jar);
jarFiles.put(jar, jarFile);
return jarFile;
}

@NotNull
static ClassLoader createClassLoaderFromJars(@Nullable ClassLoader parentClassLoader, JarFile... jarFiles) {
URL[] urls = Arrays.stream(jarFiles).map(JarFile::getUrl).toArray(URL[]::new);
ClassLoader classLoader = parentClassLoader == null ? JarFile.class.getClassLoader() : parentClassLoader;
return new URLClassLoader(urls, classLoader);
}

@NotNull
private URL getUrl() {
try {
return jar.toURI().toURL();
} catch (MalformedURLException e) {
throw new RuntimeException(String.format("Could not create classloader for \"%s\"", jar.getAbsolutePath()), e);
}
}

Set<JarClass<?>> getClasses(ClassLoader loader) throws IOException {
java.util.jar.JarFile iJar = new java.util.jar.JarFile(jar);
return Streams.makeFor(iJar.entries())
.filter(jarEntry -> jarEntry.getName().endsWith(".class"))
.map(entry -> {
try {
return new JarClass<>(loader, entry.getName());
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
})
.flatMap(klazz -> Stream.concat(Stream.of(klazz), findInnerClasses(klazz)))
.collect(Collectors.toSet());
}

@NotNull
private Stream<JarClass<?>> findInnerClasses(@NotNull JarClass<?> jarClass) {
return jarClass.innerClasses().stream()
.flatMap(klazz -> {
Stream<JarClass<?>> stream = klazz.innerClasses().stream();
Stream<JarClass<?>> innerClasses = findInnerClasses(klazz);
return Stream.concat(Stream.concat(Stream.of(klazz), stream), innerClasses);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@
* License for the specific language governing permissions and limitations under the License.
*/

package me.davidsargent.stubjars;
package davidsar.gent.stubjars;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

class Main {
private static final Logger log = LoggerFactory.getLogger(Main.class);

public static void main(String... args) {
StubJars.Builder builder = StubJars.builder();
List<File> files = new ArrayList<>(args.length);
Expand All @@ -27,38 +32,34 @@ public static void main(String... args) {
continue;
}

// if (argument.contains("*")) {
// String baseFolder = argument.startsWith("*") ? new File(".").getPath() : argument.substring(0, argument.lastIndexOf(File.separatorChar) + 1);
// String regexToCheck = argument.substring(argument.lastIndexOf("/" + 1, argument.indexOf('*')));
// regexToCheck = argument.substring(0, regexToCheck.indexOf("*") + 1);
// regexToCheck = regexToCheck.replace("*", "\\.*");
// List<String> matches = new ArrayList<>();
// for (String possibleResult : Objects.requireNonNull(new File(baseFolder).list())) {
// if (possibleResult.matches(regexToCheck)) {
// matches.add(new File(Path));
// }
// }
// }

File e = new File(argument);
if (!e.exists()) System.err.println(String.format("The file \"%s\" does not exist!", argument));
if (e.isDirectory()) System.err.println(String.format("The file \"%s\" is a folder!", argument));
if (!e.canRead()) System.err.println(String.format("The file \"%s\" cannot be read!", argument));
files.add(e);
if (!e.exists()) {
log.error("The file \"{}\" does not exist!", argument);
} else if (e.isDirectory()) {
log.error("The file \"{}\" is a folder!", argument);
} else if (!e.canRead()) {
log.error("The file \"{}\" cannot be read!", argument);
} else {
files.add(e);
continue;
}

log.error("Encountered an error loading the specified JAR files");
System.exit(1);
}

builder.addJars(files.toArray(new File[] {}));
System.out.print("Loading...");
log.info("Loading the JARs to be stubbed");
StubJars build = builder.build();
System.out.println("done!");
System.out.print("Building directory tree at \"" + build.getSourceDestination().getAbsolutePath() + "\"...");
log.info("JAR load finished");
log.info("Creating the stub_src directory tree at \"{}\"", build.getSourceDestination().getAbsolutePath());
build.createDirectoryTree();
System.out.println("done!");
System.out.print("Writing source files...");
log.info("stub_src directory tree creation finished");
log.info("Starting creation of stub_src files");
build.createSourceFiles();
System.out.println("done!");

log.info("Creation of stub_src files finished");

System.out.println("\nDone! Bye!");
log.info("StubJars has finished");
}

private static void parseArg(StubJars.Builder builder, String arg) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* License for the specific language governing permissions and limitations under the License.
*/

package me.davidsargent.stubjars;
package davidsar.gent.stubjars;

import org.jetbrains.annotations.Contract;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
* License for the specific language governing permissions and limitations under the License.
*/

package me.davidsargent.stubjars;
package davidsar.gent.stubjars;

import me.davidsargent.stubjars.components.JarClass;
import me.davidsargent.stubjars.components.SecurityModifier;
import me.davidsargent.stubjars.components.writer.JavaClassWriter;
import me.davidsargent.stubjars.components.writer.Writer;
import me.davidsargent.stubjars.components.writer.WriterThread;
import davidsar.gent.stubjars.components.JarClass;
import davidsar.gent.stubjars.components.SecurityModifier;
import davidsar.gent.stubjars.components.writer.JavaClassWriter;
import davidsar.gent.stubjars.components.writer.Writer;
import davidsar.gent.stubjars.components.writer.WriterThread;
import org.jetbrains.annotations.NotNull;

import java.io.File;
Expand All @@ -43,7 +43,7 @@ public class StubJars {
private final File CLASSES_DIR = new File(BUILD_DIR, "classes");
private final File SOURCES_LIST_FILE = new File(SOURCE_DIR, "sources.list");

private StubJars(@NotNull ConcurrentMap<Class<?>, JarClass<?>> klazzes, @NotNull ClassLoader classLoader) {
private StubJars(@NotNull ConcurrentMap<Class<?>, JarClass<?>> klazzes) {
this.klazzes = klazzes;
}

Expand All @@ -53,11 +53,11 @@ private StubJars(@NotNull ConcurrentMap<Class<?>, JarClass<?>> klazzes, @NotNull
* @return a new StubJars builder
*/
@NotNull
public static Builder builder() {
static Builder builder() {
return new Builder();
}

public void createDirectoryTree() {
void createDirectoryTree() {
if (packages == null) buildPackagesList();
SOURCE_DIR.mkdirs();
createBuildDir();
Expand All @@ -75,7 +75,7 @@ private void buildPackagesList() {
}
}

public void createSourceFiles() {
void createSourceFiles() {
WriterThread writerThread = new WriterThread();
writerThread.start();
StringBuilder sourceFiles = new StringBuilder();
Expand All @@ -84,7 +84,7 @@ public void createSourceFiles() {
continue;
// this breaks compilation (currently)
// todo: make unneeded
if (e.getKlazz().getName().equals("java.lang.Enum"))
if (e.getKlazz().getName().equals(Enum.class.getName()))
continue;
File file = new File(SOURCE_DIR, e.getKlazz().getName().replace('.', File.separatorChar) + ".java");
JavaClassWriter writer = new JavaClassWriter(file, e, writerThread);
Expand Down Expand Up @@ -114,15 +114,14 @@ private void createBuildDir() {
CLASSES_DIR.mkdirs();
}

@NotNull
public File getSourceDestination() {
@NotNull File getSourceDestination() {
return SOURCE_DIR;
}

/**
* Creates new {@link StubJars} instances
*/
public static class Builder {
static class Builder {
private final List<JarFile> jars;
private final List<JarFile> classpathJars;

Expand All @@ -136,7 +135,7 @@ private Builder() {
*
* @param jar a {@link File} representing a JAR file
*/
public void addJar(@NotNull File jar) {
void addJar(@NotNull File jar) {
jars.add(JarFile.forFile(jar));
}

Expand All @@ -145,7 +144,7 @@ public void addJar(@NotNull File jar) {
*
* @param jar a {@link File} representing a JAR file
*/
public void addClasspathJar(@NotNull File jar) {
void addClasspathJar(@NotNull File jar) {
classpathJars.add(JarFile.forFile(jar));
}

Expand All @@ -154,7 +153,7 @@ public void addClasspathJar(@NotNull File jar) {
*
* @param jars the {@link File}s representing a JAR files
*/
public void addJars(@NotNull File... jars) {
void addJars(@NotNull File... jars) {
for (File jar : jars) {
addJar(jar);
}
Expand All @@ -165,16 +164,15 @@ public void addJars(@NotNull File... jars) {
*
* @return a new {@link StubJars} instance
*/
@NotNull
public StubJars build() {
ClassLoader cpClassLoader = JarFile.createClassLoaderFromJars(null, classpathJars.toArray(new JarFile[classpathJars.size()]));
ClassLoader classLoader = JarFile.createClassLoaderFromJars(cpClassLoader, jars.toArray(new JarFile[jars.size()]));
@NotNull StubJars build() {
ClassLoader cpClassLoader = JarFile.createClassLoaderFromJars(null, classpathJars.toArray(new JarFile[0]));
ClassLoader classLoader = JarFile.createClassLoaderFromJars(cpClassLoader, jars.toArray(new JarFile[0]));
ConcurrentMap<Class<?>, JarClass<?>> klazzes = new ConcurrentHashMap<>();
for (JarFile jar : jars) {
final Set<JarClass<?>> classes;
try {
classes = jar.getClasses(classLoader);
} catch (IOException | ClassNotFoundException e) {
} catch (IOException e) {
throw new RuntimeException("Cannot load jar!", e);
}

Expand All @@ -184,7 +182,7 @@ public StubJars build() {
}

JarClass.loadClassToJarClassMap(klazzes);
return new StubJars(klazzes, classLoader);
return new StubJars(klazzes);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* License for the specific language governing permissions and limitations under the License.
*/

package me.davidsargent.stubjars;
package davidsar.gent.stubjars;

import org.jetbrains.annotations.NotNull;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
* License for the specific language governing permissions and limitations under the License.
*/

package me.davidsargent.stubjars.components;
package davidsar.gent.stubjars.components;

public interface CompileableString {
String compileToString();
interface CompileableExpression {
Expression compileToExpression();
}
Loading

0 comments on commit a963b2c

Please # to comment.