Ejemplo n.º 1
0
 private static AbstractInsnNode getDefault(Type type) {
   if (type.equals(Type.BOOLEAN_TYPE)) {
     return new LdcInsnNode(0);
   } else if (type.equals(Type.INT_TYPE)) {
     return new LdcInsnNode(0);
   } else if (type.equals(Type.BYTE_TYPE)) {
     return new LdcInsnNode(0);
   } else if (type.equals(Type.CHAR_TYPE)) {
     return new LdcInsnNode(0);
   } else if (type.equals(Type.DOUBLE_TYPE)) {
     return new LdcInsnNode(0.0);
   } else if (type.equals(Type.FLOAT_TYPE)) {
     return new LdcInsnNode(0.0F);
   } else if (type.equals(Type.INT_TYPE)) {
     return new LdcInsnNode(0);
   } else if (type.equals(Type.LONG_TYPE)) {
     return new LdcInsnNode(0L);
   } else if (type.equals(Type.SHORT_TYPE)) {
     return new LdcInsnNode(0);
   } else if (type.equals(Type.VOID_TYPE)) {
     return new LabelNode();
   } else {
     return new InsnNode(Opcodes.ACONST_NULL);
   }
 }
Ejemplo n.º 2
0
  public DirectedGraph<Node, Edge> convert() {
    DirectedGraph<Node, Edge> graph = new SimpleDirectedGraph<Node, Edge>(Edge.factory);

    // Add vertices

    for (Type t : hierarchy) graph.addVertex(Node.get(t));

    // Add edges

    for (Type t : hierarchy) {
      for (Type i : hierarchy.getInterfaces(t)) {
        graph.addVertex(Node.get(i));
        graph.addEdge(Node.get(t), Node.get(i));
      }

      Type sc = hierarchy.getSuperclass(t);

      if (sc == null) {
        assert t.equals(OBJECT) : t;
        continue;
      }

      graph.addVertex(Node.get(sc));
      graph.addEdge(Node.get(t), Node.get(sc));
    }

    // Check for cycles

    if (new CycleDetector<Node, Edge>(graph).detectCycles())
      throw new CyclicHierarchyException(graph.toString());

    return graph;
  }
