/**
   * Find the index of the current class member.
   *
   * @param wc
   * @param classTree
   * @param offset
   * @return
   */
  static int findClassMemberIndex(WorkingCopy wc, ClassTree classTree, int offset) {

    int index = 0;
    SourcePositions sp = wc.getTrees().getSourcePositions();
    GuardedDocument gdoc = null;
    try {
      Document doc = wc.getDocument();
      if (doc != null && doc instanceof GuardedDocument) {
        gdoc = (GuardedDocument) doc;
      }
    } catch (IOException ioe) {
    }

    Tree lastMember = null;
    for (Tree tree : classTree.getMembers()) {
      if (offset <= sp.getStartPosition(wc.getCompilationUnit(), tree)) {
        if (gdoc == null) {
          break;
        }
        int pos =
            (int)
                (lastMember != null
                    ? sp.getEndPosition(wc.getCompilationUnit(), lastMember)
                    : sp.getStartPosition(wc.getCompilationUnit(), classTree));
        pos = gdoc.getGuardedBlockChain().adjustToBlockEnd(pos);
        if (pos <= sp.getStartPosition(wc.getCompilationUnit(), tree)) {
          break;
        }
      }
      index++;
      lastMember = tree;
    }
    return index;
  }
Beispiel #2
0
  public void run(WorkingCopy workingCopy) throws Exception {
    try {
      workingCopy.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);

      CompilationUnitTree cut = workingCopy.getCompilationUnit();
      ClassTree classTree = (ClassTree) cut.getTypeDecls().get(0);

      TreeMaker make = workingCopy.getTreeMaker();

      // Import org.glassfish.openesb.pojose.api.annotation.POJO
      //       org.glassfish.openesb.pojose.api.annotation.Operation

      CompilationUnitTree copy =
          make.addCompUnitImport(
              cut,
              make.Import(make.Identifier(GeneratorUtil.PROVIDER_QUAL_CLASS_ANNOTATION), false));
      // workingCopy.rewrite(cut, copy);
      // cut = workingCopy.getCompilationUnit();
      // classTree = (ClassTree) cut.getTypeDecls().get(0);

      CompilationUnitTree copy1 =
          make.addCompUnitImport(
              copy,
              make.Import(
                  make.Identifier(GeneratorUtil.POJO_QUAL_OPERATION_ANNOTATION_CLASS), false));

      copy =
          make.addCompUnitImport(
              copy1, make.Import(make.Identifier(GeneratorUtil.CTX_QUAL_CLASS_ANNOTATION), false));

      copy1 =
          make.addCompUnitImport(
              copy, make.Import(make.Identifier(GeneratorUtil.RSRC_QUAL_CLASS_ANNOTATION), false));

      workingCopy.rewrite(cut, copy1);
      cut = workingCopy.getCompilationUnit();
      classTree = (ClassTree) cut.getTypeDecls().get(0);

      // ADD POJO CLASS TYPE ANNOTATION
      /*
      Document dox = workingCopy.getDocument();
      Element[] arrayOfElements = dox.getRootElements();
      for ( Element el : arrayOfElements) {
         displayElements(el,0);
      }*/

      List<ExpressionTree> argumentValueList = new ArrayList<ExpressionTree>();
      if (annotationArguments != null) {
        Set<Map.Entry<String, Object>> annArgValSet = annotationArguments.entrySet();
        for (Map.Entry<String, Object> mapenty : annArgValSet) {
          argumentValueList.add(
              GeneratorUtil.createAnnotationArgument(make, mapenty.getKey(), mapenty.getValue()));
        }
      }
      AnnotationTree pojoAnnTypeTree =
          GeneratorUtil.createAnnotation(
              make, workingCopy, GeneratorUtil.PROVIDER_CLASS_ANNOTATION, argumentValueList);

      ModifiersTree oldTree = classTree.getModifiers();
      ModifiersTree newTree = make.Modifiers(oldTree, Collections.singletonList(pojoAnnTypeTree));
      workingCopy.rewrite(oldTree, newTree);
      cut = workingCopy.getCompilationUnit();
      classTree = (ClassTree) cut.getTypeDecls().get(0);

      ModifiersTree modifiers =
          handleModifiersAndAnnotations(make, workingCopy, GeneratorUtil.RSRC_ANNOTATION, null);

      VariableTree variableTree =
          GeneratorUtil.createField(
              make,
              workingCopy,
              modifiers,
              GeneratorUtil
                  .POJO_CTX_VARIABLE, // TODO: Check if this variable is not used by this class.
              GeneratorUtil.CTX_ANNOTATION, // NOI18N
              null);

      ClassTree newClassTree1 = make.addClassMember(classTree, variableTree);
      // workingCopy.rewrite(classTree, newClassTree1);

      // CompilationUnitTree modClassTree = make.addCompUnitTypeDecl(cut, pojoAnnTypeTree);
      // workingCopy.rewrite(cut, modClassTree);
      cut = workingCopy.getCompilationUnit();
      classTree = newClassTree1; // (ClassTree) cut.getTypeDecls().get(0);
      // ADD POJO OPERATION.
      boolean bOperationReturnsVoid =
          methodReturnType == null || methodReturnType.equals(GeneratorUtil.GENERATE_VOID);
      argumentValueList = new ArrayList<ExpressionTree>();
      if (!bOperationReturnsVoid && this.operationArguments != null) {
        Set<Map.Entry<String, Object>> annArgValSet = operationArguments.entrySet();
        for (Map.Entry<String, Object> mapenty : annArgValSet) {
          argumentValueList.add(
              GeneratorUtil.createAnnotationArgument(make, mapenty.getKey(), mapenty.getValue()));
        }
      }
      AnnotationTree anTree =
          GeneratorUtil.createAnnotation(make, workingCopy, annotationType, argumentValueList);
      Tree returnType = null;
      boolean bReturnVoid = false;
      if (bOperationReturnsVoid) {
        returnType = make.PrimitiveType(TypeKind.VOID); // return type
        bReturnVoid = true;
      } else {
        returnType = GeneratorUtil.createType(make, workingCopy, methodReturnType);
      }

      List<TypeParameterTree> listOfInputTypes = new ArrayList<TypeParameterTree>();
      List<VariableTree> listOfInputVariables = new ArrayList<VariableTree>();
      //   ModifiersTree modTree = make.Modifiers(Collections.singleton(Modifier.PUBLIC));
      ModifiersTree parMods = make.Modifiers(Collections.EMPTY_SET, Collections.EMPTY_LIST);

      int ix = 0;
      String inputVariableName = null;
      String inputVariableType = null;
      VariableTree inputIdentifier = null;
      for (String argumentType : methodArgumentType) {
        if (!argumentType.equals("")) {
          //  listOfInputTypes.add(  make.TypeParameter(argumentType, null));
          inputVariableName = GeneratorUtil.POJO_VARIABLE_NAME_PREFIX + (ix++);
          inputVariableType = argumentType;
          inputIdentifier =
              GeneratorUtil.createField(
                  make,
                  workingCopy,
                  parMods
                  /** modTree* */
                  ,
                  inputVariableName,
                  argumentType,
                  null);
          listOfInputVariables.add(inputIdentifier);
        }
      }

      BlockTree blockTree = null;
      if (bReturnVoid) {
        blockTree = make.Block(Collections.EMPTY_LIST, false);
      } else {
        // List<ExpressionTree> returnExpList =
        // Collections.singletonList((ExpressionTree)make.Identifier("null"));
        if (inputVariableType != null
            && this.methodReturnType != null
            && methodReturnType.equals(inputVariableType)) {
          ReturnTree retTree = make.Return(make.Identifier(inputVariableName));
          blockTree = make.Block(Collections.singletonList(retTree), false);

        } else {
          ReturnTree retTree = make.Return(make.Identifier("null"));
          blockTree = make.Block(Collections.singletonList(retTree), false);
        }
      }

      MethodTree newMethod =
          make.Method(
              make.Modifiers(
                  Collections.singleton(Modifier.PUBLIC), // modifiers
                  Collections.singletonList(anTree)), // modifiers and annotations
              methodName, // name
              returnType, // return type
              Collections.EMPTY_LIST, // listOfInputTypes, // type parameters for parameters
              listOfInputVariables, // parameters
              Collections.EMPTY_LIST,
              blockTree, // empty statement block
              null // default value - not applicable here, used by annotations
              );
      ClassTree oldClassTree = (ClassTree) cut.getTypeDecls().get(0);
      ClassTree newClassTree = make.addClassMember(classTree, newMethod);
      workingCopy.rewrite(oldClassTree, newClassTree);
    } catch (Exception e) {
      myException = e;
      throw e;
    }
  }