Esempio n. 1
0
 public void createBCode(CodeGeneration gen) {
   super.createBCode(gen);
   if (hasResult()) {
     TypeDecl type = null;
     BodyDecl b = enclosingBodyDecl();
     if (b instanceof MethodDecl) {
       type = ((MethodDecl) b).type();
     } else {
       throw new Error("Can not create code that returns value within non method");
     }
     getResult().createBCode(gen);
     getResult().type().emitCastTo(gen, type);
     if (!finallyList().isEmpty()) {
       type.emitStoreLocal(gen, resultSaveLocalNum());
     }
     for (Iterator iter = finallyList().iterator(); iter.hasNext(); ) {
       FinallyHost stmt = (FinallyHost) iter.next();
       gen.emitJsr(stmt.label_finally_block());
     }
     if (!finallyList().isEmpty()) {
       type.emitLoadLocal(gen, resultSaveLocalNum());
     }
     type.emitReturn(gen);
   } else {
     for (Iterator iter = finallyList().iterator(); iter.hasNext(); ) {
       FinallyHost stmt = (FinallyHost) iter.next();
       gen.emitJsr(stmt.label_finally_block());
     }
     gen.emitReturn();
   }
 }
 /** @apilevel internal */
 private boolean isSupertypeOfArrayDecl_compute(ArrayDecl type) {
   if (super.isSupertypeOfArrayDecl(type)) return true;
   for (Iterator iter = type.interfacesIterator(); iter.hasNext(); ) {
     TypeDecl typeDecl = (TypeDecl) iter.next();
     if (typeDecl.instanceOf(this)) return true;
   }
   return false;
 }
 /** @apilevel internal */
 private boolean isSupertypeOfClassDecl_compute(ClassDecl type) {
   if (super.isSupertypeOfClassDecl(type)) return true;
   for (Iterator iter = type.interfacesIterator(); iter.hasNext(); ) {
     TypeDecl typeDecl = (TypeDecl) iter.next();
     if (typeDecl.instanceOf(this)) return true;
   }
   return type.hasSuperclass() && type.superclass() != null && type.superclass().instanceOf(this);
 }
Esempio n. 4
0
 /**
  * @ast method
  * @aspect PreciseRethrow
  * @declaredat
  *     /Users/eric/Documents/workspaces/clara-soot/JastAddJ/Java7Frontend/PreciseRethrow.jrag:163
  */
 public void exceptionHandling() {
   Collection<TypeDecl> exceptionTypes = getExpr().throwTypes();
   for (TypeDecl exceptionType : exceptionTypes) {
     if (exceptionType == typeNull()) exceptionType = typeNullPointerException();
     // 8.4.4
     if (!handlesException(exceptionType))
       error("" + this + " throws uncaught exception " + exceptionType.fullName());
   }
 }
 /** @apilevel internal */
 private SimpleSet memberTypes_compute(String name) {
   SimpleSet set = localTypeDecls(name);
   if (!set.isEmpty()) return set;
   for (Iterator outerIter = superinterfacesIterator(); outerIter.hasNext(); ) {
     TypeDecl typeDecl = (TypeDecl) outerIter.next();
     for (Iterator iter = typeDecl.memberTypes(name).iterator(); iter.hasNext(); ) {
       TypeDecl decl = (TypeDecl) iter.next();
       if (!decl.isPrivate()) set = set.add(decl);
     }
   }
   return set;
 }
