/** * Get a class reference for this class. Causes the ClassLoader to load the class. * * @return The class reference. * @throws IllegalArgumentException * if there were problems loading or initializing the class. (Note that class initialization on load * is disabled by default, you can enable it with * {@code FastClasspathScanner#initializeLoadedClasses(true)} .) */ public Class<?> getClassRef() { return scanResult.classNameToClassRef(className); }
/** * Get a class reference for the annotation. * * @return The annotation type, as a {@code Class<?>} reference. */ public Class<?> getAnnotationType() { return scanResult.classNameToClassRef(annotationName); }
/** * Get a class reference for this class, casting it to the requested interface or superclass type. Causes the * ClassLoader to load the class. * * @param classType * The class to cast the result to. * @return The class reference. * @throws IllegalArgumentException * if there were problems loading the class, initializing the class, or casting it to the requested * type. (Note that class initialization on load is disabled by default, you can enable it with * {@code FastClasspathScanner#initializeLoadedClasses(true)} .) */ public <T> Class<T> getClassRef(final Class<T> classType) { return scanResult.classNameToClassRef(className, classType); }
/** * Produce Class reference given a class name. If the class cannot be loaded (due to classloading error, or due * to an exception being thrown in the class initialization block), an IllegalArgumentException is thrown. * * <p> * Enable verbose scanning to see details of any exceptions thrown during classloading, even if ignoreExceptions * is false. * * @param className * The names of the classe to load. * @throws IllegalArgumentException * if there were problems loading or initializing the class. (Note that class initialization on load * is disabled by default, you can enable it with * {@code FastClasspathScanner#initializeLoadedClasses(true)} .) * @return a reference to the loaded class, or null if the class could not be loaded and ignoreExceptions is * true. */ public Class<?> classNameToClassRef(final String className) throws IllegalArgumentException { return classNameToClassRef(className, /* ignoreExceptions = */ false); }
/** * Produce Class reference given a class name. If the class cannot be loaded (due to classloading error, or due * to an exception being thrown in the class initialization block), an IllegalArgumentException is thrown. * * <p> * Enable verbose scanning to see details of any exceptions thrown during classloading, even if ignoreExceptions * is false. * * @param className * The names of the classe to load. * @param classType * The class type to cast the result to. * @throws IllegalArgumentException * if there were problems loading the class, initializing the class, or casting it to the requested * type. (Note that class initialization on load is disabled by default, you can enable it with * {@code FastClasspathScanner#initializeLoadedClasses(true)} .) * @return a reference to the loaded class, or null if the class could not be loaded and ignoreExceptions is * true. */ public <T> Class<T> classNameToClassRef(final String className, final Class<T> classType) throws IllegalArgumentException { return classNameToClassRef(className, classType, /* ignoreExceptions = */ false); }
/** Instantiate class ref. Type arguments are ignored. */ @Override public Class<?> instantiate(final ScanResult scanResult) { final StringBuilder buf = new StringBuilder(); buf.append(className); for (int i = 0; i < suffixes.size(); i++) { buf.append("$"); buf.append(suffixes.get(i)); } final String classNameWithSuffixes = buf.toString(); return scanResult.classNameToClassRef(classNameWithSuffixes); }
/** * Returns a list of {@code Class<?>} references for the annotations on this method. Note that this calls * Class.forName() on the annotation types, which will cause each annotation class to be loaded. * * @return a list of {@code Class<?>} references for the annotations on this method, or the empty list if none. * @throws IllegalArgumentException * if the annotation type could not be loaded. */ public List<Class<?>> getAnnotationTypes() throws IllegalArgumentException { if (annotationInfo == null || annotationInfo.isEmpty()) { return Collections.<Class<?>> emptyList(); } else { final List<Class<?>> annotationClassRefs = new ArrayList<>(); for (final String annotationName : getAnnotationNames()) { annotationClassRefs.add(scanResult.classNameToClassRef(annotationName)); } return annotationClassRefs; } }
/** * Returns {@code Class<?>} references for the unique annotations on this field. Note that this calls * Class.forName() on the annotation types, which will cause each annotation class to be loaded. * * @return {@code Class<?>} references for the unique annotations on this field. * @throws IllegalArgumentException * if the annotation type could not be loaded. */ public List<Class<?>> getAnnotationTypes() throws IllegalArgumentException { if (annotationInfo == null || annotationInfo.isEmpty()) { return Collections.<Class<?>> emptyList(); } else { final List<Class<?>> annotationClassRefs = new ArrayList<>(); for (final String annotationName : getAnnotationNames()) { annotationClassRefs.add(scanResult.classNameToClassRef(annotationName)); } return annotationClassRefs; } }
final Class<?> classRef = classNameToClassRef(className, ignoreExceptions); if (classRef != null) { classRefs.add(classRef);
/** * Returns the unique annotation types for annotations on each method parameter, if any parameters have * annotations, else returns null. * * Note that it is always faster to call {@link #getParameterInfo()} and get the parameter information from the * returned list of {@link MethodParameterInfo} objects, since this method calls that to compile its results. * * @return The method parameter annotation types, as an array of Strings, or null if no parameter annotations * are present. */ public Class<?>[][] getParameterAnnotationTypes() { final String[][] paramAnnotationNames = getParameterAnnotationNames(); if (paramAnnotationNames == null) { return null; } final Class<?>[][] parameterAnnotationTypes = new Class<?>[paramAnnotationNames.length][]; for (int i = 0; i < paramAnnotationNames.length; i++) { parameterAnnotationTypes[i] = new Class<?>[paramAnnotationNames[i].length]; for (int j = 0; j < paramAnnotationNames[i].length; j++) { parameterAnnotationTypes[i][j] = scanResult.classNameToClassRef(paramAnnotationNames[i][j]); } } return parameterAnnotationTypes; }
private static void ensureInit() { if (!init) { synchronized (BeanProcessors.class) { if (!init) { log.info("Init bean processors ..."); ScanResult result = AutumnClasspathScan.getScanResult(); result.getNamesOfClassesImplementing(BeanProcessor.class).forEach(cn -> { try { Class<? extends BeanProcessor> processorClass = (Class<? extends BeanProcessor>) result.classNameToClassRef(cn); BeanProcessor processor = processorClass.newInstance(); for (Class<?> intf : processorClass.getInterfaces()) { if (BeanProcessor.class.isAssignableFrom(intf)) { beanProcessors.computeIfAbsent((Class<? extends BeanProcessor>) intf, x -> new ArrayList<>()).add(processor); } } } catch (Exception e) { log.warn("Cannot init bean processor [{}]: {}", cn, String.valueOf(e)); } }); init = true; } } } }
final Class<T> classRef = classNameToClassRef(className, commonClassType, ignoreExceptions); if (classRef != null) { classRefs.add(classRef);
public static void processAll(ScanSpec spec, AutumnContext context) { if (scanners == null) { synchronized (BeanScanners.class) { if (scanners == null) { log.info("Init bean scanners ..."); scanners = new ArrayList<>(); ScanResult result = AutumnClasspathScan.getScanResult(); result.getNamesOfClassesImplementing(BeanScanner.class).forEach(cn -> { try { scanners.add((BeanScanner) result.classNameToClassRef(cn).newInstance()); } catch (Exception e) { log.warn("Cannot init bean scanner [{}], {}", cn, String.valueOf(e)); } }); } } } scanners.forEach(sc -> sc.process(spec, context)); } }
private static void ensureInit() { if (!init) { synchronized (BeanPopulators.class) { if (!init) { log.info("Init bean populators ..."); ScanResult result = AutumnClasspathScan.getScanResult(); result.getNamesOfSubclassesOf(AnnotatedFieldPopulator.class).forEach(cn -> { try { AnnotatedFieldPopulator<?> populator = (AnnotatedFieldPopulator<?>) result.classNameToClassRef(cn).newInstance(); annotatedFieldPopulatorMap.put(populator.getAnnotationClass(), populator); } catch (Exception e) { log.warn("Cannot init bean populator [{}]: {}", cn, String.valueOf(e)); } }); init = true; } } } }
/** * Get the enum constant. Causes the ClassLoader to load the enum class. * * @return A ref to the enum constant value. * @throws IllegalArgumentException * if the class could not be loaded, or the enum constant is invalid. */ public Object getEnumValueRef() throws IllegalArgumentException { final Class<?> classRef = scanResult.classNameToClassRef(className); if (!classRef.isEnum()) { throw new IllegalArgumentException("Class " + className + " is not an enum"); } Field field; try { field = classRef.getDeclaredField(constName); } catch (NoSuchFieldException | SecurityException e) { throw new IllegalArgumentException("Could not find enum constant " + toString(), e); } if (!field.isEnumConstant()) { throw new IllegalArgumentException("Field " + toString() + " is not an enum constant"); } try { return field.get(null); } catch (final IllegalAccessException e) { throw new IllegalArgumentException("Field " + toString() + " is not accessible", e); } }
@SuppressWarnings("unchecked") public synchronized void scanAnnotations() { log.info("Scanning annotations ..."); ScanResult result = AutumnClasspathScan.getScanResult(); result.getNamesOfClassesWithAnnotation(WebServlet.class).forEach(cn -> { Class<?> cls = result.classNameToClassRef(cn); if (HttpServlet.class.isAssignableFrom(cls)) { WebServlet ws = cls.getAnnotation(WebServlet.class); TPServletRegistration registration = this.addServlet( ws.name().isEmpty() ? cn : ws.name(), (Class<? extends Servlet>) cls); registration.loadAnnotation(ws); } }); result.getNamesOfClassesWithAnnotation(WebFilter.class).forEach(cn -> { Class<?> cls = result.classNameToClassRef(cn); if (HttpFilter.class.isAssignableFrom(cls)) { WebFilter wf = cls.getAnnotation(WebFilter.class); TPFilterRegistration registration = this.addFilter( wf.filterName().isEmpty() ? cn : wf.filterName(), (Class<? extends Filter>) cls); registration.loadAnnotation(wf); } }); result.getNamesOfClassesWithAnnotation(WebListener.class).forEach(cn -> { Class<?> cls = result.classNameToClassRef(cn); if (EventListener.class.isAssignableFrom(cls)) { this.addListener((Class<? extends EventListener>) cls); } }); }
@Override public Set<Field> getFieldsAnnotatedWith(Class clazz) { Map<String, ClassInfo> classInfos = scanResult.getClassNameToClassInfo(); Set<FieldIdentifier> fieldIdentifiers = new HashSet<>(); for (ClassInfo classInfo : classInfos.values()){ for (FieldInfo fieldInfo : classInfo.getFieldInfo()) { List<AnnotationInfo> fieldsAnnotationInfos = fieldInfo.getAnnotationInfo(); for (AnnotationInfo oneFieldAnnotation: fieldsAnnotationInfos) { if (oneFieldAnnotation.getAnnotationType() == clazz) { fieldIdentifiers.add(new FieldIdentifier(fieldInfo.getClassName(), fieldInfo.getFieldName(), fieldInfo.getType())); } } } } Set<Field> fields = new HashSet<>(fieldIdentifiers.size()); for (FieldIdentifier fieldIdentifier : fieldIdentifiers) { Class<?> classe = scanResult.classNameToClassRef(fieldIdentifier.getClassName()); for (Field field : classe.getDeclaredFields()) { if (fieldIdentifier.matchesField(classe, field)){ fields.add(field); } } } return fields; }
@Override public Set<Method> getMethodsWithAnyParamAnnotated(Class clazz) { Map<String, ClassInfo> classInfos = scanResult.getClassNameToClassInfo(); Set<MethodIdentifier> methodIdentifiers = new HashSet<>(); for (ClassInfo classInfo : classInfos.values()){ for (MethodInfo methodInfo : classInfo.getMethodAndConstructorInfo()) { AnnotationInfo[][] parametersAnnotationInfo = methodInfo.getParameterAnnotationInfo(); if (parametersAnnotationInfo != null) for (AnnotationInfo[] oneParametersAnnotationInfo: parametersAnnotationInfo) { for (AnnotationInfo ai : oneParametersAnnotationInfo) { if (ai.getAnnotationType() == clazz) { methodIdentifiers.add(new MethodIdentifier(methodInfo.getClassName(), methodInfo.getMethodName(), methodInfo.getParameterTypeSignatures())); } } } } } Set<Method> methods = new HashSet<>(methodIdentifiers.size()); for (MethodIdentifier methodIdentifier : methodIdentifiers) { Class<?> classe = scanResult.classNameToClassRef(methodIdentifier.getClassName()); for (Method method : classe.getDeclaredMethods()) { if (methodIdentifier.matchesMethod(classe, method, scanResult)){ methods.add(method); } } } return methods; }
for (MethodIdentifier methodIdentifier : methodIdentifiers) Class<?> classe = scanResult.classNameToClassRef(methodIdentifier.getClassName()); for (Constructor<?> constructor : classe.getConstructors()) { if (methodIdentifier.matchesConstructor(classe, constructor, scanResult)){
Class<?> javaClass = result.classNameToClassRef(className); if (!isVisible(javaClass.getModifiers())) {