@SuppressWarnings("unchecked")
 public static void accept(String name, Object v, AnnotationVisitor av) {
   if (v instanceof AnnotationNode) {
     AnnotationNode a = (AnnotationNode) v;
     AnnotationVisitor av1 = av.visitAnnotation(name, a.type);
     accept(a.items, av1);
     av1.visitEnd();
   } else if (v instanceof Field) {
     Field e = (Field) v;
     av.visitEnum(name, e.getOwner(), e.getName());
   } else if (v instanceof List) {
     List<Object> list = (List<Object>) v;
     AnnotationVisitor av1 = av.visitArray(name);
     for (Object i : list) {
       accept(null, i, av1);
     }
     av1.visitEnd();
   } else if (v instanceof Method) {
     // Method method = (Method) v;
     // AnnotationVisitor av1 = av.visitAnnotation(item.name, "Lcom.googlecode.Method;");
     // av1.visit("owner", method.getOwner());
     // av1.visit("name", method.getName());
     // av1.visit("desc", method.getType().getDesc());
     // av1.visitEnd();
     av.visit(name, v);
   } else if (v instanceof DexType) {
     av.visit(name, ((DexType) v).desc);
   } else {
     av.visit(name, v);
   }
 }
Example #2
0
 private void addAnnotation() {
   if (!isAnnotationPresent) {
     AnnotationVisitor av = cv.visitAnnotation(annotationDesc, true);
     if (av != null) {
       av.visitEnd();
     }
     isAnnotationPresent = true;
   }
 }
  /* (non-Javadoc)
   * @see edu.umd.cs.findbugs.ba.INullnessAnnotationDatabase#addDefaultAnnotation(java.lang.String, java.lang.String, edu.umd.cs.findbugs.ba.NullnessAnnotation)
   */
  public void addDefaultAnnotation(Target target, String c, NullnessAnnotation n) {
    if (DEBUG) {
      System.out.println("addDefaultAnnotation: target=" + target + ", c=" + c + ", n=" + n);
    }

    ClassDescriptor classDesc =
        DescriptorFactory.instance().getClassDescriptorForDottedClassName(c);
    ClassInfo xclass;

    // Get the XClass (really a ClassInfo object)
    try {
      xclass = (ClassInfo) Global.getAnalysisCache().getClassAnalysis(XClass.class, classDesc);
    } catch (MissingClassException e) {
      //
      //	AnalysisContext.currentAnalysisContext().getLookupFailureCallback().reportMissingClass(e.getClassDescriptor());
      return;
    } catch (CheckedAnalysisException e) {
      //			AnalysisContext.logError("Error adding built-in nullness annotation", e);
      return;
    }
    if (n == NullnessAnnotation.NONNULL && target == AnnotationDatabase.Target.PARAMETER) {
      xclass.addAnnotation(new AnnotationValue(PARAMETERS_ARE_NONNULL_BY_DEFAULT));
      return;
    } else if (n == NullnessAnnotation.NONNULL && target == AnnotationDatabase.Target.METHOD) {
      xclass.addAnnotation(new AnnotationValue(RETURN_VALUES_ARE_NONNULL_BY_DEFAULT));
      return;
    }
    // Get the default annotation type
    ClassDescriptor defaultAnnotationType;
    if (target == AnnotationDatabase.Target.ANY) {
      defaultAnnotationType = FindBugsDefaultAnnotations.DEFAULT_ANNOTATION;
    } else if (target == AnnotationDatabase.Target.FIELD) {
      defaultAnnotationType = FindBugsDefaultAnnotations.DEFAULT_ANNOTATION_FOR_FIELDS;
    } else if (target == AnnotationDatabase.Target.METHOD) {
      defaultAnnotationType = FindBugsDefaultAnnotations.DEFAULT_ANNOTATION_FOR_METHODS;
    } else if (target == AnnotationDatabase.Target.PARAMETER) {
      defaultAnnotationType = FindBugsDefaultAnnotations.DEFAULT_ANNOTATION_FOR_PARAMETERS;
    } else {
      throw new IllegalArgumentException("Unknown target for default annotation: " + target);
    }

    // Get the JSR-305 nullness annotation type
    ClassDescriptor nullnessAnnotationType = getNullnessAnnotationClassDescriptor(n);

    // Construct an AnnotationValue containing the default annotation
    AnnotationValue annotationValue = new AnnotationValue(defaultAnnotationType);
    AnnotationVisitor v = annotationValue.getAnnotationVisitor();
    v.visit("value", Type.getObjectType(nullnessAnnotationType.getClassName()));
    v.visitEnd();

    if (DEBUG) {
      System.out.println("Adding AnnotationValue " + annotationValue + " to class " + xclass);
    }

    // Destructively add the annotation to the ClassInfo object
    xclass.addAnnotation(annotationValue);
  }