Esempio n. 6
0
  /**
   * @ast method
   * @aspect Expressions
   * @declaredat
   *     /Users/eric/Documents/workspaces/clara-soot/JastAddExtensions/JimpleBackend/Expressions.jrag:84
   */
  public soot.Value eval(Body b) {
    TypeDecl dest = getDest().type();
    TypeDecl source = getSource().type();
    if (dest.isString()) {

      Value lvalue = getDest().eval(b);

      Value v = asImmediate(b, lvalue);

      // new StringBuffer(left)
      Local local =
          b.newTemp(b.newNewExpr(lookupType("java.lang", "StringBuffer").sootRef(), this));
      b.setLine(this);
      b.add(
          b.newInvokeStmt(
              b.newSpecialInvokeExpr(
                  local,
                  Scene.v()
                      .getMethod("<java.lang.StringBuffer: void <init>(java.lang.String)>")
                      .makeRef(),
                  v,
                  this),
              this));

      // append right
      Local rightResult =
          b.newTemp(
              b.newVirtualInvokeExpr(
                  local,
                  lookupType("java.lang", "StringBuffer")
                      .methodWithArgs("append", new TypeDecl[] {source.stringPromotion()})
                      .sootRef(),
                  asImmediate(b, getSource().eval(b)),
                  this));

      // toString
      Local result =
          b.newTemp(
              b.newVirtualInvokeExpr(
                  rightResult,
                  Scene.v()
                      .getMethod("<java.lang.StringBuffer: java.lang.String toString()>")
                      .makeRef(),
                  this));

      Value v2 = lvalue instanceof Local ? lvalue : (Value) lvalue.clone();
      getDest().emitStore(b, v2, result, this);
      return result;
    } else {
      return super.eval(b);
    }
  }
 /** @apilevel internal */
 private HashMap memberFieldsMap_compute() {
   HashMap map = new HashMap(localFieldsMap());
   for (Iterator outerIter = superinterfacesIterator(); outerIter.hasNext(); ) {
     TypeDecl typeDecl = (TypeDecl) outerIter.next();
     for (Iterator iter = typeDecl.fieldsIterator(); iter.hasNext(); ) {
       FieldDeclaration f = (FieldDeclaration) iter.next();
       if (f.accessibleFrom(this) && !f.isPrivate() && !localFieldsMap().containsKey(f.name())) {
         putSimpleSetElement(map, f.name(), f);
       }
     }
   }
   return map;
 }
Esempio n. 8
0
 /** @apilevel internal */
 private TypeDecl binaryNumericPromotedType_compute() {
   TypeDecl leftType = left().type();
   TypeDecl rightType = right().type();
   if (leftType.isString()) return leftType;
   if (rightType.isString()) return rightType;
   if (leftType.isNumericType() && rightType.isNumericType())
     return leftType.binaryNumericPromotion(rightType);
   if (leftType.isBoolean() && rightType.isBoolean()) return leftType;
   return unknownType();
 }
 /** @apilevel internal */
 private SimpleSet memberFields_compute(String name) {
   SimpleSet fields = localFields(name);
   if (!fields.isEmpty()) return fields;
   for (Iterator outerIter = superinterfacesIterator(); outerIter.hasNext(); ) {
     TypeDecl typeDecl = (TypeDecl) outerIter.next();
     for (Iterator iter = typeDecl.memberFields(name).iterator(); iter.hasNext(); ) {
       FieldDeclaration f = (FieldDeclaration) iter.next();
       if (f.accessibleFrom(this) && !f.isPrivate()) {
         fields = fields.add(f);
       }
     }
   }
   return fields;
 }
  /**
   * @ast method
   * @aspect AccessControl
   * @declaredat /home/uoji/JastAddJ/Java1.4Frontend/AccessControl.jrag:167
   */
  public void accessControl() {
    super.accessControl();

    if (!isCircular()) {
      // 9.1.2
      HashSet set = new HashSet();
      for (int i = 0; i < getNumSuperInterfaceId(); i++) {
        TypeDecl decl = getSuperInterfaceId(i).type();

        if (!decl.isInterfaceDecl() && !decl.isUnknown())
          error(
              "interface " + fullName() + " tries to extend non interface type " + decl.fullName());
        if (!decl.isCircular() && !decl.accessibleFrom(this))
          error(
              "interface " + fullName() + " can not extend non accessible type " + decl.fullName());

        if (set.contains(decl))
          error(
              "extended interface "
                  + decl.fullName()
                  + " mentionened multiple times in extends clause");
        set.add(decl);
      }
    }
  }
