-
-
Notifications
You must be signed in to change notification settings - Fork 301
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5902 from pkriens/feature/class-analysis
Minor convenience functions to classfile and libg
- Loading branch information
Showing
14 changed files
with
349 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
@Version("1.0.0") | ||
@Version("1.1.0") | ||
package aQute.libg.parameters; | ||
|
||
import org.osgi.annotation.versioning.Version; |
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,23 @@ | ||
package aQute.libg.parameters; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.List; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class AttributesTest { | ||
|
||
@SuppressWarnings("unchecked") | ||
@Test | ||
void test() { | ||
Attributes a = new Attributes("foo = 1 , bar:Long = 42, l:List<Long> = '1,2,3,4'"); | ||
assertThat(a.toString()).isEqualTo("foo=1,bar:Long=42,l:List<Long>=\"1,2,3,4\""); | ||
assertThat(a.get("foo")).isEqualTo("1"); | ||
assertThat(a.getTyped("foo")).isEqualTo("1"); | ||
assertThat(a.getTyped("bar")).isEqualTo(42L); | ||
assertThat(a.get("l")).isEqualTo("1,2,3,4"); | ||
assertThat((List<Long>) a.getTyped("l")).containsExactly(1L, 2L, 3L, 4L); | ||
} | ||
|
||
} |
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
102 changes: 102 additions & 0 deletions
102
biz.aQute.bnd.util/src/aQute/bnd/classfile/builder/ReflectBuilder.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,102 @@ | ||
package aQute.bnd.classfile.builder; | ||
|
||
import java.lang.reflect.Field; | ||
import java.lang.reflect.Member; | ||
import java.lang.reflect.Method; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
import aQute.bnd.classfile.Attribute; | ||
import aQute.bnd.classfile.FieldInfo; | ||
import aQute.bnd.classfile.MethodInfo; | ||
|
||
/** | ||
* In some cases it is necessary to have a ClassFile for a built in Java class. | ||
* This builder will create a reflect builder that is based on a Class object. | ||
* Unfortunately, restrictions in the Java API make it impossible to get all the | ||
* details. For example, there are no code attributes. | ||
* <p> | ||
* This is far from complete but it can be used in analysis cases. All | ||
* information about the types of methods and fields are captured. | ||
*/ | ||
public class ReflectBuilder { | ||
final static int javaSpecificationVersion = Integer | ||
.parseInt(System.getProperty("java.specification.version")); | ||
final static int major_version = javaSpecificationVersion + 44; | ||
|
||
/** | ||
* Create a ClassFileBuilder based on the given class | ||
* | ||
* @param c the given class | ||
* @return a ClassFileBuilder initialized with the class | ||
*/ | ||
public static ClassFileBuilder of(Class<?> c) { | ||
ClassFileBuilder cfb = new ClassFileBuilder(c.getModifiers(), major_version, 0, toName(c), | ||
toName(c.getSuperclass()), Stream.of(c.getInterfaces()) | ||
.map(ReflectBuilder::toName) | ||
.collect(Collectors.toSet())); | ||
|
||
for (Field f : c.getDeclaredFields()) { | ||
FieldInfo fi = new FieldInfo(f.getModifiers(), f.getName(), descriptor(f, f.getType()), attributes(f)); | ||
cfb.fields(fi); | ||
} | ||
|
||
for (Method m : c.getDeclaredMethods()) { | ||
MethodInfo mi = new MethodInfo(m.getModifiers(), m.getName(), | ||
descriptor(m, m.getReturnType(), m.getParameterTypes()), attributes(m)); | ||
cfb.methods(mi); | ||
} | ||
|
||
cfb.attributes(attributes(c)); | ||
|
||
return cfb; | ||
} | ||
|
||
private static Attribute[] attributes(Object f) { | ||
return new Attribute[0]; | ||
} | ||
|
||
@SuppressWarnings("rawtypes") | ||
private static String descriptor(Member m, Class<?> type, Class... parameters) { | ||
StringBuilder sb = new StringBuilder(); | ||
if (m instanceof Method) { | ||
sb.append("("); | ||
for (Class p : parameters) { | ||
encode(p); | ||
} | ||
sb.append(")"); | ||
} | ||
sb.append(encode(type)); | ||
return sb.toString(); | ||
} | ||
|
||
private static String encode(Class<?> type) { | ||
if (type == void.class) | ||
return "V"; | ||
if (type == boolean.class) | ||
return "Z"; | ||
if (type == byte.class) | ||
return "B"; | ||
if (type == short.class) | ||
return "S"; | ||
if (type == char.class) | ||
return "C"; | ||
if (type == int.class) | ||
return "I"; | ||
if (type == long.class) | ||
return "J"; | ||
if (type == float.class) | ||
return "F"; | ||
if (type == double.class) | ||
return "D"; | ||
|
||
return "L".concat(toName(type)) | ||
.concat(";"); | ||
} | ||
|
||
private static String toName(Class<?> c) { | ||
if (c == null) | ||
return null; | ||
return c.getName(); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
biz.aQute.bnd.util/src/aQute/bnd/classfile/builder/package-info.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
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
/** | ||
* This package provides an object model and parser for Java class files. | ||
*/ | ||
@Version("1." + ClassFile.MAJOR_VERSION + "1.0") | ||
@Version("1." + ClassFile.MAJOR_VERSION + "2.0") | ||
package aQute.bnd.classfile; | ||
|
||
import org.osgi.annotation.versioning.Version; |
34 changes: 34 additions & 0 deletions
34
biz.aQute.bnd.util/test/aQute/bnd/classfile/builder/ReflectBuilderTest.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,34 @@ | ||
package aQute.bnd.classfile.builder; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.io.IOException; | ||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import aQute.bnd.classfile.ClassFile; | ||
|
||
class ReflectBuilderTest { | ||
|
||
@Test | ||
void testSupplier() { | ||
ClassFileBuilder cfb = ReflectBuilder.of(Supplier.class); | ||
ClassFile build = cfb.build(); | ||
assertThat(build).isNotNull(); | ||
assertThat(build.methods).hasSize(1); | ||
assertThat(build.methods[0].name).isEqualTo("get"); | ||
assertThat(build.methods[0].descriptor).isEqualTo("()Ljava.lang.Object;"); | ||
} | ||
|
||
@Test | ||
void testFunction() throws IOException { | ||
ClassFileBuilder cfb = ReflectBuilder.of(Function.class); | ||
ClassFile build = cfb.build(); | ||
assertThat(build).isNotNull(); | ||
assertThat(build.methods).hasSize(7); | ||
byte[] write = build.write(); | ||
assertThat(write).isNotNull(); | ||
} | ||
} |
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
Oops, something went wrong.