/*
   * Elements returned by next() are BodyDeclaration or Annotation instances.
   */
  private Iterator<ASTNode> getReplacementScope() throws JavaModelException {
    boolean declPredecessorReached = false;

    Collection<ASTNode> scope = new ArrayList<>();

    AbstractTypeDeclaration containingType = getContainingTypeDeclarationNode();
    if (containingType instanceof EnumDeclaration) {
      // replace in all enum constants bodies
      EnumDeclaration enumDeclaration = (EnumDeclaration) containingType;
      scope.addAll(enumDeclaration.enumConstants());
    }

    for (Iterator<IExtendedModifier> iter = containingType.modifiers().iterator();
        iter.hasNext(); ) {
      IExtendedModifier modifier = iter.next();
      if (modifier instanceof Annotation) {
        scope.add((ASTNode) modifier);
      }
    }

    for (Iterator<BodyDeclaration> bodyDeclarations = containingType.bodyDeclarations().iterator();
        bodyDeclarations.hasNext(); ) {
      BodyDeclaration bodyDeclaration = bodyDeclarations.next();

      if (bodyDeclaration == getNodeToInsertConstantDeclarationAfter())
        declPredecessorReached = true;

      if (insertFirst()
          || declPredecessorReached
          || !isStaticFieldOrStaticInitializer(bodyDeclaration)) scope.add(bodyDeclaration);
    }
    return scope.iterator();
  }
Exemple #2
0
 /* (non-Javadoc)
  * @see org.eclipse.jdt.core.dom.ASTVisitor#visit(org.eclipse.jdt.core.dom.EnumDeclaration)
  */
 public boolean visit(EnumDeclaration node) {
   if (isPrivate(node.getModifiers())) {
     return false;
   }
   enterType(node.getName());
   return isContinue();
 }
Exemple #3
0
 @Override
 public boolean visit(final EnumDeclaration node) {
   if (className.isEmpty()) {
     className.push(currentPackageName + "." + node.getName().getIdentifier());
   } else {
     className.push(className.peek() + "." + node.getName().getIdentifier());
   }
   return super.visit(node);
 }
