/** * Determines if this is an action method, i.e., any method whose name isn't {@code Add}, {@code Get}, {@code List}, * {@code Update} or {@code Remove}. */ public boolean isAction() { return !(NON_ACTION_METHOD_NAMES.contains(getName()) || (base!=null && NON_ACTION_METHOD_NAMES.contains(base.getName()))); }
private String getName(Method method) { return names.getLowerJoined(method.getName(), ""); }
private String getUrlSegment(Method method) { return names.getLowerJoined(method.getName(), ""); }
private String getHttpMethod(Method method) { Name name = method.getName(); switch (name.toString().toLowerCase()) { case "get": case "list": return "GET"; case "update": return "PUT"; case "remove": return "DELETE"; default: return "POST"; } }
@Override public String toString(boolean protect) { StringBuilder buffer = new StringBuilder(); buffer.append(parameter.getDeclaringMethod().getName()); buffer.append(":"); buffer.append(parameter.getName()); return buffer.toString(); } }
/** * Return true if this method has any 'action' (e.g: install) as its base. * Methods which have a base method represent 'signatures' of the same method. */ public boolean isActionSignature(Method method) { return method.getBase()!=null && !method.getBase().getName().equals(ADD) && !method.getBase().getName().equals(UPDATE); }
/** * Return true if this method has ADD as its base. * Methods which have a base method represent 'signatures' of the same method. */ public boolean isAddSignature(Method method) { return method.getBase()!=null && method.getBase().getName().equals(ADD); }
/** * Return true if this method has UPDATE as its base. * Methods which have a base method represent 'signatures' of the same method. */ public boolean isUpdateSignature(Method method) { return method.getBase()!=null && method.getBase().getName().equals(UPDATE); }
public boolean isContained(Method method, Method method2) { if (method.getName().equals(method2.getName())) { return false; //same method } if (method.getBase()==null || method2.getBase()==null) { return false; //no parent, containment irrelevant } if (!method.getBase().getName().equals(method2.getBase().getName())) { return false; //different parents, containment irrelevant. } return isParametersContained(method.getMandatoryParameters(), method2.getMandatoryParameters()) && isAttributesContained(method.getMandatoryAttributes(), method2.getMandatoryAttributes()); }
private void writeActionValidationMethodName(Method method) { javaBuffer.addImports(schemaNames.getXjcTypeReference(ACTION_TYPE).getImports()); javaBuffer.addLine("public static void validate%s(Action action) {", javaNames.getJavaClassStyleName(method.getName())); }
private Name getSignatureDetectionMethodName(Method method) { Name methodName = new Name(); methodName.addWord("get"); methodName.addWords(method.getName().getWords()); methodName.addWord("Signature"); return methodName; }
private void generateActionSignature(Method method) { generateDoc(method); javaBuffer.addImport(Response.class); javaBuffer.addImport(javaPackages.getXjcPackageName(), "Action"); Name methodName = new Name(method.getBase().getName()); methodName.addWords(method.getName().getWords()); javaBuffer.addLine("default public Response %s(Action action) {", jaxrsNames.getMethodName(methodName)); javaBuffer.addLine("throw new UnsupportedOperationException();"); javaBuffer.addLine("}"); }
private void generateUpdateSignature(Method method) { generateDoc(method); // Find the main parameter of the method, as this is the only one that appears in the JAX-RS interfaces, the // rest of the methods are extracted explicitly by the implementation: Parameter mainParameter = jaxrsGeneratorUtils.getMainUpdateParameter(method); if (mainParameter == null) { throw new IllegalStateException("Method \"" + method + "\" doesn't have any struct parameter"); } // Calculate the Java type of the main parameter: Type mainType = mainParameter.getType(); JavaTypeReference mainTypeReference = schemaNames.getXjcTypeReference(mainType); String methodName = javaNames.getJavaMemberStyleName(names.concatenate(method.getBase().getName(), method.getName())); javaBuffer.addLine( "default public %s " + methodName + "(%s %s) {", mainTypeReference.getText(), mainTypeReference.getText(), javaNames.getJavaMemberStyleName(mainParameter.getName())); javaBuffer.addLine("throw new UnsupportedOperationException();"); javaBuffer.addLine("}"); }
private void generateAddSignature(Method method) { generateDoc(method); // Find the main parameter of the method, as this is the only one that appears in the JAX-RS interfaces, the // rest of the methods are extracted explicitly by the implementation: Parameter mainParameter = jaxrsGeneratorUtils.getMainAddParameter(method); if (mainParameter == null) { throw new IllegalStateException("Method \"" + method + "\" doesn't have any struct parameter"); } // Calculate the Java type of the main parameter: Type mainType = mainParameter.getType(); JavaTypeReference mainTypeReference = schemaNames.getXjcTypeReference(mainType); javaBuffer.addImports(mainTypeReference.getImports()); String parameterName = javaNames.getJavaMemberStyleName(mainParameter.getName()); String methodName = javaNames.getJavaMemberStyleName(names.concatenate(method.getBase().getName(), method.getName())); javaBuffer.addLine("default public Response %s(%s %s) {", methodName, mainTypeReference.getText(), parameterName); javaBuffer.addLine("throw new UnsupportedOperationException();"); javaBuffer.addLine("}"); javaBuffer.addLine(); }
javaBuffer.addLine( "@Path(\"%s\")", jaxrsNames.getActionPath(method.getName()) ); String methodName = jaxrsNames.getMethodName(method.getName()); if (baseMethods.containsKey(method)) { javaBuffer.addLine("default Response %s(Action action) {", methodName); Set<Method> signatures = baseMethods.get(method); if (signatures!=null && mandatoryAttributeExists(signatures)) { writeHelperInvocation(helperClassName, "action", method.getName()); addResponseReturnMethod(jaxrsNames.getMethodName(method.getName()) + "(Action action)");
private void handleSignature(Name parentMethodName, Method signature, Name parameterName, Type parameterType) { Parameter parameter = signature.getParameter(parameterName); Iterator<MemberInvolvementTree> iterator = parameter.getMandatoryAttributes().iterator(); if (iterator.hasNext()) {//if there are any mandatory attributes javaBuffer.addDocComment(signature.getName().toString()); javaBuffer.addLine("if (" +javaNames.getJavaMemberStyleName(parameter.getName()) + "!=null"); while (iterator.hasNext()) { MemberInvolvementTree attribute = iterator.next(); List<MemberInvolvementTree> list = stackAttributeComponents(attribute); String attributeCheck = getFullAttributeCheck(javaNames.getJavaMemberStyleName(parameter.getName()), list, Operator.AND, true); if (attribute.getAlternative()!=null) {//'or' expression list = stackAttributeComponents(attribute.getAlternative()); String alternativeCheck = getFullAttributeCheck(javaNames.getJavaMemberStyleName(parameter.getName()), list, Operator.AND, true); String fullCheck = mergeChecks(attributeCheck, alternativeCheck); javaBuffer.addLine(fullCheck); } else { javaBuffer.addLine(attributeCheck); } } javaBuffer.addLine(") {"); javaBuffer.addLine("return " + resourceName.getSimpleName() + ".class.getMethod(\"" + javaNames.getJavaMemberStyleName(parentMethodName) + javaNames.getJavaClassStyleName(signature.getName()) + "\", " + javaNames.getJavaClassStyleName(parameter.getType().getName()) + ".class);"); javaBuffer.addLine("}"); } }
private void generateValidationMethodName(Method method) { Parameter parameter = jaxrsGeneratorUtils.getMainAddParameter(method); javaBuffer.addImports(schemaNames.getXjcTypeReference(parameter.getType()).getImports()); javaBuffer.addLine("public static void validate%s(%s %s) {", javaNames.getJavaClassStyleName(method.getName()), javaNames.getJavaClassStyleName(parameter.getType().getName()), javaNames.getJavaMemberStyleName(parameter.getName())); }
private void generateHelperCode(Method method) { boolean base = baseMethods.containsKey(method); Name name = method.getName(); //for the following cases - no validation required //(signatures are handled in the context of the base method). if (JaxrsGeneratorUtils.GET.equals(name) || JaxrsGeneratorUtils.LIST.equals(name) || JaxrsGeneratorUtils.REMOVE.equals(name) || jaxrsGeneratorUtils.isAddSignature(method) || jaxrsGeneratorUtils.isUpdateSignature(method) || jaxrsGeneratorUtils.isActionSignature(method)) { return; //do nothing. } if (JaxrsGeneratorUtils.ADD.equals(name) || JaxrsGeneratorUtils.UPDATE.equals(name)) { if (base) { generateSignatureDetection(method); } else { generateValidation(method); } } else {//other options exhausted, this must be an action. if (base) { generateActionSignatureDetection(method); } else { generateActionValidation(method); } } }
private void generateSignatureDetection(Method method) { Set<Method> signatures = baseMethods.get(method); if (mandatoryAttributeExists(signatures)) { javaBuffer.addImport(java.lang.reflect.Method.class); Parameter parameter = jaxrsGeneratorUtils.getMainAddParameter(method); Name parameterName = parameter.getName(); javaBuffer.addImports(schemaNames.getXjcTypeReference(parameter.getType()).getImports()); Name methodName = getSignatureDetectionMethodName(method); javaBuffer.addLine("public static Method %s(%s %s) throws NoSuchMethodException, SecurityException {", javaNames.getJavaMemberStyleName(methodName), javaNames.getJavaClassStyleName(parameter.getType().getName()), javaNames.getJavaMemberStyleName(parameterName)); generateParameterValidation(parameter); CyclicIterator iterator = new CyclicIterator(signatures); while (iterator.hasNext()) { Method signature = iterator.next(); if (!jaxrsHelperGeneratorUtils.isContained(signature, signatures)) { handleSignature(method.getName(), signature, parameterName, parameter.getType()); iterator.remove(); } } closeSignatureDetectionMethod(); } //TODO: in the future fail for this // else { // throw new ValidationException(method.getName() + "'s signatures have 0 mandatory attributes (any signature must have at least 1 mandatory attribute)"); // } }
private void generateMethod(Method method, JavaClassName helperClassName, Map<Method, Set<Method>> baseMethods) { Name name = method.getName(); if (JaxrsGeneratorUtils.ADD.equals(name)) { generateAddMethod(method, helperClassName, baseMethods); } else if (JaxrsGeneratorUtils.GET.equals(name)) { generateGetMethod(method); } else if (JaxrsGeneratorUtils.LIST.equals(name)) { generateListMethod(method); } else if (JaxrsGeneratorUtils.REMOVE.equals(name)) { generateRemoveMethod(method); } else if (JaxrsGeneratorUtils.UPDATE.equals(name)) { generateUpdateMethod(method, helperClassName, baseMethods); } else if (jaxrsGeneratorUtils.isAddSignature(method)) { generateAddSignature(method); } else if (jaxrsGeneratorUtils.isUpdateSignature(method)) { generateUpdateSignature(method); } else if (jaxrsGeneratorUtils.isActionSignature(method)) { generateActionSignature(method); } else {//other options exhausted; must be an action generateActionMethod(method, helperClassName, baseMethods); } }