-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
352 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package io.toolisticon.cute; | ||
|
||
public interface CuteClassLoader { | ||
|
||
<TYPE> Class<TYPE> getClass(String binaryClassName) throws ClassNotFoundException; | ||
|
||
} |
86 changes: 86 additions & 0 deletions
86
cute/src/main/java/io/toolisticon/cute/CuteClassLoaderImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package io.toolisticon.cute; | ||
|
||
import javax.tools.JavaFileObject; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.Collectors; | ||
|
||
class CuteClassLoaderImpl extends ClassLoader implements CuteClassLoader { | ||
|
||
final CompileTestFileManager compileTestFileManager; | ||
|
||
final Map<String, Class<?>> classMap = new HashMap<>(); | ||
|
||
public CuteClassLoaderImpl(CompileTestFileManager compileTestFileManager) { | ||
this.compileTestFileManager = compileTestFileManager; | ||
|
||
// get classes | ||
List<JavaFileObject> javaFileObjectList = compileTestFileManager.getGeneratedJavaFileObjects().stream() | ||
.filter(e -> e.getKind() == JavaFileObject.Kind.CLASS) | ||
.collect(Collectors.toList()); | ||
|
||
for (JavaFileObject javaFileObject : javaFileObjectList) { | ||
try { | ||
byte[] byteArray = readAllBytes(javaFileObject.openInputStream()); | ||
String className = convertJavaFileObjectNameToBinaryClassName(javaFileObject); | ||
classMap.put(className, defineClass(className, byteArray, 0, byteArray.length)); | ||
} catch (IOException e) { | ||
// Ignore for the moment | ||
} | ||
} | ||
|
||
} | ||
|
||
private static byte[] readAllBytes(InputStream inputStream) throws IOException { | ||
final int bufLen = 4 * 0x400; // 4KB | ||
byte[] buf = new byte[bufLen]; | ||
int readLen; | ||
IOException exception = null; | ||
|
||
try { | ||
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { | ||
while ((readLen = inputStream.read(buf, 0, bufLen)) != -1) | ||
outputStream.write(buf, 0, readLen); | ||
|
||
return outputStream.toByteArray(); | ||
} | ||
} catch (IOException e) { | ||
exception = e; | ||
throw e; | ||
} finally { | ||
if (exception == null) inputStream.close(); | ||
else try { | ||
inputStream.close(); | ||
} catch (IOException e) { | ||
exception.addSuppressed(e); | ||
} | ||
} | ||
} | ||
|
||
static String convertJavaFileObjectNameToBinaryClassName(JavaFileObject javaFileObject) { | ||
Pattern pattern = Pattern.compile("^[/](.*)[.]class$"); | ||
Matcher matcher = pattern.matcher(javaFileObject.getName()); | ||
if (matcher.matches()) { | ||
return matcher.group(1).replaceAll("[/]","."); | ||
} | ||
throw new IllegalStateException("Got invalid name : " + javaFileObject.getName()); | ||
} | ||
|
||
@Override | ||
public <TYPE> Class<TYPE> getClass(String binaryClassName) throws ClassNotFoundException { | ||
|
||
|
||
Class<TYPE> clazz = (Class<TYPE>) this.classMap.get(binaryClassName); | ||
|
||
if(clazz == null){ | ||
throw new ClassNotFoundException(binaryClassName); | ||
} | ||
return clazz; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
cute/src/main/java/io/toolisticon/cute/GeneratedClassesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package io.toolisticon.cute; | ||
|
||
public interface GeneratedClassesTest { | ||
|
||
void doTests(CuteClassLoader cuteClassLoader) throws Exception; | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
cute/src/main/java/io/toolisticon/cute/GeneratedClassesTestForSpecificClass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package io.toolisticon.cute; | ||
|
||
public interface GeneratedClassesTestForSpecificClass { | ||
|
||
void doTests(Class<?> clazz, CuteClassLoader cuteClassLoader) throws Exception; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
cute/src/test/java/io/toolisticon/cute/testcases/SimpleTestInterface.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package io.toolisticon.cute.testcases; | ||
|
||
public interface SimpleTestInterface { | ||
String saySomething(); | ||
} |
Oops, something went wrong.