Skip to content

Commit dc1eda0

Browse files
committed
0.1.0 - Initial class loading/manipulation code with examples console apps
0 parents  commit dc1eda0

File tree

161 files changed

+12084
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

161 files changed

+12084
-0
lines changed

Diff for: CHANGELOG.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Change Log
2+
All notable changes to this project will be documented in this file.
3+
This project does its best to adhere to [Semantic Versioning](http://semver.org/).
4+
5+
6+
--------
7+
###[0.1.0](N/A) - 2017-01-11
8+
#### Added
9+
* Initial versioning of existing code, including classfile loading/parsing, dynamic classfile modification, and runtime class loading.

Diff for: LICENSE

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2017 TeamworkGuy2
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+

Diff for: README.md

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
ClassLoading
2+
==========
3+
version: 0.1.0
4+
5+
Java class file parsing and manipulation library.
6+
Reference: [Java Virtual Machine Spec](http://docs.oracle.com/javase/specs/jvms/se8/html/index.html)
7+
8+
###See `twg2.jbcm.main/`
9+
for example code and interactive command line tools.
10+
11+
####`twg2.jbcm.classFormat`
12+
Contains implementation of the [class file format](http://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html)
13+
with related attributes and constant pool types.
14+
15+
####`twg2.jbcm` and `twg2.jbcm.modify`
16+
Interfaces and utilities for searching and modifying class files.
17+
18+
####`twg2.jbcm.opcode`
19+
Partial implementation of [Java instruction set opcodes](http://docs.oracle.com/javase/specs/jvms/se8/html/jvms-6.html#jvms-6.5).
20+
21+
####`twg2.jbcm.dynamicModification`, `twg2.jbcm.parserExamples`
22+
Classes used by the example and test packages.
23+
24+
####`twg2.jbcm.runtimeLoading`
25+
Runtime class loading.
26+
27+
####`twg2.jbcm.main`
28+
Example console apps.

Diff for: bin/class_loading.jar

246 KB
Binary file not shown.

Diff for: examples/ExampleMain.class

1.5 KB
Binary file not shown.

Diff for: examples/ExampleMain.java

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package examples;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* @author TeamworkGuy2
8+
* @since 2014-4-23
9+
*/
10+
public class ExampleMain implements Runnable {
11+
private int num;
12+
13+
14+
public ExampleMain() {
15+
this.num = 20;
16+
}
17+
18+
19+
@Override
20+
public void run() {
21+
List<Integer> list = new ArrayList<Integer>();
22+
series(num, 0, list);
23+
System.out.print(list);
24+
}
25+
26+
27+
private void series(int count, int initial, List<Integer> values) {
28+
int vn1 = values.size() > 0 ? values.get(values.size()-1) : 1;
29+
int vn2 = values.size() > 1 ? values.get(values.size()-2) : 0;
30+
values.add(vn1 + vn2);
31+
if(count > 0) {
32+
series(count-1, initial, values);
33+
}
34+
}
35+
36+
37+
public static void main(String[] args) {
38+
ExampleMain example = new ExampleMain();
39+
example.run();
40+
}
41+
42+
}

Diff for: package-lib.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"version" : "0.1.0",
3+
"name" : "class-loading",
4+
"description" : "Java class file parsing and manipulation",
5+
"homepage" : "https://github.com/TeamworkGuy2/ClassLoading",
6+
"license" : "MIT",
7+
"main" : "./bin/class_loading.jar",
8+
"dependencies" : {
9+
"ecj-4.4M2" : "*"
10+
}
11+
}

Diff for: res/build.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Run the following command to compile java files in the source\classLoading folder, if additional folders are added, add them to this command before the -d option.
2+
This folder is for various java files used to test dynamic reloading that cannot be part of the eclipse project's source/build path.
3+
Navigate to the classpath folder, then run the following command:
4+
5+
"C:\Program Files\Java\jdk1.8.0_112\bin\javac" -sourcepath source -classpath C:\Users\TeamworkGuy2\Documents\Java\Projects\ClassLoading\res source\classLoading\*.java source\classLoading\base\*.java source\classLoading\load\*.java -d destination

Diff for: res/destination/classLoading/ClassLoaded.class

2.15 KB
Binary file not shown.

Diff for: res/destination/classLoading/Reload.class

971 Bytes
Binary file not shown.

Diff for: res/destination/classLoading/RunnableThing.class

1.62 KB
Binary file not shown.

Diff for: res/destination/classLoading/Settings.class

348 Bytes
Binary file not shown.

Diff for: res/destination/classLoading/SubReload.class

863 Bytes
Binary file not shown.

Diff for: res/destination/classLoading/TestThing.class

849 Bytes
Binary file not shown.

Diff for: res/destination/classLoading/base/Test.class

1.22 KB
Binary file not shown.
158 Bytes
Binary file not shown.
159 Bytes
Binary file not shown.

Diff for: res/second_runtime/classLoading/ClassLoaded.class

2.15 KB
Binary file not shown.

Diff for: res/second_runtime/classLoading/Reload.class

971 Bytes
Binary file not shown.

Diff for: res/second_runtime/classLoading/RunnableThing.class

1.62 KB
Binary file not shown.

Diff for: res/second_runtime/classLoading/Settings.class

348 Bytes
Binary file not shown.

Diff for: res/second_runtime/classLoading/SubReload.class

863 Bytes
Binary file not shown.

Diff for: res/second_runtime/classLoading/TestThing.class

849 Bytes
Binary file not shown.

Diff for: res/second_runtime/classLoading/base/Test.class

1.22 KB
Binary file not shown.
158 Bytes
Binary file not shown.
159 Bytes
Binary file not shown.

Diff for: res/source/classLoading/ClassLoaded.java

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package classLoading;
2+
3+
import java.util.ArrayList;
4+
5+
import classLoading.base.WorkerInterface;
6+
7+
public class ClassLoaded implements WorkerInterface {
8+
private int id;
9+
private ArrayList<Thread> threads = new ArrayList<Thread>();
10+
private ArrayList<RunnableThing> objs = new ArrayList<RunnableThing>();
11+
12+
@Override
13+
public void startThreads(int threadCount) {
14+
Thread newT = null;
15+
RunnableThing thing = null;
16+
int sleep = 10;
17+
for(int i = 0; i < threadCount; i++) {
18+
thing = new RunnableThing();
19+
newT = new Thread(thing);
20+
newT.setName("runnable test " + id);
21+
threads.add(newT);
22+
objs.add(thing);
23+
newT.start();
24+
id++;
25+
}
26+
try {
27+
Thread.sleep(sleep);
28+
} catch (InterruptedException e) {
29+
e.printStackTrace();
30+
}
31+
for(int i = 0; i < threadCount; i++) {
32+
thing = objs.get(i);
33+
thing.event(true, false);
34+
synchronized(thing) {
35+
thing.notify();
36+
}
37+
}
38+
try {
39+
Thread.sleep(sleep);
40+
} catch (InterruptedException e) {
41+
e.printStackTrace();
42+
}
43+
for(int i = 0; i < threadCount; i++) {
44+
thing = objs.get(i);
45+
thing.event(true, true);
46+
synchronized(thing) {
47+
thing.notify();
48+
}
49+
}
50+
try {
51+
Thread.sleep(sleep);
52+
} catch (InterruptedException e) {
53+
e.printStackTrace();
54+
}
55+
for(int i = 0; i < threadCount; i++) {
56+
if(threads.get(i).isAlive()) {
57+
System.out.println("Thread " + threads.get(i) + " (" + i + ") still alive");
58+
}
59+
}
60+
threads.clear();
61+
objs.clear();
62+
id = 0;
63+
}
64+
65+
}

Diff for: res/source/classLoading/Reload.java

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package classLoading;
2+
3+
import classLoading.SubReload;
4+
5+
/** Reload test that is being used to test whether two version of the same named class can be loaded during
6+
* a program's runtime.
7+
* @author TeamworkGuy2
8+
* @since 2013-7-20
9+
*/
10+
public class Reload implements Runnable {
11+
12+
public void callTest() {
13+
SubReload r = new SubReload();
14+
System.out.println("Reload.callTest(" + Settings.version + ") call: " + r.toString());
15+
}
16+
17+
18+
@Override
19+
public void run() {
20+
SubReload r = new SubReload();
21+
System.out.println("Reload.run(" + Settings.version + ") call: " + r.toString());
22+
}
23+
24+
25+
public static final void main(String[] args) {
26+
Reload a = new Reload();
27+
a.callTest();
28+
}
29+
30+
}

Diff for: res/source/classLoading/RunnableThing.java

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package classLoading;
2+
3+
import classLoading.base.TestInterface;
4+
5+
public class RunnableThing implements TestInterface, Runnable {
6+
TestThing thing = new TestThing();
7+
int variable;
8+
boolean printInfo;
9+
boolean doExit;
10+
11+
@Override
12+
public void run() {
13+
variable++;
14+
thing = new TestThing();
15+
thing.printContextInfo();
16+
synchronized(this) {
17+
while(!doExit) {
18+
try {
19+
this.wait();
20+
} catch (InterruptedException e) {
21+
e.printStackTrace();
22+
}
23+
if(printInfo) {
24+
printContextInfo();
25+
}
26+
}
27+
}
28+
}
29+
30+
31+
public void event(boolean printInfo, boolean exit) {
32+
synchronized(this) {
33+
this.printInfo = printInfo;
34+
this.doExit = exit;
35+
}
36+
}
37+
38+
39+
@Override
40+
public void printContextInfo() {
41+
System.out.println("Thread: " + Thread.currentThread() + ", classloader: " + Thread.currentThread().getContextClassLoader());
42+
synchronized(this) {
43+
this.printInfo = false;
44+
}
45+
}
46+
47+
}

Diff for: res/source/classLoading/Settings.java

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package classLoading;
2+
3+
/** Settings to use for the various dynamically loaded classes
4+
* @author TeamworkGuy2
5+
* @since 2013-7-23
6+
*/
7+
public class Settings {
8+
public static String version = "vReload";
9+
10+
}

Diff for: res/source/classLoading/SubReload.java

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package classLoading;
2+
3+
/** SubReload test that is being used to test class calling from multiple dynamically loaded calling classes.
4+
* @author TeamworkGuy2
5+
* @since 2013-7-20
6+
*/
7+
public class SubReload {
8+
9+
public void callTest() {
10+
System.out.println("SubReload.callTest(" + Settings.version + ")");
11+
}
12+
13+
14+
public String toString() {
15+
return "SubReload.toString(" + Settings.version + ")";
16+
}
17+
18+
19+
public static final void main(String[] args) {
20+
SubReload a = new SubReload();
21+
a.callTest();
22+
}
23+
24+
}

Diff for: res/source/classLoading/TestThing.java

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package classLoading;
2+
3+
import classLoading.base.TestInterface;
4+
5+
public class TestThing implements TestInterface {
6+
7+
@Override
8+
public void printContextInfo() {
9+
System.out.println("Test Thing: " + this + ", " + Thread.currentThread() + ", " + Thread.currentThread().getContextClassLoader());
10+
}
11+
}

Diff for: res/source/classLoading/base/Test.java

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package classLoading.base;
2+
3+
/** Test of classloading and dynamically loading classes
4+
* @author TeamworkGuy2
5+
* @since 2013-7-12
6+
*/
7+
public class Test {
8+
9+
/* Careful this class relies on class files in a folder called "load" inside this class's folder
10+
*/
11+
public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
12+
Class<?> clazz = Thread.currentThread().getContextClassLoader().loadClass("classLoading.ClassLoaded");
13+
WorkerInterface load = (WorkerInterface)clazz.newInstance();
14+
//ClassLoaded load = new ClassLoaded();
15+
System.out.println("Test.Main: " + Thread.currentThread() + ", " + Thread.currentThread().getContextClassLoader());
16+
load.startThreads(3);
17+
}
18+
19+
}

Diff for: res/source/classLoading/base/TestInterface.java

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package classLoading.base;
2+
3+
public interface TestInterface {
4+
5+
public void printContextInfo();
6+
7+
}

Diff for: res/source/classLoading/base/WorkerInterface.java

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package classLoading.base;
2+
3+
/** Generic interface for casting dynamically loaded worker objects to
4+
* @author TeamworkGuy2
5+
* @since 2013-7-12
6+
*/
7+
public interface WorkerInterface {
8+
9+
public void startThreads(int threadCount);
10+
11+
}

0 commit comments

Comments
 (0)