Example #4
0
  public static void visitAnnotationFields(AnnotationVisitor visitor, Map<String, Object> fields) {
    try {
      for (Map.Entry<String, Object> fieldEntry : fields.entrySet()) {
        Object value = fieldEntry.getValue();
        String key = fieldEntry.getKey();

        if (value instanceof Map) {
          @SuppressWarnings("unchecked")
          Map<Class, Map<String, Object>> nestedAnnotationMap =
              (Map<Class, Map<String, Object>>) value;

          for (Map.Entry<Class, Map<String, Object>> nestedAnnotation :
              nestedAnnotationMap.entrySet()) {
            AnnotationVisitor annotationV;

            annotationV =
                visitor.visitAnnotation(
                    key, Type.getType(nestedAnnotation.getKey()).getDescriptor());
            visitAnnotationFields(annotationV, nestedAnnotation.getValue());
            annotationV.visitEnd();
          }
        } else if (value.getClass().isArray()) {
          Object[] values = (Object[]) value;

          AnnotationVisitor arrayV = visitor.visitArray(key);
          for (int i = 0; i < values.length; i++) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put(null, values[i]);
            visitAnnotationFields(arrayV, map);
          }
          arrayV.visitEnd();
        } else if (value.getClass().isEnum()) {
          visitor.visitEnum(key, ci(value.getClass()), value.toString());
        } else if (value instanceof Class) {
          visitor.visit(key, Type.getType((Class) value));
        } else {
          visitor.visit(key, value);
        }
      }
    } catch (ClassCastException e) {
      throw new InvalidAnnotationDescriptorException(
          "Fields "
              + fields
              + " did not match annotation format.  See CodegenUtils#visitAnnotationFields for format",
          e);
    }
  }
  private void writeFlags(BitSet flags) {
    int flagsValue = 0;
    for (int bit = 0; bit < flags.length(); bit++) {
      if (flags.get(bit)) {
        flagsValue |= (1 << bit);
      }
    }

    if (flagsValue != JvmStdlibNames.JET_METHOD_FLAGS_DEFAULT_VALUE) {
      av.visit(JvmStdlibNames.JET_METHOD_FLAGS_FIELD, flagsValue);
    }
  }
 /**
  * Handles the writing of a single annotation to an annotation visitor.
  *
  * @param annotationVisitor The annotation visitor the write process is to be applied on.
  * @param annotation The annotation to be written.
  * @param annotationValueFilter The value filter to apply for discovering which values of an
  *     annotation should be written.
  */
 private static void handle(
     AnnotationVisitor annotationVisitor,
     AnnotationDescription annotation,
     AnnotationValueFilter annotationValueFilter) {
   for (MethodDescription.InDefinedShape methodDescription :
       annotation.getAnnotationType().getDeclaredMethods()) {
     if (annotationValueFilter.isRelevant(annotation, methodDescription)) {
       apply(
           annotationVisitor,
           methodDescription.getReturnType().asErasure(),
           methodDescription.getName(),
           annotation.getValue(methodDescription));
     }
   }
   annotationVisitor.visitEnd();
 }
