private boolean testModifier(IVariableBinding curr) {
   int modifiers = curr.getModifiers();
   int staticFinal = Modifier.STATIC | Modifier.FINAL;
   if ((modifiers & staticFinal) == staticFinal) {
     return false;
   }
   if (Modifier.isStatic(modifiers) && !Modifier.isStatic(fMethodDecl.getModifiers())) {
     return false;
   }
   return true;
 }
  private void computeConstantDeclarationLocation() throws JavaModelException {
    if (isDeclarationLocationComputed()) return;

    BodyDeclaration lastStaticDependency = null;
    Iterator<BodyDeclaration> decls =
        getContainingTypeDeclarationNode().bodyDeclarations().iterator();

    while (decls.hasNext()) {
      BodyDeclaration decl = decls.next();

      int modifiers;
      if (decl instanceof FieldDeclaration) modifiers = ((FieldDeclaration) decl).getModifiers();
      else if (decl instanceof Initializer) modifiers = ((Initializer) decl).getModifiers();
      else {
        continue; /* this declaration is not a field declaration
                  or initializer, so the placement of the constant
                  declaration relative to it does not matter */
      }

      if (Modifier.isStatic(modifiers) && depends(getSelectedExpression(), decl))
        lastStaticDependency = decl;
    }

    if (lastStaticDependency == null) fInsertFirst = true;
    else fToInsertAfter = lastStaticDependency;
  }
 private List<VariableDeclarationFragment> getProperties(FieldDeclaration[] fields) {
   List<VariableDeclarationFragment> properties = Lists.newArrayList();
   for (FieldDeclaration field : fields) {
     if (!Modifier.isStatic(field.getModifiers())) {
       properties.addAll(ASTUtil.getFragments(field));
     }
   }
   return properties;
 }
  // Método para configurar os modificadores
  @SuppressWarnings("unchecked")
  private void setModifiers(ApiElement apiElement, BodyDeclaration node) {

    for (Object o : node.modifiers()) {
      if (o instanceof Modifier) {
        Modifier modifier = (Modifier) o;
        if (modifier.isAbstract()) {
          apiElement.setAbstract(true);
        } else if (modifier.isFinal()) {
          apiElement.setFinal(true);
        } else if (modifier.isPrivate()) {
          apiElement.setPrivate(true);
        } else if (modifier.isProtected()) {
          apiElement.setProtected(true);
        } else if (modifier.isPublic()) {
          apiElement.setPublic(true);
        } else if (modifier.isStatic()) {
          apiElement.setFinal(true);
        }
      }
    }

    apiElement.setDefault(
        !(apiElement.isPrivate() || apiElement.isPublic() || apiElement.isProtected()));

    Javadoc javadoc = node.getJavadoc();
    if (javadoc != null) {
      apiElement.setHasJavadoc(true);
      apiElement.setHidden(false);
      Stack<Object> tags = new Stack<Object>();
      tags.addAll(javadoc.tags());
      while (!tags.isEmpty()) {
        Object tag = tags.pop();
        if (tag instanceof TagElement) {
          String tagName = ((TagElement) tag).getTagName();
          if (tagName != null && tagName.equalsIgnoreCase("@hide")) {
            apiElement.setHidden(true);
            break;
          }
          tags.addAll(((TagElement) tag).fragments());
        }
      }
    } else {
      apiElement.setHasJavadoc(false);
      apiElement.setHidden(true);
    }
  }
  private static boolean isStaticFieldOrStaticInitializer(BodyDeclaration node) {
    if (node instanceof MethodDeclaration || node instanceof AbstractTypeDeclaration) return false;

    int modifiers;
    if (node instanceof FieldDeclaration) {
      modifiers = ((FieldDeclaration) node).getModifiers();
    } else if (node instanceof Initializer) {
      modifiers = ((Initializer) node).getModifiers();
    } else {
      Assert.isTrue(false);
      return false;
    }

    if (!Modifier.isStatic(modifiers)) return false;

    return true;
  }
  private void possibleStaticImportFound(Name name) {
    if (fStaticImports == null || fASTRoot == null) {
      return;
    }

    while (name.isQualifiedName()) {
      name = ((QualifiedName) name).getQualifier();
    }
    if (!isAffected(name)) {
      return;
    }

    IBinding binding = name.resolveBinding();
    SimpleName simpleName = (SimpleName) name;
    if (binding == null
        || binding instanceof ITypeBinding
        || !Modifier.isStatic(binding.getModifiers())
        || simpleName.isDeclaration()) {
      return;
    }

    if (binding instanceof IVariableBinding) {
      IVariableBinding varBinding = (IVariableBinding) binding;
      if (varBinding.isField()) {
        varBinding = varBinding.getVariableDeclaration();
        ITypeBinding declaringClass = varBinding.getDeclaringClass();
        if (declaringClass != null && !declaringClass.isLocal()) {
          if (new ScopeAnalyzer(fASTRoot)
              .isDeclaredInScope(
                  varBinding, simpleName, ScopeAnalyzer.VARIABLES | ScopeAnalyzer.CHECK_VISIBILITY))
            return;
          fStaticImports.add(simpleName);
        }
      }
    } else if (binding instanceof IMethodBinding) {
      IMethodBinding methodBinding = ((IMethodBinding) binding).getMethodDeclaration();
      ITypeBinding declaringClass = methodBinding.getDeclaringClass();
      if (declaringClass != null && !declaringClass.isLocal()) {
        if (new ScopeAnalyzer(fASTRoot)
            .isDeclaredInScope(
                methodBinding, simpleName, ScopeAnalyzer.METHODS | ScopeAnalyzer.CHECK_VISIBILITY))
          return;
        fStaticImports.add(simpleName);
      }
    }
  }
