Java Code Examples for com.baidu.hugegraph.util.ReflectionUtil

The following examples show how to use com.baidu.hugegraph.util.ReflectionUtil. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: hugegraph-common   Source File: PerfUtil.java    License: Apache License 2.0 6 votes vote down vote up
public void profilePackage(String... packages)
                           throws NotFoundException, IOException,
                           ClassNotFoundException, CannotCompileException {
    Set<String> loadedClasses = new HashSet<>();

    Iterator<ClassInfo> classes = ReflectionUtil.classes(packages);
    while (classes.hasNext()) {
        String cls = classes.next().getName();
        // super class first
        for (String s : ReflectionUtil.superClasses(cls)) {
            if (!loadedClasses.contains(s)) {
                profileClass(s);
                loadedClasses.add(s);
            }
        }
        // self class
        if (!loadedClasses.contains(cls)) {
            profileClass(cls);
            loadedClasses.add(cls);
        }
    }
}
 
Example 2
Source Project: hugegraph-common   Source File: PerfUtil.java    License: Apache License 2.0 6 votes vote down vote up
public void profileClass(String... classes)
                         throws NotFoundException, CannotCompileException,
                         ClassNotFoundException {
    ClassPool classPool = ClassPool.getDefault();

    for (String cls : classes) {
        CtClass ctClass = classPool.get(cls);
        List<CtMethod> methods = ReflectionUtil.getMethodsAnnotatedWith(
                ctClass, Watched.class, false);
        for (CtMethod method : methods) {
            profile(method);
        }

        // load class and make it effective
        if (!methods.isEmpty()) {
            ctClass.toClass();
        }
    }
}
 
Example 3
Source Project: hugegraph-common   Source File: ReflectionUtilTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testIsSimpleType() {
    Assert.assertTrue(ReflectionUtil.isSimpleType(byte.class));
    Assert.assertTrue(ReflectionUtil.isSimpleType(char.class));
    Assert.assertTrue(ReflectionUtil.isSimpleType(short.class));
    Assert.assertTrue(ReflectionUtil.isSimpleType(int.class));
    Assert.assertTrue(ReflectionUtil.isSimpleType(long.class));
    Assert.assertTrue(ReflectionUtil.isSimpleType(float.class));
    Assert.assertTrue(ReflectionUtil.isSimpleType(double.class));
    Assert.assertTrue(ReflectionUtil.isSimpleType(boolean.class));

    Assert.assertTrue(ReflectionUtil.isSimpleType(Byte.class));
    Assert.assertTrue(ReflectionUtil.isSimpleType(Character.class));
    Assert.assertTrue(ReflectionUtil.isSimpleType(Short.class));
    Assert.assertTrue(ReflectionUtil.isSimpleType(Integer.class));
    Assert.assertTrue(ReflectionUtil.isSimpleType(Long.class));
    Assert.assertTrue(ReflectionUtil.isSimpleType(Float.class));
    Assert.assertTrue(ReflectionUtil.isSimpleType(Double.class));
    Assert.assertTrue(ReflectionUtil.isSimpleType(Boolean.class));
    Assert.assertTrue(ReflectionUtil.isSimpleType(String.class));

    Assert.assertFalse(ReflectionUtil.isSimpleType(Object.class));
    Assert.assertFalse(ReflectionUtil.isSimpleType(BaseUnitTest.class));
}
 
Example 4
Source Project: hugegraph-common   Source File: ReflectionUtilTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testGetMethodsAnnotatedWith() {
    List<Method> methods;

    methods = ReflectionUtil.getMethodsAnnotatedWith(Sub.class,
                                                     Watched.class,
                                                     false);
    methods.sort((m1, m2) -> m1.getName().compareTo(m2.getName()));
    Assert.assertEquals(2, methods.size());
    Assert.assertEquals("func1", methods.get(0).getName());
    Assert.assertEquals("func3", methods.get(1).getName());


    methods = ReflectionUtil.getMethodsAnnotatedWith(Sub.class,
                                                     Watched.class,
                                                     true);
    methods.sort((m1, m2) -> m1.getName().compareTo(m2.getName()));
    Assert.assertEquals(3, methods.size());
    Assert.assertEquals("func", methods.get(0).getName());
    Assert.assertEquals("func1", methods.get(1).getName());
    Assert.assertEquals("func3", methods.get(2).getName());
}
 
Example 5
Source Project: hugegraph-common   Source File: ReflectionUtilTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testClasses() throws IOException {
    @SuppressWarnings("unchecked")
    List<ClassInfo> classes = IteratorUtils.toList(ReflectionUtil.classes(
                              "com.baidu.hugegraph.util"));
    Assert.assertEquals(16, classes.size());
    classes.sort((c1, c2) -> c1.getName().compareTo(c2.getName()));
    Assert.assertEquals("com.baidu.hugegraph.util.Bytes",
                        classes.get(0).getName());
    Assert.assertEquals("com.baidu.hugegraph.util.CheckSocket",
                        classes.get(1).getName());
    Assert.assertEquals("com.baidu.hugegraph.util.CollectionUtil",
                        classes.get(2).getName());
    Assert.assertEquals("com.baidu.hugegraph.util.VersionUtil",
                        classes.get(15).getName());
}
 
Example 6
Source Project: hugegraph-client   Source File: GraphElement.java    License: Apache License 2.0 5 votes vote down vote up
public GraphElement property(String name, Object value) {
    E.checkArgumentNotNull(name, "property name");
    E.checkArgumentNotNull(value, "property value");

    Class<?> clazz = value.getClass();
    E.checkArgument(ReflectionUtil.isSimpleType(clazz) ||
                    clazz.equals(UUID.class) ||
                    clazz.equals(Date.class) ||
                    value instanceof List ||
                    value instanceof Set,
                    "Invalid property value type: '%s'", clazz);

    this.properties.put(name, value);
    return this;
}
 
Example 7
Source Project: hugegraph-common   Source File: ReflectionUtilTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testSuperClasses() throws NotFoundException {
    List<String> classes = ReflectionUtil.superClasses(Sub.class.getName());
    Assert.assertEquals(2, classes.size());
    classes.sort((c1, c2) -> c1.compareTo(c2));
    Assert.assertEquals(Base.class.getName(), classes.get(0));
    Assert.assertEquals(Object.class.getName(), classes.get(1));
}
 
Example 8
Source Project: hugegraph-loader   Source File: DataTypeUtil.java    License: Apache License 2.0 4 votes vote down vote up
public static boolean isSimpleValue(Object value) {
    if (value == null) {
        return false;
    }
    return ReflectionUtil.isSimpleType(value.getClass());
}