List of usage examples for java.lang.reflect Field getDeclaredAnnotation
@Override public <T extends Annotation> T getDeclaredAnnotation(Class<T> annotationClass)
From source file:com.m4rc310.cb.builders.ComponentBuilder.java
private void printDialog() { for (Field field : fields) { // fields.stream().forEach((field) -> { Acomponent ac = field.getDeclaredAnnotation(Acomponent.class); AbstractComponetAdapter adapter = adapters.get(field); Object target = getTargetForField(field); Object containner = containers.get(target.hashCode()); if (!ac.tabFor().isEmpty()) { Object jtp = getJTabbedPane(ac.tabFor()); String text = getString(ac.text()); MethodUtils.method(jtp, "addTab", String.class, Component.class).invoke(text, adapter.getComponent()); continue; }//w w w . j a va 2 s. c o m if (!ac.label().isEmpty()) { JLabel jLabel = gui.getJLabel(ac.label(), adapter.getComponent()); MethodUtils.method(containner, "add", Component.class, Object.class).invoke(jLabel, ac.layoutLabel()); mapLabels.put(ac.ref(), jLabel); } MethodUtils.method(containner, "add", Component.class, Object.class).invoke(adapter.getComponent(), ac.layout()); } }
From source file:com.m4rc310.cb.builders.ComponentBuilder.java
public Object getComponent(String ref) { for (Map.Entry<Field, AbstractComponetAdapter> entrySet : adapters.entrySet()) { Field field = entrySet.getKey(); AbstractComponetAdapter adapter = entrySet.getValue(); Acomponent ac = field.getDeclaredAnnotation(Acomponent.class); if (ref.equals(ac.ref())) { Object component = adapter.getComponent(); return component; }// w w w .ja v a2s .c o m } return null; }
From source file:com.m4rc310.cb.builders.ComponentBuilder.java
private void buildAllComponents() { for (Map.Entry<Field, AbstractComponetAdapter> entrySet : adapters.entrySet()) { Field field = entrySet.getKey(); Acomponent ac = field.getDeclaredAnnotation(Acomponent.class); AbstractComponetAdapter adapter = entrySet.getValue(); Object target = getTargetForField(field); adapter.setComponentsBuilder(this); adapter.build(field, target);/*w w w . j av a2 s.com*/ Adialog adialog = objectAnnotated.getClass().getDeclaredAnnotation(Adialog.class); if (adialog.debug()) { try { Object component = adapter.getComponent(); StringBuilder sgroup = new StringBuilder(); for (String group : ac.groups()) { sgroup.append("[ "); sgroup.append(group); sgroup.append(" ]"); } String toolTipText = String.format("Name: %s - ToolTipText: %s - Groups: %s", ac.ref(), ac.toolTipText(), sgroup); MethodUtils.method(component, "setToolTipText", String.class).invoke(toolTipText); } catch (Exception e) { infoError(e); } } else { Object component = adapter.getComponent(); MethodUtils.method(component, "setToolTipText", String.class).invoke(getString(ac.toolTipText())); } try { Object value = field.get(target); adapter.update(field, value); if (ac.type().equals(EnumComponentType.PANEL)) { Object component = adapter.getComponent(); // Object[] components = ((JPanel) component).getComponents(); // // for (Object c : components) { // boolean bvalue = (boolean) MethodUtils.method(c, "isEnabled").invoke(); // mc.put(c, bvalue); // } PropertyChangeListener pcl = new PropertyChangeListener() { Map<Object, Boolean> mc = new HashMap<>(); @Override public void propertyChange(PropertyChangeEvent evt) { Object[] components = ((JPanel) component).getComponents(); if (evt.getPropertyName().equals("enabled")) { boolean enable = (boolean) evt.getNewValue(); for (Object com : components) { if (!mc.containsKey(com)) { boolean bvalue = (boolean) MethodUtils.method(com, "isEnabled").invoke(); mc.put(com, bvalue); } if (enable) { MethodUtils.method(com, "setEnabled", boolean.class).invoke(mc.get(com)); } else { MethodUtils.method(com, "setEnabled", boolean.class).invoke(enable); } } } } }; MethodUtils.method(component, "addPropertyChangeListener", PropertyChangeListener.class) .invoke(pcl); putContainer(value.hashCode(), component); } } catch (IllegalArgumentException | IllegalAccessException e) { infoError(e); throw new UnsupportedOperationException(e); } } }
From source file:com.m4rc310.cb.builders.ComponentBuilder.java
public Object getJTabbedPane(String name) { for (Map.Entry<Field, AbstractComponetAdapter> entrySet : adapters.entrySet()) { Field field = entrySet.getKey(); AbstractComponetAdapter adapter = entrySet.getValue(); if (adapter instanceof AdapterTabPanel) { Acomponent ac = field.getDeclaredAnnotation(Acomponent.class); if (ac.name().equals(name)) { return adapter.getComponent(); }//from ww w.j a v a 2s . c o m } } throw new UnsupportedOperationException("JTabbedPane no encontrado: " + name); }
From source file:com.m4rc310.cb.builders.ComponentBuilder.java
private void loadAllFields(Object object, Class<?> type) { for (Field field : type.getDeclaredFields()) { if (!field.isAnnotationPresent(Acomponent.class)) { continue; }//w ww. ja v a 2 s . c o m field.setAccessible(true); Acomponent ac = field.getDeclaredAnnotation(Acomponent.class); if (field.getClass().getDeclaredFields().length > 0) { Object value; try { value = field.get(object); if (ac.type().equals(EnumComponentType.PANEL)) { addTargets(value); loadAllFields(value, field.getType()); } mudarReferencia(field); addField(field); } catch (IllegalArgumentException | IllegalAccessException e) { infoError(e); LogServer.getInstance().error(null, "O <field> [{0}] est nulo!\nDialog foi iniciado de forma incompleta com falhas!", field); } } } }
From source file:com.angkorteam.framework.swagger.factory.SwaggerFactory.java
@Override public void afterPropertiesSet() throws Exception { Swagger swagger = new Swagger(); io.swagger.models.Info info = new io.swagger.models.Info(); swagger.setInfo(info);/*from ww w. j a va 2 s . c o m*/ info.setTitle(title); info.setLicense(license); info.setContact(contact); info.setTermsOfService(termsOfService); info.setVersion(version); info.setDescription(description); swagger.setConsumes(consumes); swagger.setProduces(produces); swagger.setSchemes(schemes); swagger.setBasePath(basePath); swagger.setHost(host); swagger.setExternalDocs(externalDocs); ConfigurationBuilder config = new ConfigurationBuilder(); Set<String> acceptablePackages = new HashSet<>(); boolean allowAllPackages = false; if (resourcePackages != null && resourcePackages.length > 0) { for (String resourcePackage : resourcePackages) { if (resourcePackage != null && !"".equals(resourcePackage)) { acceptablePackages.add(resourcePackage); config.addUrls(ClasspathHelper.forPackage(resourcePackage)); } } } config.setScanners(new ResourcesScanner(), new TypeAnnotationsScanner(), new SubTypesScanner()); Reflections reflections = new Reflections(config); Set<Class<?>> controllers = reflections.getTypesAnnotatedWith(Controller.class); Set<Class<?>> output = new HashSet<Class<?>>(); for (Class<?> cls : controllers) { if (allowAllPackages) { output.add(cls); } else { for (String pkg : acceptablePackages) { if (cls.getPackage().getName().startsWith(pkg)) { output.add(cls); } } } } Map<String, Path> paths = new HashMap<>(); swagger.setPaths(paths); Map<String, Model> definitions = new HashMap<>(); swagger.setDefinitions(definitions); Stack<Class<?>> modelStack = new Stack<>(); for (Class<?> controller : controllers) { List<String> clazzPaths = new ArrayList<>(); RequestMapping clazzRequestMapping = controller.getDeclaredAnnotation(RequestMapping.class); Api api = controller.getDeclaredAnnotation(Api.class); if (clazzRequestMapping != null) { clazzPaths = lookPaths(clazzRequestMapping); } if (clazzPaths.isEmpty()) { clazzPaths.add(""); } if (api != null) { if (!"".equals(api.description())) { for (String name : api.tags()) { if (!"".equals(name)) { io.swagger.models.Tag tag = new io.swagger.models.Tag(); tag.setDescription(api.description()); tag.setName(name); swagger.addTag(tag); } } } } else { io.swagger.models.Tag tag = new io.swagger.models.Tag(); tag.setDescription("Unknown"); tag.setName("Unknown"); swagger.addTag(tag); } Method[] methods = null; try { methods = controller.getDeclaredMethods(); } catch (NoClassDefFoundError e) { } if (methods != null && methods.length > 0) { for (Method method : methods) { RequestMapping requestMapping = method.getAnnotation(RequestMapping.class); ApiOperation apiOperation = method.getAnnotation(ApiOperation.class); ApiResponses apiResponses = method.getAnnotation(ApiResponses.class); ApiHeaders apiHeaders = method.getAnnotation(ApiHeaders.class); List<String> methodPaths = new ArrayList<>(); if (requestMapping != null && apiOperation != null && apiResponses != null) { methodPaths = lookPaths(requestMapping); } if (methodPaths.isEmpty()) { methodPaths.add(""); } if (requestMapping != null && apiOperation != null && apiResponses != null) { for (String classPath : clazzPaths) { for (String methodPath : methodPaths) { RequestMethod[] requestMethods = requestMapping.method(); if (requestMethods == null || requestMethods.length == 0) { requestMethods = RequestMethod.values(); } Path path = new Path(); paths.put(classPath + methodPath, path); for (RequestMethod requestMethod : requestMethods) { Operation operation = new Operation(); operation.setDescription(apiOperation.description()); for (String consume : requestMapping.consumes()) { operation.addConsumes(consume); } for (String produce : requestMapping.produces()) { operation.addProduces(produce); } if (api != null) { if (!"".equals(api.description())) { for (String name : api.tags()) { if (!"".equals(name)) { operation.addTag(name); } } } } else { io.swagger.models.Tag tag = new io.swagger.models.Tag(); operation.addTag("Unknown"); } if (requestMethod == RequestMethod.DELETE) { path.delete(operation); } else if (requestMethod == RequestMethod.GET) { path.get(operation); } else if (requestMethod == RequestMethod.HEAD) { path.head(operation); } else if (requestMethod == RequestMethod.OPTIONS) { path.options(operation); } else if (requestMethod == RequestMethod.PATCH) { path.patch(operation); } else if (requestMethod == RequestMethod.POST) { path.post(operation); } else if (requestMethod == RequestMethod.PUT) { path.put(operation); } if (apiHeaders != null && apiHeaders.value() != null && apiHeaders.value().length > 0) { for (ApiHeader header : apiHeaders.value()) { HeaderParameter parameter = new HeaderParameter(); parameter.setName(header.name()); parameter.setType("string"); parameter.setDescription(header.description()); parameter.setRequired(header.required()); operation.addParameter(parameter); } } for (Parameter parameter : method.getParameters()) { PathVariable pathVariable = parameter.getAnnotation(PathVariable.class); RequestParam requestParam = parameter.getAnnotation(RequestParam.class); RequestBody requestBody = parameter.getAnnotation(RequestBody.class); RequestPart requestPart = parameter.getAnnotation(RequestPart.class); ApiParam apiParam = parameter.getAnnotation(ApiParam.class); if (apiParam != null && pathVariable != null && isSimpleScalar(parameter.getType())) { PathParameter pathParameter = new PathParameter(); pathParameter.setRequired(true); pathParameter.setDescription(apiParam.description()); pathParameter.setType(lookupType(parameter.getType())); pathParameter.setFormat(lookupFormat(parameter.getType(), apiParam)); pathParameter.setName(pathVariable.value()); operation.addParameter(pathParameter); continue; } if (requestMethod == RequestMethod.DELETE || requestMethod == RequestMethod.GET || requestMethod == RequestMethod.HEAD || requestMethod == RequestMethod.OPTIONS || requestMethod == RequestMethod.PATCH || requestMethod == RequestMethod.PUT) { if (apiParam != null && requestParam != null && isSimpleArray(parameter.getType())) { QueryParameter param = new QueryParameter(); param.setRequired(requestParam.required()); param.setDescription(apiParam.description()); param.setType("array"); if (!"".equals(requestParam.value())) { param.setName(requestParam.value()); } if (!"".equals(requestParam.name())) { param.setName(requestParam.name()); } param.setItems(lookupProperty(parameter.getType(), requestParam, apiParam)); operation.addParameter(param); continue; } if (apiParam != null && requestParam != null && isSimpleScalar(parameter.getType())) { QueryParameter param = new QueryParameter(); param.setRequired(requestParam.required()); param.setDescription(apiParam.description()); param.setType(lookupType(parameter.getType())); param.setFormat(lookupFormat(parameter.getType(), apiParam)); if (!"".equals(requestParam.value())) { param.setName(requestParam.value()); } if (!"".equals(requestParam.name())) { param.setName(requestParam.name()); } operation.addParameter(param); continue; } if (apiParam != null && requestBody != null && parameter.getType() == MultipartFile.class) { FormParameter param = new FormParameter(); param.setRequired(true); param.setIn("body"); param.setName("body"); param.setType("file"); param.setDescription(apiParam.description()); operation.addConsumes("application/octet-stream"); // BodyParameter param = new BodyParameter(); // param.setRequired(requestBody.required()); // param.setDescription(apiParam.description()); // param.setName("body"); // ModelImpl model = new ModelImpl(); // model.setType("file"); // param.setSchema(model); operation.addParameter(param); continue; } if (apiParam != null && requestBody != null && isSimpleArray(parameter.getType())) { BodyParameter param = new BodyParameter(); param.setRequired(requestBody.required()); param.setDescription(apiParam.description()); param.setName("body"); ArrayModel model = new ArrayModel(); StringProperty property = new StringProperty(); property.setType(lookupType(parameter.getType())); property.setFormat(lookupFormat(parameter.getType(), apiParam)); model.setItems(property); param.setSchema(model); operation.addParameter(param); continue; } if (apiParam != null && requestBody != null && isSimpleScalar(parameter.getType())) { BodyParameter param = new BodyParameter(); param.setRequired(requestBody.required()); param.setDescription(apiParam.description()); param.setName("body"); ModelImpl model = new ModelImpl(); model.setType(lookupType(parameter.getType())); model.setFormat(lookupFormat(parameter.getType(), apiParam)); param.setSchema(model); operation.addParameter(param); continue; } if (apiParam != null && requestBody != null && isModelArray(parameter.getType())) { BodyParameter param = new BodyParameter(); param.setRequired(requestBody.required()); param.setDescription(apiParam.description()); param.setName("body"); ArrayModel model = new ArrayModel(); RefProperty property = new RefProperty(); property.setType(lookupType(parameter.getType())); property.set$ref("#/definitions/" + parameter.getType().getComponentType().getSimpleName()); if (!modelStack.contains(parameter.getType().getComponentType())) { modelStack.push(parameter.getType().getComponentType()); } model.setItems(property); param.setSchema(model); operation.addParameter(param); continue; } if (apiParam != null && requestBody != null && isModelScalar(parameter.getType())) { BodyParameter param = new BodyParameter(); param.setRequired(requestBody.required()); param.setDescription(apiParam.description()); param.setName("body"); RefModel model = new RefModel(); model.set$ref( "#/definitions/" + parameter.getType().getSimpleName()); if (!modelStack.contains(parameter.getType())) { modelStack.push(parameter.getType()); } param.setSchema(model); operation.addParameter(param); continue; } } else if (requestMethod == RequestMethod.POST) { if (apiParam != null && requestParam != null && isSimpleArray(parameter.getType())) { FormParameter param = new FormParameter(); param.setRequired(requestParam.required()); param.setDescription(apiParam.description()); param.setType("array"); if (!"".equals(requestParam.value())) { param.setName(requestParam.value()); } if (!"".equals(requestParam.name())) { param.setName(requestParam.name()); } param.setItems(lookupProperty(parameter.getType(), requestParam, apiParam)); operation.addParameter(param); continue; } if (apiParam != null && requestParam != null && isSimpleScalar(parameter.getType())) { FormParameter param = new FormParameter(); param.setRequired(requestParam.required()); param.setDescription(apiParam.description()); param.setType(lookupType(parameter.getType())); param.setFormat(lookupFormat(parameter.getType(), apiParam)); if (!"".equals(requestParam.value())) { param.setName(requestParam.value()); } if (!"".equals(requestParam.name())) { param.setName(requestParam.name()); } operation.addParameter(param); continue; } if (apiParam != null && requestPart != null && isSimpleArray(parameter.getType())) { FormParameter param = new FormParameter(); param.setRequired(requestPart.required()); param.setDescription(apiParam.description()); param.setType("array"); if (!"".equals(requestPart.value())) { param.setName(requestPart.value()); } if (!"".equals(requestPart.name())) { param.setName(requestPart.name()); } param.setItems(lookupProperty(parameter.getType(), requestParam, apiParam)); operation.addParameter(param); continue; } if (apiParam != null && requestPart != null && isSimpleScalar(parameter.getType())) { FormParameter param = new FormParameter(); param.setRequired(requestPart.required()); param.setDescription(apiParam.description()); param.setType(lookupType(parameter.getType())); param.setFormat(lookupFormat(parameter.getType(), apiParam)); if (!"".equals(requestPart.value())) { param.setName(requestPart.value()); } if (!"".equals(requestPart.name())) { param.setName(requestPart.name()); } operation.addParameter(param); continue; } } } for (ApiResponse apiResponse : apiResponses.value()) { if (isSimpleScalar(apiResponse.response())) { if (apiResponse.array()) { Response response = new Response(); if (!"".equals(apiResponse.description())) { response.setDescription( apiResponse.httpStatus().getReasonPhrase()); } else { response.setDescription(apiResponse.description()); } ArrayProperty property = new ArrayProperty(); property.setItems( lookupProperty(apiResponse.response(), apiResponse)); response.setSchema(property); operation.addResponse( String.valueOf(apiResponse.httpStatus().value()), response); } else { Response response = new Response(); if ("".equals(apiResponse.description())) { response.setDescription( apiResponse.httpStatus().getReasonPhrase()); } else { response.setDescription(apiResponse.description()); } response.setSchema( lookupProperty(apiResponse.response(), apiResponse)); operation.addResponse( String.valueOf(apiResponse.httpStatus().value()), response); } } else if (isModelScalar(apiResponse.response())) { if (apiResponse.array()) { Response response = new Response(); if (!"".equals(apiResponse.description())) { response.setDescription( apiResponse.httpStatus().getReasonPhrase()); } else { response.setDescription(apiResponse.description()); } RefProperty property = new RefProperty(); property.set$ref( "#/definitions/" + apiResponse.response().getSimpleName()); if (!modelStack.contains(apiResponse.response())) { modelStack.push(apiResponse.response()); } ArrayProperty array = new ArrayProperty(); array.setItems(property); response.setSchema(array); operation.addResponse( String.valueOf(apiResponse.httpStatus().value()), response); } else { Response response = new Response(); if (!"".equals(apiResponse.description())) { response.setDescription( apiResponse.httpStatus().getReasonPhrase()); } else { response.setDescription(apiResponse.description()); } RefProperty property = new RefProperty(); property.set$ref( "#/definitions/" + apiResponse.response().getSimpleName()); if (!modelStack.contains(apiResponse.response())) { modelStack.push(apiResponse.response()); } response.setSchema(property); operation.addResponse( String.valueOf(apiResponse.httpStatus().value()), response); } } } } } } } } } } while (!modelStack.isEmpty()) { Class<?> scheme = modelStack.pop(); if (definitions.containsKey(scheme.getSimpleName())) { continue; } java.lang.reflect.Field[] fields = scheme.getDeclaredFields(); if (fields != null && fields.length > 0) { ModelImpl model = new ModelImpl(); model.setType("object"); for (Field field : fields) { ApiProperty apiProperty = field.getDeclaredAnnotation(ApiProperty.class); if (apiProperty != null) { if (apiProperty.array()) { Class<?> type = apiProperty.model(); ArrayProperty property = new ArrayProperty(); if (isSimpleScalar(type)) { property.setItems(lookupProperty(type, apiProperty)); } else if (isModelScalar(type)) { if (!definitions.containsKey(type.getSimpleName())) { modelStack.push(type); } RefProperty ref = new RefProperty(); ref.set$ref("#/definitions/" + type.getSimpleName()); property.setItems(ref); } model.addProperty(field.getName(), property); } else { Class<?> type = field.getType(); if (isSimpleScalar(type)) { model.addProperty(field.getName(), lookupProperty(type, apiProperty)); } else if (isModelScalar(type)) { if (!definitions.containsKey(type.getSimpleName())) { modelStack.push(type); } RefProperty ref = new RefProperty(); ref.set$ref("#/definitions/" + type.getSimpleName()); model.addProperty(field.getName(), ref); } } } } definitions.put(scheme.getSimpleName(), model); } } this.swagger = swagger; }
From source file:jsondb.JsonDBTemplate.java
public void join(String sourceID, String sourceCollection, String destID, String destiCollection) { Object sourceObj = this.findById(sourceID, sourceCollection); Object destObj = this.findById(destID, destiCollection); Class sc = sourceObj.getClass(); Class dc = destObj.getClass(); String scgettermethod = null; String scsettermethod = null; String destCollection = null; String destField = null;//from w w w .ja v a 2s .com String dcgettermethod = null; String dcsettermethod = null; Field[] fields = sc.getDeclaredFields(); for (Field field : fields) { DBRef d = field.getDeclaredAnnotation(DBRef.class); if (d != null) { // destCollection = d.destcollection(); destField = d.destfieldname(); // sourcefield = field.getName(); scgettermethod = "get" + field.getName(); scsettermethod = "set" + field.getName(); Method[] methods = sc.getDeclaredMethods(); for (Method method : methods) { if (method.getName().equalsIgnoreCase(scsettermethod)) { scsettermethod = method.getName(); } if (method.getName().equalsIgnoreCase(scgettermethod)) { scgettermethod = method.getName(); } } } } try { Method s = sc.getDeclaredMethod(scgettermethod, (Class[]) null); String p = (String) s.invoke(sourceObj); Field df = dc.getDeclaredField(destField); dcgettermethod = "get" + df.getName(); dcsettermethod = "set" + df.getName(); Method[] methods = dc.getDeclaredMethods(); for (Method method : methods) { if (method.getName().equalsIgnoreCase(dcsettermethod)) { dcsettermethod = method.getName(); } if (method.getName().equalsIgnoreCase(dcgettermethod)) { dcgettermethod = method.getName(); } } if (p == null) { String rs = UUID.randomUUID().toString(); Method sm = sc.getDeclaredMethod(scsettermethod, rs.getClass()); sm.invoke(sourceObj, rs); Method dm = dc.getDeclaredMethod(dcsettermethod, rs.getClass()); dm.invoke(destObj, rs); } else { Method d = dc.getDeclaredMethod(dcsettermethod, p.getClass()); d.invoke(destObj, p); } this.upsert(sourceObj); this.upsert(destObj); } catch (NoSuchMethodException ex) { Logger.error("NoSuchMethodExeption"); java.util.logging.Logger.getLogger(JsonDBTemplate.class.getName()).log(Level.SEVERE, null, ex); } catch (SecurityException ex) { Logger.error("SecurityException"); java.util.logging.Logger.getLogger(JsonDBTemplate.class.getName()).log(Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { Logger.error("IllegalAccessException"); java.util.logging.Logger.getLogger(JsonDBTemplate.class.getName()).log(Level.SEVERE, null, ex); } catch (IllegalArgumentException ex) { Logger.error("IllegalArgumentException"); java.util.logging.Logger.getLogger(JsonDBTemplate.class.getName()).log(Level.SEVERE, null, ex); } catch (InvocationTargetException ex) { Logger.error("InvokationTargetException"); java.util.logging.Logger.getLogger(JsonDBTemplate.class.getName()).log(Level.SEVERE, null, ex); } catch (NoSuchFieldException ex) { Logger.error("Feld not found"); java.util.logging.Logger.getLogger(JsonDBTemplate.class.getName()).log(Level.SEVERE, null, ex); } this.reloadCollection(destCollection); this.reloadCollection(sourceCollection); }