List of usage examples for org.apache.commons.lang3 ClassUtils getAllSuperclasses
public static List<Class<?>> getAllSuperclasses(final Class<?> cls)
Gets a List of superclasses for the given class.
From source file:com.streamsets.datacollector.definition.StageDefinitionExtractor.java
public static List<String> getGroups(Class<? extends Stage> klass) { Set<String> set = new LinkedHashSet<>(); addGroupsToList(klass, set);//from w ww. j a va2 s. com List<Class<?>> allSuperclasses = ClassUtils.getAllSuperclasses(klass); for (Class<?> superClass : allSuperclasses) { if (!superClass.isInterface() && superClass.isAnnotationPresent(ConfigGroups.class)) { addGroupsToList(superClass, set); } } if (set.isEmpty()) { set.add(""); // the default empty group } return new ArrayList<>(set); }
From source file:controllers.factories.ViewModelFactory.java
private ViewModel createSpecific(Object model) { Reflections reflections = new Reflections("models", Play.application().getWrappedApplication().classloader()); Set<Class<? extends ViewModel>> viewModelClasses = reflections.getSubTypesOf(ViewModel.class); List<Class<?>> modelCandidateClasses = ClassUtils.getAllSuperclasses(model.getClass()); modelCandidateClasses.add(0, model.getClass()); Class<?> availableModelClass = null; Class<? extends ViewModel> availableViewModelClass = null; for (final Class<?> modelSuperclass : modelCandidateClasses) { availableViewModelClass = Iterables.find(viewModelClasses, new Predicate<Class<? extends ViewModel>>() { @Override//from ww w. j a va2 s . com public boolean apply(Class<? extends ViewModel> viewModelClass) { return ClassUtils.getShortClassName(viewModelClass) .equals(ClassUtils.getShortClassName(modelSuperclass) + "Model"); } }, null); if (availableViewModelClass != null) { availableModelClass = modelSuperclass; break; } } if (availableModelClass == null) { return null; } try { Constructor<? extends ViewModel> constructor = availableViewModelClass .getConstructor(availableModelClass); return constructor.newInstance(model); } catch (Throwable e) { return null; } }
From source file:io.github.m0pt0pmatt.survivalgames.listener.SurvivalGameEventListener.java
private void handleCommandBlock(CommandBlock commandBlock, SurvivalGameEvent event) { Value<String> storedCommand = commandBlock.storedCommand(); if (!storedCommand.exists()) { return;//from w ww. j a v a 2s. c o m } String[] parts = storedCommand.get().trim().split("\\s+"); if (parts.length < 3) { return; } if (!parts[0].equalsIgnoreCase("/ssg") || !parts[1].equalsIgnoreCase("event")) { return; } String eventName = parts[2]; List<String> classes = ImmutableList.<Class<?>>builder().add(event.getClass()) .addAll(ClassUtils.getAllSuperclasses(event.getClass())).build().stream() .filter(c -> !survivalGameClasses.contains(c)).map(Class::getSimpleName) .collect(Collectors.toList()); if (!classes.contains(eventName)) { return; } // execute other command if (parts.length > 3) { String internalCommand = storedCommand.get().substring(COMMAND_BLOCK_STRING.length()); CommandSource source = null; if (event instanceof PlayerEvent) { source = ((PlayerEvent) event).getPlayer(); } else if (event instanceof IntervalEvent) { source = commandBlock; } if (source != null) { Sponge.getCommandManager().process(source, internalCommand); } } // Redstone trick BlockSnapshot snapshot = ActiveIntervalRepository.getSnapshot(commandBlock.getLocation()); commandBlock.getLocation().setBlock(BlockState.builder().blockType(BlockTypes.REDSTONE_TORCH).build(), BlockChangeFlags.ALL); SurvivalGamesPlugin.SYNC_EXECUTOR.schedule(() -> snapshot.restore(true, BlockChangeFlags.ALL), 1, TimeUnit.SECONDS); }
From source file:com.threewks.thundr.introspection.ClassIntrospector.java
public List<Class<?>> listImplementedTypes(Class<?> type) { EList<Class<?>> types = Expressive.<Class<?>>list(type); types.addItems(ClassUtils.getAllSuperclasses(type)); types.addItems(ClassUtils.getAllInterfaces(type)); return types; }
From source file:controllers.ModuleController.java
private static List<ModuleModel> getNextModules(String input) { // get all the supplied view models. List<ViewModel> suppliedViewModels = Lists.newArrayList(); JsonNode inputJson = Json.parse(input); // convert json nodes to view models. if (inputJson != null && inputJson.isArray()) { suppliedViewModels = Lists//from w w w. j a v a 2 s . c o m .newArrayList(Iterators.transform(inputJson.getElements(), new Function<JsonNode, ViewModel>() { @Override @Nullable public ViewModel apply(@Nullable JsonNode input) { if (!input.isTextual()) { return null; } return createViewModelQuietly( fetchResource(UuidUtils.create(input.asText()), PersistentObject.class), null); } })); } else if (inputJson != null && inputJson.isObject()) { suppliedViewModels.add(createViewModelQuietly(inputJson, null)); } suppliedViewModels = Lists.newArrayList(Iterables.filter(suppliedViewModels, Predicates.notNull())); // get all the modules that can use these inputs. Map<Module, Double> nullModulesMap = Maps.newHashMap(); Map<Module, Double> modulesMap = Maps.newHashMap(); Reflections reflections = new Reflections("controllers.modules", Play.application().classloader()); for (Class<? extends Module> moduleClass : reflections.getSubTypesOf(Module.class)) { // we're not interested in abstract classes. if (Modifier.isAbstract(moduleClass.getModifiers())) { continue; } // get the Module.Requires/Requireses annotation for each module class. // the requirements within each Module.Require are ANDed. // the requirements across multiple Module.Require annotations are ORed. List<Module.Requires> requireds = Lists.newArrayList(); if (moduleClass.isAnnotationPresent(Module.Requires.class)) { requireds.add(moduleClass.getAnnotation(Module.Requires.class)); } if (moduleClass.isAnnotationPresent(Module.Requireses.class)) { Collections.addAll(requireds, moduleClass.getAnnotation(Module.Requireses.class).value()); } if (requireds.size() == 0) { requireds.add(null); } for (Module.Requires required : requireds) { final Set<Class<? extends ViewModel>> requiredViewModelClasses = Sets.newHashSet(); if (required != null) { Collections.addAll(requiredViewModelClasses, required.value()); } // get all the supplied view modules that are relevant to this module. List<ViewModel> usefulViewModels = Lists .newArrayList(Iterables.filter(suppliedViewModels, new Predicate<ViewModel>() { @Override public boolean apply(@Nullable ViewModel input) { // if this class is required, then return true. if (requiredViewModelClasses.contains(input.getClass())) { return true; } // if any of its super classes are required, that also works. for (Class<?> superClass : ClassUtils.getAllSuperclasses(input.getClass())) { if (requiredViewModelClasses.contains(superClass)) { return true; } } return false; } })); // if all the requirements were satisfied. if (usefulViewModels.size() >= requiredViewModelClasses.size()) { // try to create an instance of the module. Module module = null; try { module = moduleClass.newInstance(); module.setViewModels(usefulViewModels); } catch (InstantiationException | IllegalAccessException | IllegalArgumentException e) { module = null; } finally { // if no module was created, just ignore. if (module == null) { continue; } } // let's not divide by zero! double relevancyScore = suppliedViewModels.size() != 0 ? usefulViewModels.size() / (double) suppliedViewModels.size() : 1.0; // keep null modules separate. Map<Module, Double> targetModulesMap = null; if (requiredViewModelClasses.size() > 0) { // if a module of this type does not exist, add it. if (Maps.filterKeys(modulesMap, Predicates.instanceOf(moduleClass)).size() == 0) { targetModulesMap = modulesMap; } } else { targetModulesMap = nullModulesMap; } if (targetModulesMap != null) { targetModulesMap.put(module, relevancyScore); } } } } // use null modules only if there are no regular ones. if (modulesMap.size() == 0) { modulesMap = nullModulesMap; } // convert to view models. Set<ModuleModel> moduleViewModels = Sets.newHashSet( Iterables.transform(modulesMap.entrySet(), new Function<Entry<Module, Double>, ModuleModel>() { @Override @Nullable public ModuleModel apply(@Nullable Entry<Module, Double> input) { return new ModuleModel(input.getKey()).setRelevancyScore(input.getValue()); } })); // order first by relevance and then by name. return Ordering.from(new Comparator<ModuleModel>() { @Override public int compare(ModuleModel o1, ModuleModel o2) { int relDiff = (int) Math.round((o2.relevancyScore - o1.relevancyScore) * 1000); if (relDiff == 0) { return o1.name.compareTo(o2.name); } return relDiff; } }).sortedCopy(moduleViewModels); }
From source file:de.tolina.common.validation.AnnotationValidation.java
/** * Calls dependent on the type of the given Object: * <br> - {@link AnnotationUtils#getAnnotations(Method)} or * <br> - {@link AnnotationUtils#getAnnotations(java.lang.reflect.AnnotatedElement)} *//*from www. j a v a2 s. c om*/ @Nullable private Annotation[] getAllAnnotationsFor(@Nonnull final Object annotated) { if (annotated instanceof Field) { return getAnnotations((Field) annotated); } if (annotated instanceof Method) { final Method annotatedMethod = (Method) annotated; final Class<?> declaringClass = annotatedMethod.getDeclaringClass(); final List<Class<?>> allClasses = new ArrayList<>(); allClasses.add(declaringClass); allClasses.addAll(ClassUtils.getAllSuperclasses(declaringClass)); final ArrayList<Annotation> allAnnotations = new ArrayList<>(); for (final Class<?> aClass : allClasses) { final ArrayList<Method> allMethods = new ArrayList<>(); allMethods.addAll(Arrays.asList(aClass.getDeclaredMethods())); final List<Class<?>> interfaces = ClassUtils.getAllInterfaces(aClass); for (final Class<?> anInterface : interfaces) { allMethods.addAll(Arrays.asList(anInterface.getDeclaredMethods())); } allMethods.stream().filter(method -> isSameMethod(method, annotatedMethod)) .forEachOrdered(method -> addIfNotPresent(allAnnotations, getAnnotations(method))); } return allAnnotations.toArray(new Annotation[] {}); } final Class<?> annotatedClass = (Class<?>) annotated; final List<Class<?>> allClasses = new ArrayList<>(); allClasses.add(annotatedClass); allClasses.addAll(ClassUtils.getAllSuperclasses(annotatedClass)); final ArrayList<Annotation> allAnnotations = new ArrayList<>(); for (final Class<?> aClass : allClasses) { addIfNotPresent(allAnnotations, getAnnotations(aClass)); final List<Class<?>> interfaces = ClassUtils.getAllInterfaces(aClass); for (final Class<?> anInterface : interfaces) { addIfNotPresent(allAnnotations, getAnnotations(anInterface)); } } return allAnnotations.toArray(new Annotation[] {}); }
From source file:io.fabric8.vertx.maven.plugin.mojos.AbstractRunMojo.java
/** * Method to check if the Launcher is {@link AbstractRunMojo#IO_VERTX_CORE_LAUNCHER} or instance of * {@code io.vertx.core.Launcher}//from w ww .j a v a 2 s . com * * @param launcher - the launcher class as string that needs to be checked * @return true if its {@link AbstractRunMojo#IO_VERTX_CORE_LAUNCHER} or instance of * {@code io.vertx.core.Launcher} * @throws MojoExecutionException - any error that might occur while checking */ protected boolean isVertxLauncher(String launcher) throws MojoExecutionException { if (launcher != null) { if (IO_VERTX_CORE_LAUNCHER.equals(launcher)) { return true; } else { try { Class customLauncher = buildClassLoader(getClassPathUrls()).loadClass(launcher); List<Class<?>> superClasses = ClassUtils.getAllSuperclasses(customLauncher); boolean isAssignable = false; if (superClasses != null) { for (Class<?> superClass : superClasses) { if (IO_VERTX_CORE_LAUNCHER.equals(superClass.getName())) { isAssignable = true; break; } } } return isAssignable; } catch (ClassNotFoundException e) { throw new MojoExecutionException("Class \"" + launcher + "\" not found"); } } } else { return false; } }
From source file:io.github.moosbusch.lumpi.util.LumpiUtil.java
public static Set<Class<?>> getSuperTypes(Class<?> type, boolean includeType, boolean includeSuperClasses, boolean includeInterfaces) { Set<Class<?>> result = new LinkedHashSet<>(); List<Class<?>> interfaces = ClassUtils.getAllInterfaces(type); List<Class<?>> superClasses = ClassUtils.getAllSuperclasses(type); if (includeType) { result.add(type);/* w ww . jav a 2 s . c o m*/ } if (includeSuperClasses) { result.addAll(superClasses); } if (includeInterfaces) { result.addAll(interfaces); } return result; }
From source file:org.apache.syncope.client.console.wizards.AbstractMappingPanel.java
private static void initFieldNames(final Class<?> entityClass, final Set<String> keys) { List<Class<?>> classes = ClassUtils.getAllSuperclasses(entityClass); classes.add(entityClass);// www.jav a 2 s . c om classes.forEach(clazz -> { for (Field field : clazz.getDeclaredFields()) { if (!Modifier.isStatic(field.getModifiers()) && !Collection.class.isAssignableFrom(field.getType()) && !Map.class.isAssignableFrom(field.getType())) { keys.add(field.getName()); } } }); }
From source file:org.apache.syncope.client.console.wizards.resources.ResourceMappingPanel.java
private static void initFieldNames(final Class<?> entityClass, final Set<String> keys) { List<Class<?>> classes = ClassUtils.getAllSuperclasses(entityClass); classes.add(entityClass);/*w ww . ja va 2 s. com*/ for (Class<?> clazz : classes) { for (Field field : clazz.getDeclaredFields()) { if (!Modifier.isStatic(field.getModifiers()) && !Collection.class.isAssignableFrom(field.getType()) && !Map.class.isAssignableFrom(field.getType())) { keys.add(field.getName()); } } } }