Exemple #4
0
 @Override
 public boolean visit(EnumDeclaration node) {
   int flags = node.getModifiers();
   if (Modifier.isPublic(flags)) {
     clazz.setVisibility(Visibility.PUBLIC);
   } else {
     clazz.setVisibility(Visibility.PACKAGE_PRIVATE);
   }
   clazz.setEnum(true);
   for (Object constant : node.enumConstants()) {
     fields.add(new OutlineField(constant.toString(), clazz));
   }
   clazz.checkVisibility(flags);
   clazz.setImg();
   return super.visit(node);
 }
 /*
  * @see ASTVisitor#visit(EnumDeclaration)
  * @since 3.0
  */
 @Override
 public boolean visit(EnumDeclaration node) {
   if (node.getJavadoc() != null) {
     node.getJavadoc().accept(this);
   }
   printModifiers(node.modifiers());
   this.fBuffer.append("enum "); // $NON-NLS-1$
   node.getName().accept(this);
   this.fBuffer.append(" "); // $NON-NLS-1$
   if (!node.superInterfaceTypes().isEmpty()) {
     this.fBuffer.append("implements "); // $NON-NLS-1$
     for (Iterator<Type> it = node.superInterfaceTypes().iterator(); it.hasNext(); ) {
       Type t = it.next();
       t.accept(this);
       if (it.hasNext()) {
         this.fBuffer.append(", "); // $NON-NLS-1$
       }
     }
     this.fBuffer.append(" "); // $NON-NLS-1$
   }
   this.fBuffer.append("{"); // $NON-NLS-1$
   for (Iterator<EnumConstantDeclaration> it = node.enumConstants().iterator(); it.hasNext(); ) {
     EnumConstantDeclaration d = it.next();
     d.accept(this);
     // enum constant declarations do not include punctuation
     if (it.hasNext()) {
       // enum constant declarations are separated by commas
       this.fBuffer.append(", "); // $NON-NLS-1$
     }
   }
   if (!node.bodyDeclarations().isEmpty()) {
     this.fBuffer.append("; "); // $NON-NLS-1$
     for (Iterator<BodyDeclaration> it = node.bodyDeclarations().iterator(); it.hasNext(); ) {
       BodyDeclaration d = it.next();
       d.accept(this);
       // other body declarations include trailing punctuation
     }
   }
   this.fBuffer.append("}"); // $NON-NLS-1$
   return false;
 }
  @Override
  public boolean visit(org.eclipse.jdt.core.dom.EnumDeclaration ed) {
    String typeFullyQualifiedName = removeSpecialSymbols(ed.getName().getFullyQualifiedName());
    if (ed.superInterfaceTypes() != null && ed.superInterfaceTypes().size() > 0) {
      List<Object> interfaces = ed.superInterfaceTypes();
      typeToInterfacesFullyQualifiedName.put(typeFullyQualifiedName, interfaces);
    }
    typesInFile.push(ed.getName().getFullyQualifiedName());
    TypeDecl obj = new TypeDecl(typeFullyQualifiedName, ed.getName().getStartPosition());
    typeDeclarations.add(obj);

    addTypeDoc(ed, typeFullyQualifiedName);

    return true;
  }
  @Override
  protected void generate(EnumDeclaration node) {
    List<EnumConstantDeclaration> constants = ASTUtil.getEnumConstants(node);
    List<MethodDeclaration> methods = Lists.newArrayList();
    List<FieldDeclaration> fields = Lists.newArrayList();
    MethodDeclaration initializeMethod = null;
    for (BodyDeclaration decl : ASTUtil.getBodyDeclarations(node)) {
      if (decl instanceof FieldDeclaration) {
        fields.add((FieldDeclaration) decl);
      } else if (decl instanceof MethodDeclaration) {
        MethodDeclaration md = (MethodDeclaration) decl;
        if (md.getName().getIdentifier().equals("initialize")) {
          initializeMethod = md;
        } else {
          methods.add(md);
        }
      }
    }
    syncLineNumbers(node.getName()); // avoid doc-comment

    String typeName = NameTable.getFullName(node);
    newline();
    for (EnumConstantDeclaration constant : constants) {
      IVariableBinding var = Types.getVariableBinding(constant.getName());
      printf("static %s *%s;\n", typeName, NameTable.getStaticVarQualifiedName(var));
    }
    printf("IOSObjectArray *%s_values;\n", typeName);
    newline();

    printf("@implementation %s\n\n", typeName);
    printStaticVars(fields, /* isInterface */ false);
    printStaticReferencesMethod(fields, typeName + "_values");

    for (EnumConstantDeclaration constant : constants) {
      String name = NameTable.getName(constant.getName());
      printf("+ (%s *)%s {\n", typeName, name);
      printf("  return %s_%s;\n", typeName, name);
      println("}");
    }
    newline();

    // Enum constants needs to implement NSCopying.  Being singletons, they
    // can just return self, as long the retain count is incremented.
    String selfString = Options.useReferenceCounting() ? "[self retain]" : "self";
    printf("- (id)copyWithZone:(NSZone *)zone {\n  return %s;\n}\n\n", selfString);

    printStaticFieldAccessors(fields, methods, /* isInterface */ false);
    printMethodsAndOcni(node, methods, blockComments.get(node));

    printf("+ (void)initialize {\n  if (self == [%s class]) {\n", typeName);
    for (int i = 0; i < constants.size(); i++) {
      EnumConstantDeclaration constant = constants.get(i);
      List<Expression> args = ASTUtil.getArguments(constant);
      String name = NameTable.getName(constant.getName());
      String constantTypeName =
          NameTable.getFullName(Types.getMethodBinding(constant).getDeclaringClass());
      printf("    %s_%s = [[%s alloc] init", typeName, name, constantTypeName);

      if (args.isEmpty()) {
        print("With");
      } else {
        print(
            StatementGenerator.generateArguments(
                Types.getMethodBinding(constant),
                args,
                fieldHiders,
                getBuilder().getSourcePosition()));
        print(" with");
      }
      printf("NSString:@\"%s\" withInt:%d];\n", name, i);
    }
    printf("    %s_values = [[IOSObjectArray alloc] initWithObjects:(id[]){ ", typeName);
    for (EnumConstantDeclaration constant : constants) {
      printf("%s_%s, ", typeName, NameTable.getName(constant.getName()));
    }
    printf(
        "nil } count:%d type:[IOSClass classWithClass:[%s class]]];\n", constants.size(), typeName);
    if (initializeMethod != null) {
      for (Statement s : ASTUtil.getStatements(initializeMethod.getBody())) {
        printf(
            "    %s",
            StatementGenerator.generate(s, fieldHiders, false, getBuilder().getSourcePosition()));
      }
    }
    println("  }\n}\n");

    // Print generated values and valueOf methods.
    println("+ (IOSObjectArray *)values {");
    printf("  return [IOSObjectArray arrayWithArray:%s_values];\n", typeName);
    println("}\n");
    printf("+ (%s *)valueOfWithNSString:(NSString *)name {\n", typeName);
    printf("  for (int i = 0; i < [%s_values count]; i++) {\n", typeName);
    printf("    %s *e = %s_values->buffer_[i];\n", typeName, typeName);
    printf("    if ([name isEqual:[e name]]) {\n");
    printf("      return e;\n");
    printf("    }\n");
    printf("  }\n");
    if (Options.useReferenceCounting()) {
      printf(
          "  @throw [[[JavaLangIllegalArgumentException alloc] initWithNSString:name]"
              + " autorelease];\n");
    } else {
      printf("  @throw [[JavaLangIllegalArgumentException alloc] initWithNSString:name];\n");
    }
    printf("  return nil;\n");
    println("}\n");

    if (!Options.stripReflection()) {
      printTypeAnnotationsMethod(node);
      printMetadata(node);
    }
    println("@end");
  }
Exemple #8
0
 public boolean visit(EnumDeclaration node) {
   if (found(node, node.getName()) && this.resolveBinding)
     this.foundBinding = node.resolveBinding();
   return true;
 }
  @Override
  public boolean visit(EnumDeclaration node) {

    if (node.isPackageMemberTypeDeclaration() || node.isMemberTypeDeclaration()) {
      ITypeBinding typeBinding = node.resolveBinding();
      if (typeBinding != null) {
        ApiEnum apiClass = new ApiEnum(typeBinding.getQualifiedName());
        apiClass.setInterface(typeBinding.isInterface());
        this.setModifiers(apiClass, node);

        this.classMap.put(typeBinding, apiClass);

        if (!this.attachmentMap.containsKey(node.getRoot())) {
          Attachment attachment =
              new Attachment(
                  typeBinding.getQualifiedName().concat(".java"),
                  node.getRoot().toString().getBytes());
          this.attachmentMap.put(node.getRoot(), attachment);
        }

        List<MethodDeclaration> declarations = new LinkedList<MethodDeclaration>();
        for (Object o : node.bodyDeclarations()) {
          if (o instanceof MethodDeclaration) {
            declarations.add((MethodDeclaration) o);
          }
        }

        for (final MethodDeclaration declaration : declarations) {

          final List<Expression> invocations = new LinkedList<Expression>();
          this.methodDeclarationHandler(declaration, apiClass);

          InvocationVisitor visitor = new InvocationVisitor();
          declaration.accept(visitor);

          invocations.addAll(visitor.invocations);

          // Parte de extracao de exemplos, se não houver invocações eu pulo esta parte
          if (invocations.isEmpty()) {
            return super.visit(node);
          } else {
            // Faço a extração de exemplos para cada invocação em separado
            Set<ApiMethod> methodsInvocations = new HashSet<ApiMethod>();
            for (Expression mi : invocations) {
              ApiMethod apiMethod = this.mapInvocations.get(mi);
              methodsInvocations.add(apiMethod);
              Example newExample =
                  makeExample(
                      declaration, Collections.singleton(mi), Collections.singletonList(apiMethod));
              if (newExample != null) {
                this.examples.add(newExample);
              }
            }

            // Se houver mais de uma invocacao faço o slicing com sementes multiplas
            if (invocations.size() > 1) {
              // Itero sobre os conjuntos das regras
              Iterator<Set<ApiMethod>> it = this.methodSets.iterator();
              while (it.hasNext()) {
                // Podando conjuntos
                Set<ApiMethod> s = it.next();
                if (s.size() > methodsInvocations.size()) {
                  break;
                } else {
                  // Se as invocacoes identificadas envolvem metodos desejados eu processo
                  if (methodsInvocations.containsAll(s)) {
                    List<ApiMethod> methods = new ArrayList<ApiMethod>();
                    Set<Expression> invocationsTemp = new HashSet<Expression>();
                    for (Expression mi : invocations) {
                      ApiMethod relatedMethod = this.mapInvocations.get(mi);
                      if (s.contains(relatedMethod)) {
                        methods.add(relatedMethod);
                        invocationsTemp.add(mi);
                      }
                    }
                    Example newExample = makeExample(declaration, invocationsTemp, methods);
                    if (newExample != null) {
                      this.examples.add(newExample);
                    }
                  }
                }
              }
            }
          }
        }
      }
    }

    return super.visit(node);
  }
 public boolean visit(EnumDeclaration node) {
   push(JavaNode.ENUM, node.getName().toString(), node.getStartPosition(), node.getLength());
   return true;
 }
Exemple #11
0
 /* (non-Javadoc)
  * @see org.eclipse.jdt.core.dom.ASTVisitor#endVisit(org.eclipse.jdt.core.dom.EnumDeclaration)
  */
 public void endVisit(EnumDeclaration node) {
   if (!isPrivate(node.getModifiers())) {
     exitType();
   }
 }