Exemple #7
0
  /**
   * Get a method's signature for dead code elimination purposes.
   *
   * <p>Since DeadCodeEliminator runs before InnerClassExtractor, inner class constructors do not
   * yet have the parameter for capturing outer class, and therefore we need this special case.
   */
  public static String getProGuardSignature(IMethodBinding binding) {
    StringBuilder sb = new StringBuilder("(");

    // If the method is an inner class constructor, prepend the outer class type.
    if (binding.isConstructor()) {
      ITypeBinding declClass = binding.getDeclaringClass();
      ITypeBinding outerClass = declClass.getDeclaringClass();
      if (outerClass != null
          && !declClass.isInterface()
          && !declClass.isAnnotation()
          && !Modifier.isStatic(declClass.getModifiers())) {
        appendParameterSignature(outerClass.getErasure(), sb);
      }
    }

    appendParametersSignature(binding, sb);
    sb.append(')');
    appendReturnTypeSignature(binding, sb);
    return sb.toString();
  }
  @Override
  public boolean visit(ClassInstanceCreation node) {
    ITypeBinding newType = node.getTypeBinding().getTypeDeclaration();
    ITypeBinding declaringClass = newType.getDeclaringClass();
    if (Modifier.isStatic(newType.getModifiers()) || declaringClass == null) {
      return true;
    }

    GeneratedMethodBinding binding =
        new GeneratedMethodBinding(node.getMethodBinding().getMethodDeclaration());
    node.setMethodBinding(binding);
    addOuterArg(node, binding, declaringClass);

    for (IVariableBinding capturedVar : getCapturedVariables(node)) {
      node.getArguments().add(new SimpleName(capturedVar));
      binding.addParameter(capturedVar.getType());
    }

    assert binding.isVarargs() || node.getArguments().size() == binding.getParameterTypes().length;
    return true;
  }
 // We generate the runtime debug method +memDebugStaticReferences.
 // This method will return an array of NSNumber containing pointers (casted into unsigned long)
 // to the objects referenced by a class variable with a strong reference.
 // It will be useful for debug purpose.
 //
 // Arrays returned by -memDebugStaticReferences and -memDebugStaticReferencesNames (see below)
 // must be the same size.
 //
 // In case of a Java enum, valuesVarNameis the name of the array of enum values.
 private void printStaticReferencesMethod(List<FieldDeclaration> fields, String valuesVarName) {
   if (Options.memoryDebug()) {
     if (!Options.useReferenceCounting()) {
       println("+ (NSArray *)memDebugStaticReferences {");
       println("  return nil;");
       println("}");
       return;
     }
     println("+ (NSArray *)memDebugStaticReferences {");
     println("  NSMutableArray *result = [NSMutableArray array];");
     for (FieldDeclaration f : fields) {
       if (Modifier.isStatic(f.getModifiers())) {
         for (VariableDeclarationFragment var : ASTUtil.getFragments(f)) {
           IVariableBinding binding = Types.getVariableBinding(var);
           // All non-primitive static variables are strong references.
           if (!binding.getType().isPrimitive()) {
             String name = NameTable.getStaticVarQualifiedName(binding);
             println(
                 String.format(
                     "  [result addObject:[JreMemDebugStrongReference "
                         + "strongReferenceWithObject:%s name:@\"%s\"]];",
                     name, name));
           }
         }
       }
     }
     if (valuesVarName != null) {
       println(
           String.format(
               "  [result addObject:[JreMemDebugStrongReference "
                   + "strongReferenceWithObject:%s name:@\"enumValues\"]];",
               valuesVarName));
     }
     println("  return result;");
     println("}\n");
   }
 }
 private void printStaticVars(List<FieldDeclaration> fields, boolean isInterface) {
   boolean hadStaticVar = false;
   for (FieldDeclaration f : fields) {
     if (Modifier.isStatic(f.getModifiers()) || isInterface) {
       for (VariableDeclarationFragment var : ASTUtil.getFragments(f)) {
         IVariableBinding binding = Types.getVariableBinding(var);
         if (!BindingUtil.isPrimitiveConstant(binding)) {
           String name = NameTable.getStaticVarQualifiedName(binding);
           String objcType = NameTable.getObjCType(binding.getType());
           Expression initializer = var.getInitializer();
           if (initializer != null) {
             printf("static %s %s = %s;\n", objcType, name, generateExpression(initializer));
           } else {
             printf("static %s %s;\n", objcType, name);
           }
           hadStaticVar = true;
         }
       }
     }
   }
   if (hadStaticVar) {
     newline();
   }
 }
    private boolean fieldCanBeFinal(
        VariableDeclarationFragment fragment, IVariableBinding binding) {
      if (Modifier.isStatic(((FieldDeclaration) fragment.getParent()).getModifiers())) return false;

      if (!fWrittenVariables.containsKey(binding)) {
        // variable is not written
        if (fragment.getInitializer() == null) { // variable is not initialized
          return false;
        } else {
          return true;
        }
      }

      if (fragment.getInitializer() != null) // variable is initialized and written
      return false;

      ITypeBinding declaringClass = binding.getDeclaringClass();
      if (declaringClass == null) return false;

      ArrayList writes = (ArrayList) fWrittenVariables.get(binding);
      if (!isWrittenInTypeConstructors(writes, declaringClass)) return false;

      HashSet writingConstructorBindings = new HashSet();
      ArrayList writingConstructors = new ArrayList();
      for (int i = 0; i < writes.size(); i++) {
        SimpleName name = (SimpleName) writes.get(i);
        MethodDeclaration constructor = getWritingConstructor(name);
        if (writingConstructors.contains(
            constructor)) // variable is written twice or more in constructor
        return false;

        if (canReturn(constructor)) return false;

        writingConstructors.add(constructor);
        IMethodBinding constructorBinding = constructor.resolveBinding();
        if (constructorBinding == null) return false;

        writingConstructorBindings.add(constructorBinding);
      }

      for (int i = 0; i < writingConstructors.size(); i++) {
        MethodDeclaration constructor = (MethodDeclaration) writingConstructors.get(i);
        if (callsWritingConstructor(
            constructor,
            writingConstructorBindings)) // writing constructor calls other writing constructor
        return false;
      }

      MethodDeclaration constructor = (MethodDeclaration) writingConstructors.get(0);
      TypeDeclaration typeDecl =
          (TypeDeclaration) ASTNodes.getParent(constructor, TypeDeclaration.class);
      if (typeDecl == null) return false;

      MethodDeclaration[] methods = typeDecl.getMethods();
      for (int i = 0; i < methods.length; i++) {
        if (methods[i].isConstructor()) {
          IMethodBinding methodBinding = methods[i].resolveBinding();
          if (methodBinding == null) return false;

          if (!writingConstructorBindings.contains(methodBinding)) {
            if (!callsWritingConstructor(
                methods[i],
                writingConstructorBindings)) // non writing constructor does not call a writing
              // constructor
              return false;
          }
        }
      }

      return true;
    }
Exemple #12
0
 public static boolean isStatic(IBinding binding) {
   return Modifier.isStatic(binding.getModifiers());
 }