Esempio n. 11
0
  // Declared in AnonymousClasses.jrag at line 52
  private AnonymousDecl rewriteRule0() {
    {
      setModifiers(new Modifiers(new List().add(new Modifier("final"))));

      ConstructorDecl constructor = new ConstructorDecl();
      addBodyDecl(constructor);

      constructor.setModifiers((Modifiers) constructorDecl().getModifiers().fullCopy());
      String name = "Anonymous" + nextAnonymousIndex();
      setID(name);
      constructor.setID(name);

      List parameterList = new List();
      for (int i = 0; i < constructorDecl().getNumParameter(); i++) {
        parameterList.add(
            new ParameterDeclaration(
                constructorDecl().getParameter(i).type().createBoundAccess(),
                constructorDecl().getParameter(i).name()));
      }
      constructor.setParameterList(parameterList);

      List argList = new List();
      for (int i = 0; i < constructor.getNumParameter(); i++)
        argList.add(new VarAccess(constructor.getParameter(i).name()));
      constructor.setConstructorInvocation(
          new ExprStmt(new SuperConstructorAccess("super", argList)));
      constructor.setBlock(new Block());

      HashSet set = new HashSet();
      for (int i = 0; i < getNumBodyDecl(); i++) {
        if (getBodyDecl(i) instanceof InstanceInitializer) {
          InstanceInitializer init = (InstanceInitializer) getBodyDecl(i);
          set.addAll(init.exceptions());
        } else if (getBodyDecl(i) instanceof FieldDeclaration) {
          FieldDeclaration f = (FieldDeclaration) getBodyDecl(i);
          if (f.isInstanceVariable()) {
            set.addAll(f.exceptions());
          }
        }
      }
      List exceptionList = new List();
      for (Iterator iter = set.iterator(); iter.hasNext(); ) {
        TypeDecl exceptionType = (TypeDecl) iter.next();
        if (exceptionType.isNull()) exceptionType = typeNullPointerException();
        exceptionList.add(exceptionType.createQualifiedAccess());
      }
      constructor.setExceptionList(exceptionList);
      return this;
    }
  }
 /** @apilevel internal */
 private SimpleSet ancestorMethods_compute(String signature) {
   SimpleSet set = SimpleSet.emptySet;
   for (Iterator outerIter = superinterfacesIterator(); outerIter.hasNext(); ) {
     TypeDecl typeDecl = (TypeDecl) outerIter.next();
     for (Iterator iter = typeDecl.methodsSignature(signature).iterator(); iter.hasNext(); ) {
       MethodDecl m = (MethodDecl) iter.next();
       set = set.add(m);
     }
   }
   if (!superinterfacesIterator().hasNext()) {
     for (Iterator iter = typeObject().methodsSignature(signature).iterator(); iter.hasNext(); ) {
       MethodDecl m = (MethodDecl) iter.next();
       if (m.isPublic()) set = set.add(m);
     }
   }
   return set;
 }
Esempio n. 13
0
 /** @apilevel internal */
 private TypeDecl sourceType_compute() {
   TypeDecl left = getDest().type();
   TypeDecl right = getSource().type();
   if (!left.isString() && !right.isString()) return super.sourceType();
   if (left.isVoid() || right.isVoid()) return unknownType();
   return left.isString() ? left : right;
 }
 /** @apilevel internal */
 private HashMap methodsSignatureMap_compute() {
   HashMap map = new HashMap(localMethodsSignatureMap());
   for (Iterator outerIter = superinterfacesIterator(); outerIter.hasNext(); ) {
     TypeDecl typeDecl = (TypeDecl) outerIter.next();
     for (Iterator iter = typeDecl.methodsIterator(); iter.hasNext(); ) {
       MethodDecl m = (MethodDecl) iter.next();
       if (!m.isPrivate()
           && m.accessibleFrom(this)
           && !localMethodsSignatureMap().containsKey(m.signature()))
         putSimpleSetElement(map, m.signature(), m);
     }
   }
   for (Iterator iter = typeObject().methodsIterator(); iter.hasNext(); ) {
     MethodDecl m = (MethodDecl) iter.next();
     if (m.isPublic() && !map.containsKey(m.signature()))
       putSimpleSetElement(map, m.signature(), m);
   }
   return map;
 }
 /**
  * @ast method
  * @aspect TypeHierarchyCheck
  * @declaredat /home/uoji/JastAddJ/Java1.4Frontend/TypeHierarchyCheck.jrag:312
  */
 public void nameCheck() {
   super.nameCheck();
   if (isCircular()) error("circular inheritance dependency in " + typeName());
   else {
     for (int i = 0; i < getNumSuperInterfaceId(); i++) {
       TypeDecl typeDecl = getSuperInterfaceId(i).type();
       if (typeDecl.isCircular()) error("circular inheritance dependency in " + typeName());
     }
   }
   for (Iterator iter = methodsSignatureMap().values().iterator(); iter.hasNext(); ) {
     SimpleSet set = (SimpleSet) iter.next();
     if (set.size() > 1) {
       Iterator i2 = set.iterator();
       MethodDecl m = (MethodDecl) i2.next();
       while (i2.hasNext()) {
         MethodDecl n = (MethodDecl) i2.next();
         if (!n.mayOverrideReturn(m) && !m.mayOverrideReturn(n))
           error(
               "multiply inherited methods with the same signature must have the same return type");
       }
     }
   }
 }
