Created
November 15, 2012 16:05
-
-
Save rschumm/4079402 to your computer and use it in GitHub Desktop.
reflections
This file contains hidden or 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
package ch.schumm.security.pdp; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.RetentionPolicy; | |
/** | |
* Policies mit dieser Annotation werden vom PolicyDispatcher nicht geladen und damit auch nicht ausgewertet. | |
* @author C709360 | |
* | |
*/ | |
@Retention(RetentionPolicy.RUNTIME) | |
public @interface IgnorePolicy { | |
} |
This file contains hidden or 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
package ch.schumm.security.pdp; | |
import java.lang.annotation.Annotation; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Set; | |
import org.reflections.Reflections; | |
import ch.schumm.security.policy.Policy; | |
public class ReflectionPolicyFinder { | |
/** | |
* Findet alle Policies im Packet ch.schumm.security.policy - ausser Policies mit der {@link IgnorePolicy} | |
* Annotation, z.B. die Default-Policy. | |
* @return | |
*/ | |
public static List<Policy> scanForPolicies() { | |
Reflections reflections = new Reflections("ch.schumm.security.policy"); | |
Set<Class<? extends Policy>> policyTypes = reflections.getSubTypesOf(Policy.class); | |
ArrayList<Policy> policiesReturn = new ArrayList<Policy>(); | |
for (Class<? extends Policy> policyType : policyTypes) { | |
// if (policyType != DefaultPolicy.class) { | |
if (!policyHasIgnoreAnnotation(policyType)) { | |
try { | |
Policy policy = policyType.newInstance(); | |
policiesReturn.add(policy); | |
System.out.println(policy); | |
} catch (InstantiationException e) { | |
throw new PolicFinderException(e); | |
} catch (IllegalAccessException e) { | |
throw new PolicFinderException(e); | |
} | |
} | |
} | |
return policiesReturn; | |
} | |
private static boolean policyHasIgnoreAnnotation(Class<? extends Policy> policyType) { | |
boolean hasIgnoreAnotation = false; | |
Annotation[] declaredAnnotations = policyType.getDeclaredAnnotations(); | |
for (Annotation annotation : declaredAnnotations) { | |
if (annotation.annotationType() == IgnorePolicy.class) { | |
hasIgnoreAnotation = true; | |
} | |
} | |
return hasIgnoreAnotation; | |
} | |
public static void main(String[] args) { | |
scanForPolicies(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment