Skip to content

Commit

Permalink
Reflection
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxim Butov committed Oct 6, 2019
1 parent b0fd7d7 commit 5aa77b7
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
/**
* Caches based on {@link java.util.WeakHashMap}.
*
* These caches can be used without any cleaning up. GC takes care of them.
*
* These caches also do not guarantee the presence of previously cached entries.
*
* @author Maxim Butov
*/
public class WeakKeysCaches {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private V unwrap(Reference<V> reference) {

@Override
public boolean containsKey(Object key) {
return cache.containsKey(key);
return get(key) != null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.sugarcubes.reflection;

import java.util.Collections;
import java.util.EnumSet;
import java.util.Set;

/**
* @author Maxim Butov
*/
public enum XModifier {

PUBLIC(0x00000001),
PRIVATE(0x00000002),
PROTECTED(0x00000004),
STATIC(0x00000008),
FINAL(0x00000010),
SYNCHRONIZED(0x00000020),
VOLATILE(0x00000040),
TRANSIENT(0x00000080),
NATIVE(0x00000100),
INTERFACE(0x00000200),
ABSTRACT(0x00000400),
STRICT(0x00000800),

BRIDGE(0x00000040),
VARARGS(0x00000080),
SYNTHETIC(0x00001000),
ANNOTATION(0x00002000),
ENUM(0x00004000),
MANDATED(0x00008000),

;

private static Set<XModifier> set(XModifier first, XModifier... rest) {
return Collections.unmodifiableSet(EnumSet.of(first, rest));
}

public static final Set<XModifier> CLASS_MODIFIERS = set(PUBLIC, PROTECTED, PRIVATE, ABSTRACT, STATIC, FINAL, STRICT);
public static final Set<XModifier> INTERFACE_MODIFIERS = set(PUBLIC, PROTECTED, PRIVATE, ABSTRACT, STATIC, STRICT);
public static final Set<XModifier> CONSTRUCTOR_MODIFIERS = set(PUBLIC, PROTECTED, PRIVATE);
public static final Set<XModifier> METHOD_MODIFIERS = set(PUBLIC, PROTECTED, PRIVATE, ABSTRACT, STATIC, FINAL, SYNCHRONIZED,
NATIVE, STRICT);
public static final Set<XModifier> FIELD_MODIFIERS = set(PUBLIC, PROTECTED, PRIVATE, STATIC, FINAL, TRANSIENT, VOLATILE);
public static final Set<XModifier> PARAMETER_MODIFIERS = set(FINAL);
public static final Set<XModifier> ACCESS_MODIFIERS = set(PUBLIC, PROTECTED, PRIVATE);

private final int intValue;

XModifier(int intValue) {
this.intValue = intValue;
}

public int getIntValue() {
return intValue;
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package org.sugarcubes.reflection;

import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.Set;
import java.util.function.IntPredicate;
import java.util.stream.Collectors;

/**
* Modifiers-related methods.
Expand Down Expand Up @@ -34,6 +37,16 @@ default boolean isModifier(IntPredicate method) {
return method.test(getModifiers());
}

default Set<XModifier> getXModifiers() {
return Arrays.stream(XModifier.values())
.filter(this::isXModifier)
.collect(Collectors.toSet());
}

default boolean isXModifier(XModifier modifier) {
return (getModifiers() & modifier.getIntValue()) != 0;
}

default boolean isPublic() {
return isModifier(Modifier::isPublic);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
import java.util.Set;
import java.util.stream.Collectors;

import org.junit.Assert;
import org.junit.Test;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

/**
* todo: document it
Expand All @@ -22,11 +26,11 @@ public void testClasses() {

XClass<Integer> xClass = XReflection.of(Integer.class);

Assert.assertThat(xClass.getConstructors().count(), greaterThan(0L));
Assert.assertThat(xClass.getFields().count(), greaterThan(0L));
Assert.assertThat(xClass.getFields().count(), greaterThan(xClass.getDeclaredFields().count()));
Assert.assertThat(xClass.getMethods().count(), greaterThan(0L));
Assert.assertThat(xClass.getMethods().count(), greaterThan(xClass.getDeclaredMethods().count()));
assertThat(xClass.getConstructors().count(), greaterThan(0L));
assertThat(xClass.getFields().count(), greaterThan(0L));
assertThat(xClass.getFields().count(), greaterThan(xClass.getDeclaredFields().count()));
assertThat(xClass.getMethods().count(), greaterThan(0L));
assertThat(xClass.getMethods().count(), greaterThan(xClass.getDeclaredMethods().count()));

Set<XField<?>> collect = xClass.getFields().filter(xField -> !xField.isPublic()).collect(Collectors.toSet());

Expand All @@ -37,7 +41,7 @@ public void testInheritance() {

List<Class> inheritance =
XReflection.of(Integer.class).getInheritance().map(XClass::getReflectionObject).collect(Collectors.toList());
Assert.assertThat(inheritance, is(Arrays.asList(Integer.class, Number.class, Object.class)));
assertThat(inheritance, is(Arrays.asList(Integer.class, Number.class, Object.class)));

}

Expand All @@ -49,8 +53,8 @@ public void testFinal() {
Integer obj = new Integer(-1);
Integer newValue = 1;
xField.set(obj, newValue);
Assert.assertEquals(newValue, xField.get(obj));

assertEquals(newValue, xField.get(obj));

}

Expand All @@ -59,20 +63,24 @@ public void testModifiers() {

XClass<Integer> xClass = XReflection.of(Integer.class);

Assert.assertTrue(xClass.isPublic());
Assert.assertTrue(xClass.isFinal());
assertTrue(xClass.isPublic());
assertTrue(xClass.isFinal());

assertFalse(xClass.isAbstract());
assertFalse(xClass.isInterface());
assertFalse(xClass.isPackage());
assertFalse(xClass.isPrivate());
assertFalse(xClass.isProtected());

Assert.assertFalse(xClass.isAbstract());
Assert.assertFalse(xClass.isInterface());
Assert.assertFalse(xClass.isPackage());
Assert.assertFalse(xClass.isPrivate());
Assert.assertFalse(xClass.isProtected());
assertThat(xClass.getXModifiers(), containsInAnyOrder(XModifier.PUBLIC, XModifier.FINAL));

XField<Object> xField = xClass.getDeclaredField("serialVersionUID");

Assert.assertTrue(xField.isPrivate());
Assert.assertTrue(xField.isStatic());
Assert.assertTrue(xField.isFinal());
assertTrue(xField.isPrivate());
assertTrue(xField.isStatic());
assertTrue(xField.isFinal());

assertThat(xField.getXModifiers(), containsInAnyOrder(XModifier.PRIVATE, XModifier.STATIC, XModifier.FINAL));

}

Expand Down

0 comments on commit 5aa77b7

Please sign in to comment.