List of usage examples for org.apache.commons.lang3 ClassUtils getAllInterfaces
public static List<Class<?>> getAllInterfaces(final Class<?> cls)
Gets a List of all interfaces implemented by the given class and its superclasses.
The order is determined by looking through each interface in turn as declared in the source file and following its hierarchy up.
From source file:org.apache.nifi.authorization.UserGroupProviderFactory.java
public static UserGroupProvider withNarLoader(final UserGroupProvider baseUserGroupProvider, final ClassLoader classLoader) { final UserGroupProviderInvocationHandler invocationHandler = new UserGroupProviderInvocationHandler( baseUserGroupProvider, classLoader); // extract all interfaces... baseUserGroupProvider is non null so getAllInterfaces is non null final List<Class<?>> interfaceList = ClassUtils.getAllInterfaces(baseUserGroupProvider.getClass()); final Class<?>[] interfaces = interfaceList.toArray(new Class<?>[interfaceList.size()]); return (UserGroupProvider) Proxy.newProxyInstance(classLoader, interfaces, invocationHandler); }
From source file:org.apache.nifi.controller.ExtensionBuilder.java
private ControllerServiceNode createControllerServiceNode() throws ClassNotFoundException, IllegalAccessException, InstantiationException, InitializationException { final ClassLoader ctxClassLoader = Thread.currentThread().getContextClassLoader(); try {/*from w ww. j a v a 2s . c om*/ final Bundle bundle = extensionManager.getBundle(bundleCoordinate); if (bundle == null) { throw new IllegalStateException( "Unable to find bundle for coordinate " + bundleCoordinate.getCoordinate()); } final ClassLoader detectedClassLoader = extensionManager.createInstanceClassLoader(type, identifier, bundle, classpathUrls == null ? Collections.emptySet() : classpathUrls); final Class<?> rawClass = Class.forName(type, true, detectedClassLoader); Thread.currentThread().setContextClassLoader(detectedClassLoader); final Class<? extends ControllerService> controllerServiceClass = rawClass .asSubclass(ControllerService.class); final ControllerService serviceImpl = controllerServiceClass.newInstance(); final StandardControllerServiceInvocationHandler invocationHandler = new StandardControllerServiceInvocationHandler( extensionManager, serviceImpl); // extract all interfaces... controllerServiceClass is non null so getAllInterfaces is non null final List<Class<?>> interfaceList = ClassUtils.getAllInterfaces(controllerServiceClass); final Class<?>[] interfaces = interfaceList.toArray(new Class<?>[0]); final ControllerService proxiedService; if (detectedClassLoader == null) { proxiedService = (ControllerService) Proxy.newProxyInstance(getClass().getClassLoader(), interfaces, invocationHandler); } else { proxiedService = (ControllerService) Proxy.newProxyInstance(detectedClassLoader, interfaces, invocationHandler); } logger.info("Created Controller Service of type {} with identifier {}", type, identifier); final ComponentLog serviceLogger = new SimpleProcessLogger(identifier, serviceImpl); final TerminationAwareLogger terminationAwareLogger = new TerminationAwareLogger(serviceLogger); final StateManager stateManager = stateManagerProvider.getStateManager(identifier); final ControllerServiceInitializationContext initContext = new StandardControllerServiceInitializationContext( identifier, terminationAwareLogger, serviceProvider, stateManager, kerberosConfig); serviceImpl.initialize(initContext); final LoggableComponent<ControllerService> originalLoggableComponent = new LoggableComponent<>( serviceImpl, bundleCoordinate, terminationAwareLogger); final LoggableComponent<ControllerService> proxiedLoggableComponent = new LoggableComponent<>( proxiedService, bundleCoordinate, terminationAwareLogger); final ComponentVariableRegistry componentVarRegistry = new StandardComponentVariableRegistry( this.variableRegistry); final ValidationContextFactory validationContextFactory = new StandardValidationContextFactory( serviceProvider, componentVarRegistry); final ControllerServiceNode serviceNode = new StandardControllerServiceNode(originalLoggableComponent, proxiedLoggableComponent, invocationHandler, identifier, validationContextFactory, serviceProvider, componentVarRegistry, reloadComponent, extensionManager, validationTrigger); serviceNode.setName(rawClass.getSimpleName()); invocationHandler.setServiceNode(serviceNode); return serviceNode; } finally { if (ctxClassLoader != null) { Thread.currentThread().setContextClassLoader(ctxClassLoader); } } }
From source file:org.apache.nifi.registry.flow.mapping.NiFiRegistryFlowMapper.java
private List<ControllerServiceAPI> mapControllerServiceApis(final ControllerServiceNode service) { final Class<?> serviceClass = service.getControllerServiceImplementation().getClass(); final Set<Class<?>> serviceApiClasses = new HashSet<>(); // get all of it's interfaces to determine the controller service api's it implements final List<Class<?>> interfaces = ClassUtils.getAllInterfaces(serviceClass); for (final Class<?> i : interfaces) { // add all controller services that's not ControllerService itself if (ControllerService.class.isAssignableFrom(i) && !ControllerService.class.equals(i)) { serviceApiClasses.add(i);/* w ww . j a v a 2 s. co m*/ } } final List<ControllerServiceAPI> serviceApis = new ArrayList<>(); for (final Class<?> serviceApiClass : serviceApiClasses) { final BundleCoordinate bundleCoordinate = extensionManager.getBundle(serviceApiClass.getClassLoader()) .getBundleDetails().getCoordinate(); final ControllerServiceAPI serviceApi = new ControllerServiceAPI(); serviceApi.setType(serviceApiClass.getName()); serviceApi.setBundle(mapBundle(bundleCoordinate)); serviceApis.add(serviceApi); } return serviceApis; }
From source file:org.apache.nifi.web.controller.ControllerFacade.java
/** * Returns whether the specified type implements the specified serviceType. * * @param serviceType type// ww w .java 2 s . co m * @param type type * @return whether the specified type implements the specified serviceType */ private boolean implementsServiceType(final String serviceType, final Class type) { final List<Class<?>> interfaces = ClassUtils.getAllInterfaces(type); for (final Class i : interfaces) { if (ControllerService.class.isAssignableFrom(i) && i.getName().equals(serviceType)) { return true; } } return false; }
From source file:org.apache.syncope.core.persistence.jpa.validation.entity.EntityValidationListener.java
@PrePersist @PreUpdate// w ww. jav a 2s . c o m public void validate(final Object object) { final Validator validator = ApplicationContextProvider.getBeanFactory().getBean(Validator.class); Set<ConstraintViolation<Object>> violations = validator.validate(object); if (!violations.isEmpty()) { LOG.warn("Bean validation errors found: {}", violations); Class<?> entityInt = null; for (Class<?> interf : ClassUtils.getAllInterfaces(object.getClass())) { if (!Entity.class.equals(interf) && !AnnotatedEntity.class.equals(interf) && !ProvidedKeyEntity.class.equals(interf) && !Schema.class.equals(interf) && !Task.class.equals(interf) && !Policy.class.equals(interf) && !GroupableRelatable.class.equals(interf) && !Any.class.equals(interf) && !DynMembership.class.equals(interf) && Entity.class.isAssignableFrom(interf)) { entityInt = interf; } } throw new InvalidEntityException(entityInt == null ? "Entity" : entityInt.getSimpleName(), violations); } }
From source file:org.ballerinalang.composer.service.ballerina.parser.service.BallerinaParserService.java
public static JsonElement generateJSON(Node node, Map<String, Node> anonStructs) throws InvocationTargetException, IllegalAccessException { if (node == null) { return JsonNull.INSTANCE; }//w ww . j a va2s. c o m Set<Method> methods = ClassUtils.getAllInterfaces(node.getClass()).stream() .flatMap(aClass -> Arrays.stream(aClass.getMethods())).collect(Collectors.toSet()); JsonObject nodeJson = new JsonObject(); JsonArray wsJsonArray = new JsonArray(); Set<Whitespace> ws = node.getWS(); if (ws != null && !ws.isEmpty()) { for (Whitespace whitespace : ws) { JsonObject wsJson = new JsonObject(); wsJson.addProperty("ws", whitespace.getWs()); wsJson.addProperty("i", whitespace.getIndex()); wsJson.addProperty("text", whitespace.getPrevious()); wsJson.addProperty("static", whitespace.isStatic()); wsJsonArray.add(wsJson); } nodeJson.add("ws", wsJsonArray); } Diagnostic.DiagnosticPosition position = node.getPosition(); if (position != null) { JsonObject positionJson = new JsonObject(); positionJson.addProperty("startColumn", position.getStartColumn()); positionJson.addProperty("startLine", position.getStartLine()); positionJson.addProperty("endColumn", position.getEndColumn()); positionJson.addProperty("endLine", position.getEndLine()); nodeJson.add("position", positionJson); } /* Virtual props */ JsonArray type = getType(node); if (type != null) { nodeJson.add(SYMBOL_TYPE, type); } if (node.getKind() == NodeKind.INVOCATION) { assert node instanceof BLangInvocation : node.getClass(); BLangInvocation invocation = (BLangInvocation) node; if (invocation.symbol != null && invocation.symbol.kind != null) { nodeJson.addProperty(INVOCATION_TYPE, invocation.symbol.kind.toString()); } } for (Method m : methods) { String name = m.getName(); if (name.equals("getWS") || name.equals("getPosition")) { continue; } String jsonName; if (name.startsWith("get")) { jsonName = toJsonName(name, 3); } else if (name.startsWith("is")) { jsonName = toJsonName(name, 2); } else { continue; } Object prop = m.invoke(node); /* Literal class - This class is escaped in backend to address cases like "ss\"" and 8.0 and null */ if (node.getKind() == NodeKind.LITERAL && "value".equals(jsonName)) { if (prop instanceof String) { nodeJson.addProperty(jsonName, '"' + StringEscapeUtils.escapeJava((String) prop) + '"'); nodeJson.addProperty(UNESCAPED_VALUE, String.valueOf(prop)); } else { nodeJson.addProperty(jsonName, String.valueOf(prop)); } continue; } if (node.getKind() == NodeKind.ANNOTATION && node instanceof BLangAnnotation) { JsonArray attachmentPoints = new JsonArray(); ((BLangAnnotation) node).getAttachPoints().stream().map(AttachPoint::getValue) .map(JsonPrimitive::new).forEach(attachmentPoints::add); nodeJson.add("attachmentPoints", attachmentPoints); } // TODO: revisit logic for user defined types // if (node.getKind() == NodeKind.USER_DEFINED_TYPE && jsonName.equals("typeName")) { // IdentifierNode typeNode = (IdentifierNode) prop; // Node structNode; // if (typeNode.getValue().startsWith("$anonStruct$") && // (structNode = anonStructs.remove(typeNode.getValue())) != null) { // JsonObject anonStruct = generateJSON(structNode, anonStructs).getAsJsonObject(); // anonStruct.addProperty("anonStruct", true); // nodeJson.add("anonStruct", anonStruct); // continue; // } // } if (prop instanceof List && jsonName.equals("types")) { // Currently we don't need any Symbols for the UI. So skipping for now. continue; } /* Node classes */ if (prop instanceof Node) { nodeJson.add(jsonName, generateJSON((Node) prop, anonStructs)); } else if (prop instanceof List) { List listProp = (List) prop; JsonArray listPropJson = new JsonArray(); nodeJson.add(jsonName, listPropJson); for (Object listPropItem : listProp) { if (listPropItem instanceof Node) { /* Remove top level anon func and struct */ if (node.getKind() == NodeKind.COMPILATION_UNIT) { if (listPropItem instanceof BLangFunction && (((BLangFunction) listPropItem)).name.value.startsWith("$lambda$")) { continue; } } listPropJson.add(generateJSON((Node) listPropItem, anonStructs)); } else { logger.debug("Can't serialize " + jsonName + ", has a an array of " + listPropItem); } } /* Runtime model classes */ } else if (prop instanceof Set && jsonName.equals("flags")) { Set flags = (Set) prop; for (Flag flag : Flag.values()) { nodeJson.addProperty(StringUtils.lowerCase(flag.toString()), flags.contains(flag)); } } else if (prop instanceof Set) { // TODO : limit this else if to getInputs getOutputs of transform. Set vars = (Set) prop; JsonArray listVarJson = new JsonArray(); nodeJson.add(jsonName, listVarJson); for (Object obj : vars) { listVarJson.add(obj.toString()); } } else if (prop instanceof NodeKind) { String kindName = CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, prop.toString()); nodeJson.addProperty(jsonName, kindName); } else if (prop instanceof OperatorKind) { nodeJson.addProperty(jsonName, prop.toString()); /* Generic classes */ } else if (prop instanceof String) { nodeJson.addProperty(jsonName, (String) prop); } else if (prop instanceof Number) { nodeJson.addProperty(jsonName, (Number) prop); } else if (prop instanceof Boolean) { nodeJson.addProperty(jsonName, (Boolean) prop); } else if (prop instanceof Enum) { nodeJson.addProperty(jsonName, StringUtils.lowerCase(((Enum) prop).name())); } else if (prop instanceof int[]) { int[] intArray = ((int[]) prop); JsonArray intArrayPropJson = new JsonArray(); nodeJson.add(jsonName, intArrayPropJson); for (int intProp : intArray) { intArrayPropJson.add(intProp); } } else if (prop != null) { nodeJson.addProperty(jsonName, prop.toString()); String message = "Node " + node.getClass().getSimpleName() + " contains unknown type prop: " + jsonName + " of type " + prop.getClass(); logger.error(message); } } return nodeJson; }
From source file:org.ballerinalang.langserver.compiler.format.TextDocumentFormatUtil.java
/** * Generate json representation for the given node. * * @param node Node to get the json representation * @param anonStructs Map of anonymous structs * @param symbolMetaInfoMap symbol meta information map * @return {@link JsonElement} Json Representation of the node * @throws JSONGenerationException when Json error occurs *///from w ww . j a v a2 s . com public static JsonElement generateJSON(Node node, Map<String, Node> anonStructs, Map<BLangNode, List<SymbolMetaInfo>> symbolMetaInfoMap) throws JSONGenerationException { if (node == null) { return JsonNull.INSTANCE; } Set<Method> methods = ClassUtils.getAllInterfaces(node.getClass()).stream() .flatMap(aClass -> Arrays.stream(aClass.getMethods())).collect(Collectors.toSet()); JsonObject nodeJson = new JsonObject(); JsonArray wsJsonArray = new JsonArray(); Set<Whitespace> ws = node.getWS(); if (ws != null && !ws.isEmpty()) { for (Whitespace whitespace : ws) { JsonObject wsJson = new JsonObject(); wsJson.addProperty("ws", whitespace.getWs()); wsJson.addProperty("i", whitespace.getIndex()); wsJson.addProperty("text", whitespace.getPrevious()); wsJson.addProperty("static", whitespace.isStatic()); wsJsonArray.add(wsJson); } nodeJson.add("ws", wsJsonArray); } Diagnostic.DiagnosticPosition position = node.getPosition(); if (position != null) { JsonObject positionJson = new JsonObject(); positionJson.addProperty("startColumn", position.getStartColumn()); positionJson.addProperty("startLine", position.getStartLine()); positionJson.addProperty("endColumn", position.getEndColumn()); positionJson.addProperty("endLine", position.getEndLine()); nodeJson.add("position", positionJson); } /* Virtual props */ // Add UUID for each node. nodeJson.addProperty("id", UUID.randomUUID().toString()); // Add the visible endpoints for a given node if (symbolMetaInfoMap.containsKey(node)) { List<SymbolMetaInfo> endpointMetaList = symbolMetaInfoMap.get(node); JsonArray endpoints = new JsonArray(); endpointMetaList.forEach(symbolMetaInfo -> endpoints.add(symbolMetaInfo.getJson())); nodeJson.add("VisibleEndpoints", endpoints); } JsonArray type = getType(node); if (type != null) { nodeJson.add(SYMBOL_TYPE, type); } if (node.getKind() == NodeKind.INVOCATION) { assert node instanceof BLangInvocation : node.getClass(); BLangInvocation invocation = (BLangInvocation) node; if (invocation.symbol != null && invocation.symbol.kind != null) { nodeJson.addProperty(INVOCATION_TYPE, invocation.symbol.kind.toString()); } } for (Method m : methods) { String name = m.getName(); if (name.equals("getWS") || name.equals("getPosition")) { continue; } String jsonName; if (name.startsWith("get")) { jsonName = toJsonName(name, 3); } else if (name.startsWith("is")) { jsonName = toJsonName(name, 2); } else { continue; } Object prop = null; try { prop = m.invoke(node); } catch (IllegalAccessException | InvocationTargetException e) { throw new JSONGenerationException("Error occurred while generating JSON", e); } /* Literal class - This class is escaped in backend to address cases like "ss\"" and 8.0 and null */ if ((node.getKind() == NodeKind.LITERAL || node.getKind() == NodeKind.NUMERIC_LITERAL) && "value".equals(jsonName)) { if (prop instanceof String) { nodeJson.addProperty(jsonName, '"' + StringEscapeUtils.escapeJava((String) prop) + '"'); nodeJson.addProperty(UNESCAPED_VALUE, String.valueOf(prop)); } else { nodeJson.addProperty(jsonName, String.valueOf(prop)); } continue; } if (node.getKind() == NodeKind.ANNOTATION && node instanceof BLangAnnotation) { JsonArray attachmentPoints = new JsonArray(); ((BLangAnnotation) node).getAttachPoints().stream().map(AttachPoint::getValue) .map(JsonPrimitive::new).forEach(attachmentPoints::add); nodeJson.add("attachmentPoints", attachmentPoints); } if (prop instanceof List && jsonName.equals("types")) { // Currently we don't need any Symbols for the UI. So skipping for now. continue; } /* Node classes */ if (prop instanceof Node) { nodeJson.add(jsonName, generateJSON((Node) prop, anonStructs, symbolMetaInfoMap)); } else if (prop instanceof List) { List listProp = (List) prop; JsonArray listPropJson = new JsonArray(); nodeJson.add(jsonName, listPropJson); for (Object listPropItem : listProp) { if (listPropItem instanceof Node) { /* Remove top level anon func and struct */ if (node.getKind() == NodeKind.COMPILATION_UNIT) { if (listPropItem instanceof BLangFunction && (((BLangFunction) listPropItem)).name.value.startsWith("$lambda$")) { continue; } } listPropJson.add(generateJSON((Node) listPropItem, anonStructs, symbolMetaInfoMap)); } else if (listPropItem instanceof BLangRecordVarRef.BLangRecordVarRefKeyValue) { listPropJson.add(generateJSON( ((BLangRecordVarRef.BLangRecordVarRefKeyValue) listPropItem).getVariableName(), anonStructs, symbolMetaInfoMap)); listPropJson.add(generateJSON( ((BLangRecordVarRef.BLangRecordVarRefKeyValue) listPropItem).getBindingPattern(), anonStructs, symbolMetaInfoMap)); } else if (listPropItem instanceof BLangRecordVariable.BLangRecordVariableKeyValue) { listPropJson.add(generateJSON( ((BLangRecordVariable.BLangRecordVariableKeyValue) listPropItem).getKey(), anonStructs, symbolMetaInfoMap)); listPropJson.add(generateJSON( ((BLangRecordVariable.BLangRecordVariableKeyValue) listPropItem).getValue(), anonStructs, symbolMetaInfoMap)); } else if (listPropItem instanceof String) { listPropJson.add((String) listPropItem); } else { logger.debug("Can't serialize " + jsonName + ", has a an array of " + listPropItem); } } /* Runtime model classes */ } else if (prop instanceof Set && jsonName.equals("flags")) { Set flags = (Set) prop; for (Flag flag : Flag.values()) { nodeJson.addProperty(StringUtils.lowerCase(flag.toString()), flags.contains(flag)); } } else if (prop instanceof Set) { // TODO : limit this else if to getInputs getOutputs of transform. Set vars = (Set) prop; JsonArray listVarJson = new JsonArray(); nodeJson.add(jsonName, listVarJson); for (Object obj : vars) { listVarJson.add(obj.toString()); } } else if (prop instanceof NodeKind) { String kindName = CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, prop.toString()); nodeJson.addProperty(jsonName, kindName); } else if (prop instanceof OperatorKind) { nodeJson.addProperty(jsonName, prop.toString()); /* Generic classes */ } else if (prop instanceof String) { nodeJson.addProperty(jsonName, (String) prop); } else if (prop instanceof Number) { nodeJson.addProperty(jsonName, (Number) prop); } else if (prop instanceof Boolean) { nodeJson.addProperty(jsonName, (Boolean) prop); } else if (prop instanceof Enum) { nodeJson.addProperty(jsonName, StringUtils.lowerCase(((Enum) prop).name())); } else if (prop instanceof int[]) { int[] intArray = ((int[]) prop); JsonArray intArrayPropJson = new JsonArray(); nodeJson.add(jsonName, intArrayPropJson); for (int intProp : intArray) { intArrayPropJson.add(intProp); } } else if (prop != null) { nodeJson.addProperty(jsonName, prop.toString()); } } return nodeJson; }
From source file:org.bonitasoft.engine.business.data.impl.jackson.EntityBeanSerializerModifier.java
private static boolean shouldBeIgnored(BeanDescription beanDescription) { JavaType type = beanDescription.getType(); Class<?> rawClass = type.getRawClass(); if (LOG.isTraceEnabled()) { LOG.trace("Checking if it has to be ignored - {} / {}", type, rawClass); LOG.trace("Interfaces: {}", getNames(ClassUtils.getAllInterfaces(rawClass))); LOG.trace("Superclasses: {}", getNames(ClassUtils.getAllSuperclasses(rawClass))); }/*from w w w . jav a2 s .c o m*/ if (MethodHandler.class.isAssignableFrom(rawClass) || Proxy.class.isAssignableFrom(rawClass)) { return true; } return false; }
From source file:org.evosuite.utils.generic.GenericClass.java
/** * <p>//from ww w . j a v a 2 s. c om * isSubclass * </p> * * @param superclass * a {@link java.lang.reflect.Type} object. * @param subclass * a {@link java.lang.reflect.Type} object. * @return a boolean. */ public static boolean isSubclass(Type superclass, Type subclass) { List<Class<?>> superclasses = ClassUtils.getAllSuperclasses((Class<?>) subclass); List<Class<?>> interfaces = ClassUtils.getAllInterfaces((Class<?>) subclass); if (superclasses.contains(superclass) || interfaces.contains(superclass)) { return true; } return false; }
From source file:org.jboss.qa.phaser.InstanceRegistry.java
public static void insert(Object o) { insert(o, o.getClass());/*from ww w . jav a 2s .c o m*/ insert(o, ClassUtils.getAllInterfaces(o.getClass())); insert(o, ClassUtils.getAllSuperclasses(o.getClass())); }