@Override
 public void postProcessAnnotations(ApplicationMetaModel metaModel) {
   ElementHandle.Package pkg = metaModel.getHandle();
   AnnotationState annotation = servlets.remove(pkg);
   if (annotation != null) {
     PackageElement pkgElt = metaModel.processingContext.get(pkg);
     String urlPattern = (String) annotation.get("value");
     String simpleName = (String) annotation.get("name");
     if (simpleName == null) {
       simpleName = metaModel.getBaseName() + "Servlet";
     }
     Name clazz = pkg.getPackage().append(simpleName);
     Writer writer = null;
     try {
       JavaFileObject file = metaModel.processingContext.createSourceFile(clazz, pkgElt);
       writer = file.openWriter();
       writer.append("package ").append(pkg.getPackage()).append(";\n");
       writer.append("import javax.servlet.annotation.WebServlet;\n");
       writer.append("import javax.servlet.annotation.WebInitParam;\n");
       writer
           .append("@WebServlet(name=\"")
           .append(simpleName)
           .append("\",urlPatterns=\"")
           .append(urlPattern)
           .append("\")\n");
       writer
           .append("public class ")
           .append(simpleName)
           .append(" extends juzu.bridge.servlet.JuzuServlet {\n");
       writer.append("@Override\n");
       writer.append(
           "protected String getApplicationName(javax.servlet.ServletConfig config) {\n");
       writer.append("return \"").append(pkg.getPackage()).append("\";\n");
       writer.append("}\n");
       writer.append("}\n");
     } catch (IOException e) {
       throw CANNOT_WRITE_SERVLET_CLASS.failure(e, pkgElt, pkg.getPackage());
     } finally {
       Tools.safeClose(writer);
     }
   }
 }
Exemplo n.º 2
0
  void addMethod(ModuleMetaModel context, AnnotationKey key2, AnnotationState annotation) {

    //
    String id = (String) annotation.get("id");

    ExecutableElement methodElt =
        (ExecutableElement) context.processingContext.get(key2.getElement());

    //
    for (Phase phase : Phase.values()) {
      if (phase.annotation.getName().equals(key2.getType().toString())) {
        ElementHandle.Method origin = (ElementHandle.Method) key2.getElement();

        // First remove the previous method
        Key<MethodMetaModel> key = Key.of(origin, MethodMetaModel.class);
        if (getChild(key) == null) {
          // Parameters
          ArrayList<ParameterMetaModel> parameters = new ArrayList<ParameterMetaModel>();
          List<? extends TypeMirror> parameterTypeMirrors =
              ((ExecutableType) methodElt.asType()).getParameterTypes();
          List<? extends VariableElement> parameterVariableElements = methodElt.getParameters();
          for (int i = 0; i < parameterTypeMirrors.size(); i++) {
            VariableElement parameterVariableElt = parameterVariableElements.get(i);
            TypeMirror parameterTypeMirror = parameterTypeMirrors.get(i);
            String typeLiteral = context.processingContext.getLiteralName(parameterTypeMirror);

            //
            String parameterName = parameterVariableElt.getSimpleName().toString();

            // Determine cardinality
            TypeMirror parameterSimpleTypeMirror;
            Cardinality parameterCardinality;
            switch (parameterTypeMirror.getKind()) {
              case DECLARED:
                DeclaredType dt = (DeclaredType) parameterTypeMirror;
                TypeElement col = context.processingContext.getTypeElement("java.util.List");
                TypeMirror tm = context.processingContext.erasure(col.asType());
                TypeMirror err = context.processingContext.erasure(dt);
                // context.env.isSubtype(err, tm)
                if (err.equals(tm)) {
                  if (dt.getTypeArguments().size() != 1) {
                    throw CONTROLLER_METHOD_PARAMETER_NOT_RESOLVED.failure(parameterVariableElt);
                  } else {
                    parameterCardinality = Cardinality.LIST;
                    parameterSimpleTypeMirror = dt.getTypeArguments().get(0);
                  }
                } else {
                  parameterCardinality = Cardinality.SINGLE;
                  parameterSimpleTypeMirror = parameterTypeMirror;
                }
                break;
              case ARRAY:
                // Unwrap array
                ArrayType arrayType = (ArrayType) parameterTypeMirror;
                parameterCardinality = Cardinality.ARRAY;
                parameterSimpleTypeMirror = arrayType.getComponentType();
                break;
              default:
                throw CONTROLLER_METHOD_PARAMETER_NOT_RESOLVED.failure(parameterVariableElt);
            }
            if (parameterSimpleTypeMirror.getKind() != TypeKind.DECLARED) {
              throw CONTROLLER_METHOD_PARAMETER_NOT_RESOLVED.failure(parameterVariableElt);
            }

            //
            TypeElement te =
                (TypeElement) context.processingContext.asElement(parameterSimpleTypeMirror);
            ElementHandle.Type a = ElementHandle.Type.create(te);

            //
            if (te.toString().equals("java.lang.String")
                || te.getAnnotation(Mapped.class) != null) {
              // Not sure we should use @Param for this (i.e for now it looks hackish)
              // however it does make sense later to use the regex part for non router
              // parameters
              Param param = parameterVariableElt.getAnnotation(Param.class);
              String alias = param != null && param.name().length() > 0 ? param.name() : null;
              parameters.add(
                  new PhaseParameterMetaModel(
                      parameterName, parameterCardinality, a, typeLiteral, alias));
            } else {
              parameters.add(new ContextualParameterMetaModel(parameterName, typeLiteral));
            }
          }

          //
          MethodMetaModel method =
              new MethodMetaModel(
                  origin, id, phase, methodElt.getSimpleName().toString(), parameters);
          addChild(key, method);
          modified = true;
        }
        break;
      }
    }
  }