Skip to content

Commit 26be76e

Browse files
committed
Add package for extended features
1 parent 877ff16 commit 26be76e

Some content is hidden

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

56 files changed

+1173
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package ist.meic.pa.GenericFunctionsExtended;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
@Retention(RetentionPolicy.RUNTIME)
9+
@Target(ElementType.METHOD)
10+
public @interface AfterMethod {
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package ist.meic.pa.GenericFunctionsExtended;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
@Retention(RetentionPolicy.RUNTIME)
9+
@Target(ElementType.METHOD)
10+
public @interface BeforeMethod {
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package ist.meic.pa.GenericFunctionsExtended;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
@Retention(RetentionPolicy.RUNTIME)
9+
@Target(ElementType.TYPE)
10+
public @interface GenericFunction {
11+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package ist.meic.pa.GenericFunctionsExtended;
2+
3+
import ist.meic.pa.GenericFunctionsExtended.translator.GenericFunctionTranslator;
4+
import javassist.ClassPool;
5+
import javassist.Loader;
6+
import javassist.NotFoundException;
7+
import javassist.Translator;
8+
9+
import java.lang.reflect.InvocationTargetException;
10+
import java.util.Arrays;
11+
12+
public class WithGenericFunctions {
13+
public static void main(String[] args) {
14+
15+
try {
16+
// Get our translator that will do our instrumentation
17+
Translator translator = new GenericFunctionTranslator();
18+
19+
// Get the current class pool and add our translator to it's loader
20+
ClassPool classPool = ClassPool.getDefault();
21+
Loader classLoader = new Loader();
22+
classLoader.addTranslator(classPool, translator);
23+
24+
// Get the new parameters for the class we're about to hand over control to
25+
String[] parameters = Arrays.copyOfRange(args, 1, args.length);
26+
27+
// Hand over control, providing the right arguments
28+
classLoader.run(args[0], parameters);
29+
30+
} catch(ArrayIndexOutOfBoundsException e) {
31+
System.out.println("No main class was specified! Can't hand over control, ending.");
32+
System.exit(-1);
33+
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | NotFoundException e) {
34+
// Can't really do anything about this, let's just end the program
35+
System.out.println("Reflection failed...");
36+
System.exit(-1);
37+
} catch (Throwable throwable) {
38+
System.out.println("Failed to run main class! - " + throwable.getMessage());
39+
System.exit(-1);
40+
}
41+
}
42+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package ist.meic.pa.GenericFunctionsExtended.examples;
2+
3+
4+
import ist.meic.pa.GenericFunctionsExtended.examples.domain.Black;
5+
import ist.meic.pa.GenericFunctionsExtended.examples.domain.Blue;
6+
import ist.meic.pa.GenericFunctionsExtended.examples.domain.Color;
7+
import ist.meic.pa.GenericFunctionsExtended.examples.domain.Red;
8+
9+
public class TestA {
10+
public static void main(String[] args) {
11+
Color[] colors = new Color[] { new Red(), new Blue(), new Black()};
12+
for(Color c : colors) System.out.println(Color.mix(c));
13+
}
14+
15+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package ist.meic.pa.GenericFunctionsExtended.examples;
2+
3+
4+
import ist.meic.pa.GenericFunctionsExtended.examples.domain.Com;
5+
6+
public class TestB {
7+
public static void main(String[] args) {
8+
Object[] objects = new Object[] { new Object(), "Foo", 123};
9+
for(Object c : objects) System.out.println(Com.bine(c));
10+
}
11+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package ist.meic.pa.GenericFunctionsExtended.examples;
2+
3+
import ist.meic.pa.GenericFunctionsExtended.examples.domain.Black;
4+
import ist.meic.pa.GenericFunctionsExtended.examples.domain.Color;
5+
import ist.meic.pa.GenericFunctionsExtended.examples.domain.Red;
6+
7+
public class TestC {
8+
public static void main(String[] args) {
9+
Object colors = new Object[] { new Red(), 2.9, new Black(), "Holla!"};
10+
System.out.println(Color.mix(colors));
11+
}
12+
}
13+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package ist.meic.pa.GenericFunctionsExtended.examples;
2+
3+
4+
import ist.meic.pa.GenericFunctionsExtended.examples.domain.Com;
5+
6+
public class TestD {
7+
public static void main(String[] args) {
8+
Object objects = new Object[] { "Foo", new Integer[] {123, -12}};
9+
System.out.println(Com.bine(objects));
10+
11+
Object numbers = new Object[] { 123, new Integer[] {456 , 21}};
12+
System.out.println(Com.bine(numbers));
13+
}
14+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package ist.meic.pa.GenericFunctionsExtended.examples;
2+
3+
import ist.meic.pa.GenericFunctionsExtended.examples.domain.Identify;
4+
5+
public class TestE {
6+
public static void main(String[] args) {
7+
Object objects = new Object[] { 123, "Foo", 1.2};
8+
System.out.println(Identify.it(objects));
9+
}
10+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package ist.meic.pa.GenericFunctionsExtended.examples;
2+
3+
import ist.meic.pa.GenericFunctionsExtended.examples.domain.Bug;
4+
import ist.meic.pa.GenericFunctionsExtended.examples.domain.C1;
5+
import ist.meic.pa.GenericFunctionsExtended.examples.domain.C2;
6+
7+
public class TestF {
8+
public static void main(String[] args) {
9+
Object c1 = new C1(), c2 = new C2();
10+
Bug.bug(c1);
11+
Bug.bug(c2);
12+
}
13+
}

0 commit comments

Comments
 (0)