Esempio n. 16
0
 /**
  * @ast method
  * @aspect PreciseRethrow
  * @declaredat
  *     /Users/eric/Documents/workspaces/clara-soot/JastAddJ/Java7Frontend/PreciseRethrow.jrag:176
  */
 protected boolean reachedException(TypeDecl catchType) {
   Collection<TypeDecl> exceptionTypes = getExpr().throwTypes();
   boolean reached = false;
   for (TypeDecl exceptionType : exceptionTypes) {
     if (exceptionType == typeNull()) exceptionType = typeNullPointerException();
     if (catchType.mayCatch(exceptionType)) {
       reached = true;
       break;
     }
     if (super.reachedException(catchType)) {
       reached = true;
       break;
     }
   }
   return reached;
 }
 /** @apilevel internal */
 private boolean castingConversionTo_compute(TypeDecl type) {
   if (type.isArrayDecl()) {
     return type.instanceOf(this);
   } else if (type.isClassDecl()) {
     return !type.isFinal() || type.instanceOf(this);
   } else if (type.isInterfaceDecl()) {
     for (Iterator i1 = methodsIterator(); i1.hasNext(); ) {
       MethodDecl m = (MethodDecl) i1.next();
       for (Iterator iter = type.methodsSignature(m.signature()).iterator(); iter.hasNext(); ) {
         MethodDecl n = (MethodDecl) iter.next();
         if (n.type() != m.type()) return false;
       }
     }
     return true;
   } else return super.castingConversionTo(type);
 }
  /**
   * @ast method
   * @aspect GenerateClassfile
   * @declaredat /home/uoji/JastAddJ/Java1.4Backend/GenerateClassfile.jrag:142
   */
  public void generateClassfile() {
    super.generateClassfile();
    String fileName = destinationPath() + File.separator + constantPoolName() + ".class";
    if (options().verbose()) System.out.println("Writing class file to " + fileName);
    try {
      ConstantPool cp = constantPool();
      // force building of constant pool
      cp.addClass(constantPoolName());
      cp.addClass("java/lang/Object");
      for (int i = 0; i < getNumSuperInterfaceId(); i++) {
        cp.addClass(getSuperInterfaceId(i).type().constantPoolName());
      }
      for (Iterator iter = bcFields().iterator(); iter.hasNext(); ) {
        FieldDeclaration field = (FieldDeclaration) iter.next();
        cp.addUtf8(field.name());
        cp.addUtf8(field.type().typeDescriptor());
        field.attributes();
      }
      for (Iterator iter = bcMethods().iterator(); iter.hasNext(); ) {
        Object obj = iter.next();
        if (obj instanceof MethodDecl) {
          MethodDecl m = (MethodDecl) obj;
          cp.addUtf8(m.name());
          cp.addUtf8(m.descName());
          m.attributes();
        }
      }
      attributes();

      if (hasClinit()) {
        cp.addUtf8("<clinit>");
        cp.addUtf8("()V");
        clinit_attributes();
      }

      // actual classfile generation
      File dest = new File(fileName);
      File parentFile = dest.getParentFile();
      if (parentFile != null) parentFile.mkdirs();

      FileOutputStream f = new FileOutputStream(fileName);
      DataOutputStream out = new DataOutputStream(new BufferedOutputStream(f));
      out.writeInt(magicHeader());
      out.writeChar(minorVersion());
      out.writeChar(majorVersion());
      cp.emit(out);
      int flags = flags();
      if (isNestedType()) flags = mangledFlags(flags);
      if (isInterfaceDecl()) flags |= Modifiers.ACC_INTERFACE;
      out.writeChar(flags);
      out.writeChar(cp.addClass(constantPoolName()));
      out.writeChar(cp.addClass("java/lang/Object"));
      if (getNumSuperInterfaceId() == 1 && getSuperInterfaceId(0).type().isObject())
        out.writeChar(0);
      else out.writeChar(getNumSuperInterfaceId());
      for (int i = 0; i < getNumSuperInterfaceId(); i++) {
        TypeDecl typeDecl = getSuperInterfaceId(i).type();
        if (typeDecl.isInterfaceDecl()) out.writeChar(cp.addClass(typeDecl.constantPoolName()));
      }
      Collection fields = bcFields();
      out.writeChar(fields.size());
      for (Iterator iter = fields.iterator(); iter.hasNext(); ) {
        FieldDeclaration field = (FieldDeclaration) iter.next();
        out.writeChar(field.flags());
        out.writeChar(cp.addUtf8(field.name()));
        out.writeChar(cp.addUtf8(field.type().typeDescriptor()));
        out.writeChar(field.attributes().size());
        for (Iterator itera = field.attributes().iterator(); itera.hasNext(); )
          ((Attribute) itera.next()).emit(out);
      }
      Collection methods = bcMethods();
      out.writeChar(methods.size() + (hasClinit() ? 1 : 0));
      for (Iterator iter = methods.iterator(); iter.hasNext(); ) {
        BodyDecl b = (BodyDecl) iter.next();
        b.generateMethod(out, cp);
      }
      if (hasClinit()) {
        out.writeChar(Modifiers.ACC_STATIC);
        out.writeChar(cp.addUtf8("<clinit>"));
        out.writeChar(cp.addUtf8("()V"));
        out.writeChar(clinit_attributes().size());
        for (Iterator itera = clinit_attributes().iterator(); itera.hasNext(); )
          ((Attribute) itera.next()).emit(out);
      }
      out.writeChar(attributes().size());
      for (Iterator itera = attributes().iterator(); itera.hasNext(); )
        ((Attribute) itera.next()).emit(out);

      out.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
 /** @apilevel internal */
 private boolean instanceOf_compute(TypeDecl type) {
   return type.isSupertypeOfInterfaceDecl(this);
 }
Esempio n. 20
0
 private boolean narrowingConversionTo_compute(TypeDecl type) {
   return type.isChar();
 }
Esempio n. 21
0
 private boolean subtype_compute(TypeDecl type) {
   return type.supertypeWildcard(this);
 }
Esempio n. 22
0
 private boolean subtype_compute(TypeDecl type) {
   return type.supertypeRawClassDecl(this);
 }
Esempio n. 23
0
  // string addition assign expression
  public void createBCode(CodeGeneration gen) {
    TypeDecl dest = getDest().type();
    TypeDecl source = getSource().type();
    if (dest.isString()) {
      getDest().createAssignLoadDest(gen);

      // new StringBuffer()
      TypeDecl stringBuffer = lookupType("java.lang", "StringBuffer");
      String classname = stringBuffer.constantPoolName();
      String desc;
      int index;
      TypeDecl argumentType;
      stringBuffer.emitNew(gen); // new StringBuffer
      gen.emitDup(); // dup
      desc = "()V";
      index = gen.constantPool().addMethodref(classname, "<init>", desc);
      gen.emit(Bytecode.INVOKESPECIAL, -1).add2(index); // invokespecial StringBuffer()

      gen.emitSwap();

      // append
      argumentType = dest.stringPromotion();
      desc = "(" + argumentType.typeDescriptor() + ")" + stringBuffer.typeDescriptor();
      index = gen.constantPool().addMethodref(classname, "append", desc);
      gen.emit(Bytecode.INVOKEVIRTUAL, -argumentType.variableSize())
          .add2(index); // StringBuffer.append

      getSource().createBCode(gen);

      // typed append
      argumentType = source.stringPromotion();
      desc = "(" + argumentType.typeDescriptor() + ")" + stringBuffer.typeDescriptor();
      index = gen.constantPool().addMethodref(classname, "append", desc);
      gen.emit(Bytecode.INVOKEVIRTUAL, -argumentType.variableSize())
          .add2(index); // StringBuffer.append

      // toString
      desc = "()" + type().typeDescriptor();
      index = gen.constantPool().addMethodref(classname, "toString", desc);
      gen.emit(Bytecode.INVOKEVIRTUAL, 0).add2(index); // StringBuffer.toString

      if (needsPush()) {
        getDest().createPushAssignmentResult(gen);
      }
      getDest().emitStore(gen);
    } else {
      super.createBCode(gen);
    }
  }
Esempio n. 24
0
 public void typeCheck() {
   TypeDecl cond = getCondition().type();
   if (!cond.isBoolean()) {
     error("the type of \"" + getCondition() + "\" is " + cond.name() + " which is not boolean");
   }
 }
Esempio n. 25
0
 public void createAssignOp(CodeGeneration gen, TypeDecl type) {
   type.add(gen);
 }