Ejemplo n.º 3
0
 public static int loadFor(Type type) {
   if (type.equals(Types.BOOLEAN_VALUE)
       || type.equals(Types.INT_VALUE)
       || type.equals(Types.CHAR)
       || type.equals(Types.SHORT_VALUE)) return Opcodes.ILOAD;
   if (type.equals(Types.FLOAT_VALUE)) return Opcodes.FLOAD;
   if (type.equals(Types.LONG_VALUE)) return Opcodes.LLOAD;
   if (type.equals(Types.DOUBLE_VALUE)) return Opcodes.DLOAD;
   return Opcodes.ALOAD;
 }
 /**
  * We've finished processing method, check what annotations were found and then add those that
  * weren't
  */
 @Override
 public void visitEnd() {
   outer:
   for (Class<? extends Annotation> toAddAnn : toAddAnnotations) {
     Type toAddAnnType = Type.getType(toAddAnn);
     if (VERBOSE) System.out.println("Annotation: " + toAddAnn);
     for (String presentAnn : presentAnnotations) {
       if (toAddAnnType.equals(Type.getType(presentAnn))) {
         if (VERBOSE)
           System.out.println("Annotation already present: " + toAddAnn + " " + presentAnn);
         continue outer;
       }
     }
     if (VERBOSE) System.out.println("Adding annotation: " + toAddAnn);
     mv.visitAnnotation("L" + toAddAnnType.getInternalName() + ";", true);
   }
   mv.visitEnd();
 }
  private InsnList fixInstructions(MethodNode originalMethod, CloneMap cloneMap) {
    InsnList instructions = new InsnList();

    for (int k = 0; k < originalMethod.instructions.size(); k++) {
      AbstractInsnNode originalInsn = originalMethod.instructions.get(k);
      switch (originalInsn.getOpcode()) {
          // the put on the field granular field is transformed to a fieldref.set
        case PUTFIELD:
          {
            FieldInsnNode fieldInsn = (FieldInsnNode) originalInsn;
            ClassMetadata ownerMetadata =
                metadataRepository.loadClassMetadata(classLoader, fieldInsn.owner);
            FieldMetadata fieldMetadata = ownerMetadata.getFieldMetadata(fieldInsn.name);
            Type originalFieldType = Type.getType(fieldMetadata.getDesc());

            if (fieldMetadata.hasFieldGranularity()) {

              boolean fieldIsCategory2 = isCategory2(fieldMetadata.getDesc());

              if (fieldIsCategory2) {
                // value(category2), owner,..

                instructions.add(new InsnNode(DUP2_X1));
                // [value(category2), owner, value(category2),...]

                instructions.add(new InsnNode(POP2));
                // [owner, value(category2), ...]
              } else {
                // [value(category1), owner,
                instructions.add(new InsnNode(SWAP));
                // [owner, value(category1),..
              }

              String referenceDesc = findReferenceDesc(fieldMetadata.getDesc());
              String referenceName = Type.getType(referenceDesc).getInternalName();

              instructions.add(
                  new FieldInsnNode(GETFIELD, fieldInsn.owner, fieldInsn.name, referenceDesc));

              if (fieldIsCategory2) {
                // [owner, value(category2),..

                instructions.add(new InsnNode(DUP_X2));
                // [owner, value(category2), owner

                instructions.add(new InsnNode(POP));
                // [value(category2), owner
              } else {
                // [owner, value(category1)
                instructions.add(new InsnNode(SWAP));
                // [value(category1), owner..
              }

              // call the set
              if (originalFieldType.getSort() == Type.ARRAY
                  || originalFieldType.getSort() == Type.OBJECT) {
                String objectDesc = Type.getDescriptor(Object.class);
                MethodInsnNode methodInsn =
                    new MethodInsnNode(
                        INVOKEVIRTUAL,
                        referenceName,
                        "set",
                        format("(%s)%s", objectDesc, objectDesc));
                instructions.add(methodInsn);
              } else {
                MethodInsnNode methodInsn =
                    new MethodInsnNode(
                        INVOKEVIRTUAL,
                        referenceName,
                        "set",
                        format("(%s)%s", fieldMetadata.getDesc(), fieldMetadata.getDesc()));
                instructions.add(methodInsn);
              }

              // pop the unused return value of the set.
              if (fieldIsCategory2) {
                instructions.add(new InsnNode(POP2));
              } else {
                instructions.add(new InsnNode(POP));
              }
            } else {
              instructions.add(originalInsn.clone(cloneMap));
            }
          }
          break;
          // the get on the field granular field is transformed to a fieldref.get
        case GETFIELD:
          {
            FieldInsnNode fieldInsn = (FieldInsnNode) originalInsn;
            FieldMetadata fieldMetadata =
                metadataRepository
                    .loadClassMetadata(classLoader, fieldInsn.owner)
                    .getFieldMetadata(fieldInsn.name);
            if (!fieldMetadata.hasFieldGranularity()) {
              // if it is not getter on a field granular field
              instructions.add(originalInsn.clone(cloneMap));
            } else {
              // it is a getter on a field granular field.
              String referenceDesc = findReferenceDesc(fieldMetadata.getDesc());
              String referenceName = Type.getType(referenceDesc).getInternalName();

              // place the fieldref on the stack.
              instructions.add(
                  new FieldInsnNode(GETFIELD, fieldInsn.owner, fieldInsn.name, referenceDesc));

              Type originalFieldType = Type.getType(fieldMetadata.getDesc());
              if (originalFieldType.getSort() == Type.ARRAY
                  || originalFieldType.getSort() == Type.OBJECT) {
                instructions.add(
                    new MethodInsnNode(
                        INVOKEVIRTUAL,
                        referenceName,
                        "get",
                        format("()%s", getDescriptor(Object.class))));

                if (!originalFieldType.equals(Type.getType(Object.class))) {
                  instructions.add(
                      new TypeInsnNode(CHECKCAST, originalFieldType.getInternalName()));
                }
              } else {
                instructions.add(
                    new MethodInsnNode(
                        INVOKEVIRTUAL,
                        referenceName,
                        "get",
                        format("()%s", fieldMetadata.getDesc())));
              }
            }
          }
          break;
        default:
          instructions.add(originalInsn.clone(cloneMap));
          break;
      }
    }

    return instructions;
  }
Ejemplo n.º 6
0
  private void anon(Type type, Object value) {
    if (anonType == null) anonType = type;
    else if (!anonType.equals(type)) throw new Error();

    anon.add(value);
  }
Ejemplo n.º 7
0
 @Override
 public boolean equals(Object other) {
   return this == other
       || !(other == null || getClass() != other.getClass())
           && methodType.equals(((MethodTypeConstant) other).methodType);
 }
Ejemplo n.º 8
0
 public void customize(CodeEmitter e, Type type) {
   if (type.equals(Constants.TYPE_CLASS)) {
     e.invoke_virtual(Constants.TYPE_CLASS, GET_NAME);
   }
 }
Ejemplo n.º 9
0
 public static void pop(GeneratorAdapter adapter, Type type) {
   if (type.equals(Types.DOUBLE_VALUE)) adapter.pop2();
   else if (type.equals(Types.VOID)) ;
   else adapter.pop();
 }
Ejemplo n.º 10
0
 public static int sizeOf(Type type) {
   if (type.equals(Types.LONG_VALUE) || type.equals(Types.DOUBLE_VALUE)) return 2;
   return 1;
 }