Example #7
0
 public static void visitAnnotationFields(AnnotationVisitor visitor, Map<String, Object> fields) {
   for (Map.Entry<String, Object> fieldEntry : fields.entrySet()) {
     Object value = fieldEntry.getValue();
     if (value.getClass().isArray()) {
       Object[] values = (Object[]) value;
       AnnotationVisitor arrayV = visitor.visitArray(fieldEntry.getKey());
       for (int i = 0; i < values.length; i++) {
         arrayV.visit(null, values[i]);
       }
       arrayV.visitEnd();
     } else if (value.getClass().isEnum()) {
       visitor.visitEnum(fieldEntry.getKey(), ci(value.getClass()), value.toString());
     } else if (value instanceof Class) {
       visitor.visit(fieldEntry.getKey(), Type.getType((Class) value));
     } else {
       visitor.visit(fieldEntry.getKey(), value);
     }
   }
 }
 /**
  * Performs the writing of a given annotation value to an annotation visitor.
  *
  * @param annotationVisitor The annotation visitor the write process is to be applied on.
  * @param valueType The type of the annotation value.
  * @param name The name of the annotation type.
  * @param value The annotation's value.
  */
 public static void apply(
     AnnotationVisitor annotationVisitor, TypeDescription valueType, String name, Object value) {
   if (valueType.isAnnotation()) {
     handle(
         annotationVisitor.visitAnnotation(name, valueType.getDescriptor()),
         (AnnotationDescription) value,
         AnnotationValueFilter.Default.APPEND_DEFAULTS);
   } else if (valueType.isEnum()) {
     annotationVisitor.visitEnum(
         name, valueType.getDescriptor(), ((EnumerationDescription) value).getValue());
   } else if (valueType.isAssignableFrom(Class.class)) {
     annotationVisitor.visit(name, Type.getType(((TypeDescription) value).getDescriptor()));
   } else if (valueType.isArray()) {
     AnnotationVisitor arrayVisitor = annotationVisitor.visitArray(name);
     int length = Array.getLength(value);
     TypeDescription componentType = valueType.getComponentType();
     for (int index = 0; index < length; index++) {
       apply(arrayVisitor, componentType, NO_NAME, Array.get(value, index));
     }
     arrayVisitor.visitEnd();
   } else {
     annotationVisitor.visit(name, value);
   }
 }
 public void writePropertyType(@NotNull String propertyType) {
   if (propertyType.length() > 0) {
     av.visit(JvmStdlibNames.JET_METHOD_PROPERTY_TYPE_FIELD, propertyType);
   }
 }
Example #10
0
 @Override
 public void visit(String name, Object value) {
   super.visit(name, value);
 }
Example #11
0
 @Override
 public void visitEnum(String name, String desc, String value) {
   super.visitEnum(name, desc, value);
 }
Example #12
0
 @Override
 public void visitEnd() {
   super.visitEnd();
 }
Example #13
0
 public void accept(ClassVisitor cv) {
   AnnotationVisitor av = cv.visitAnnotation(type, visible);
   accept(items, av);
   av.visitEnd();
 }
 public void visitEnd() {
   av.visitEnd();
 }
Example #15
0
 public void accept(FieldVisitor fv) {
   AnnotationVisitor av = fv.visitAnnotation(type, visible);
   accept(items, av);
   av.visitEnd();
 }
 public void writeKind(int kind) {
   if (kind != JvmStdlibNames.JET_METHOD_KIND_DEFAULT) {
     av.visit(JvmStdlibNames.JET_METHOD_KIND_FIELD, kind);
   }
 }
 public void writeReturnType(@NotNull String returnType) {
   if (returnType.length() > 0) {
     av.visit(JvmStdlibNames.JET_METHOD_RETURN_TYPE_FIELD, returnType);
   }
 }
 public void writeTypeParameters(@NotNull String typeParameters) {
   if (typeParameters.length() > 0) {
     av.visit(JvmStdlibNames.JET_METHOD_TYPE_PARAMETERS_FIELD, typeParameters);
   }
 }
 public void writeNullableReturnType(boolean nullableReturnType) {
   if (nullableReturnType) {
     av.visit(JvmStdlibNames.JET_METHOD_NULLABLE_RETURN_TYPE_FIELD, nullableReturnType);
   }
 }
Example #20
0
 public void visitParameterAnnotationWithFields(
     int param, String name, boolean visible, Map<String, Object> fields) {
   AnnotationVisitor visitor = visitParameterAnnotation(param, name, visible);
   visitAnnotationFields(visitor, fields);
   visitor.visitEnd();
 }