Package dk.brics.string.intermediate

Examples of dk.brics.string.intermediate.StringFromStringBuffer


               
                Variable temp = factory.createVariable(VariableType.STRINGBUFFER);
                Variable result = factory.createVariable(VariableType.STRING);
                factory.addStatement(new StringBufferInit(temp, callee));
                factory.addStatement(new StringBufferUnaryOp(temp, op));
                factory.addStatement(new StringFromStringBuffer(result, temp));
                return result;
               
            // String.replace(CharSequence, CharSequence)
            } else if (methodName.equals("replace") && numArgs == 2 &&
                    isCharSequence(target.getParameterType(0)) &&
                    isCharSequence(target.getParameterType(1))) {
                String arg1 = trackString(expr.getArg(0));
                String arg2 = trackString(expr.getArg(1));
                Variable result = factory.createVariable(VariableType.STRING);
               
                // if either argument is unknown, give up.  [TODO make this better]
                if (arg1 == null || arg2 == null) {
                  factory.addStatement(new StringInit(result, Basic.makeAnyString()));
                  return result;
                }
               
                Replace6 rep = new Replace6(arg1, arg2);
                Variable temp = factory.createVariable(VariableType.STRINGBUFFER);
                factory.addStatement(new StringBufferInit(temp, callee));
                factory.addStatement(new StringBufferUnaryOp(temp, rep));
                factory.addStatement(new StringFromStringBuffer(result, temp));
                return result;
               
            // String.trim()
            } else if (methodName.equals("trim") && numArgs == 0) {
                UnaryOperation op = new Trim();
                Variable temp = factory.createVariable(VariableType.STRINGBUFFER);
                Variable result = factory.createVariable(VariableType.STRING);
                factory.addStatement(new StringBufferInit(temp, callee));
                factory.addStatement(new StringBufferUnaryOp(temp, op));
                factory.addStatement(new StringFromStringBuffer(result, temp));
                return result;
           
            // String.substring(int)    [this method returns a suffix of the string, starting at the specified index]
            } else if (methodName.equals("substring") && numArgs == 1) {
                UnaryOperation op = new Postfix();
                Variable temp = factory.createVariable(VariableType.STRINGBUFFER);
                Variable result = factory.createVariable(VariableType.STRING);
                factory.addStatement(new StringBufferInit(temp, callee));
                factory.addStatement(new StringBufferUnaryOp(temp, op));
                factory.addStatement(new StringFromStringBuffer(result, temp));
                return result;
           
            // String.substring(int,int)
            } else if (methodName.equals("substring") && numArgs == 2) {
                UnaryOperation op;
                Integer arg1 = trackInteger(expr.getArg(0));
                if (arg1 != null && arg1.intValue() == 0) {
                    op = new Prefix();
                } else {
                    op = new Substring();
                }
                Variable temp = factory.createVariable(VariableType.STRINGBUFFER);
                Variable result = factory.createVariable(VariableType.STRING);
                factory.addStatement(new StringBufferInit(temp, callee));
                factory.addStatement(new StringBufferUnaryOp(temp, op));
                factory.addStatement(new StringFromStringBuffer(result, temp));
                return result;
           
            // String.toLowerCase()
            } else if (methodName.equals("toLowerCase") && numArgs == 0) {
                UnaryOperation op = new ToLowerCase();
                Variable temp = factory.createVariable(VariableType.STRINGBUFFER);
                Variable result = factory.createVariable(VariableType.STRING);
                factory.addStatement(new StringBufferInit(temp, callee));
                factory.addStatement(new StringBufferUnaryOp(temp, op));
                factory.addStatement(new StringFromStringBuffer(result, temp));
                return result;
           
            // String.toUpperCase()
            } else if (methodName.equals("toUpperCase") && numArgs == 0) {
                UnaryOperation op = new ToUpperCase();
                Variable temp = factory.createVariable(VariableType.STRINGBUFFER);
                Variable result = factory.createVariable(VariableType.STRING);
                factory.addStatement(new StringBufferInit(temp, callee));
                factory.addStatement(new StringBufferUnaryOp(temp, op));
                factory.addStatement(new StringFromStringBuffer(result, temp));
                return result;
           
            // String.split(String)
            } else if (methodName.equals("split") && numArgs == 1) {
                Variable result = factory.createVariable(VariableType.ARRAY);
                factory.addStatement(new BasicUnaryOp(result, callee, new Split()));
                return result;
           
            // String.charAt(int)
            } else if (methodName.equals("charAt") && numArgs == 1) {
                UnaryOperation op;
                Integer arg = trackInteger(expr.getArg(0));
                if (arg != null) {
                    op = new CharAt1(arg);
                } else {
                    op = new CharAt2();
                }
                Variable result = factory.createVariable(VariableType.PRIMITIVE);
                factory.addStatement(new BasicUnaryOp(result, callee, op));
                return result;
               
            // String.contains(CharSequence)
            } else if (methodName.equals("contains") && numArgs == 1) {
                // this is experimental stuff. it is working, but probably not that useful
                Variable result = factory.createVariable(VariableType.PRIMITIVE);
                factory.addStatement(new BasicBinaryOp(result, callee, arguments.get(0), new Contains()));
                return result;
           
           
            // String.contentEquals(CharSequence) and String.contentEquals(StringBuffer)
            } else if (methodName.equals("contentEquals") && numArgs == 1) {
                // we can't say anything meaningful except the argument is NOT corrupted
                // (the argument will be considered corrupted if we do not handle it here)
                Variable result = factory.createVariable(VariableType.PRIMITIVE);
                factory.addStatement(new PrimitiveInit(result, Basic.getBinaryBooleans()));
                return result;
            }
           
            // String.toCharArray()
            else if (methodName.equals("toCharArray") && numArgs == 0) {
                Variable result = factory.createVariable(VariableType.ARRAY);
                factory.addStatement(new ArrayNew(result));
                Variable charAt = factory.createVariable(VariableType.PRIMITIVE);
                factory.addStatement(new BasicUnaryOp(charAt, callee, new CharAt2()));
                factory.addStatement(new ArrayWriteElement(result, charAt));
                return result;
            }
           
     
          return null;
     
    }
   
    //
    //    STRINGBUFFER
    //
    else if (isBufferOrBuilder(declaringClass)) {
      if (methodName.equals("toString") && numArgs == 0) {
        Variable result = factory.createVariable(VariableType.STRING);
                factory.addStatement(new StringFromStringBuffer(result, callee));
                return result;
            }
            // StringBuffer.append(<any type>)
            else if (methodName.equals("append") && numArgs == 1) {
                Variable rvar = valueOf(expr.getArgBox(0), arguments.get(0), 10, target.getParameterType(0), factory);
                factory.addStatement(new StringBufferAppend(callee, rvar));
                return callee;
            }
            // StringBuffer.insert(int, <any type>)
            else if (methodName.equals("insert") && numArgs == 2 &&
                    isInt(target.getParameterType(0))) {
                Integer pos = trackInteger(expr.getArg(0));
                Variable rvar = valueOf(expr.getArgBox(1), arguments.get(1), 10, target.getParameterType(0), factory);
                if (pos != null && pos.intValue() == 0) {
                  factory.addStatement(new StringBufferPrepend(callee, rvar));
                } else {
                  factory.addStatement(new StringBufferBinaryOp(callee, new Insert(), rvar));
                }
                return callee;
            }
            // StringBuffer.delete(int,int)
            else if (methodName.equals("delete") && numArgs == 2) {
                UnaryOperation op = new Delete();
                factory.addStatement(new StringBufferUnaryOp(callee, op));
                return callee;
            }
            // StringBuffer.deleteCharAt(int)
            else if (methodName.equals("deleteCharAt") && numArgs == 1) {
                UnaryOperation op = new DeleteCharAt();
                factory.addStatement(new StringBufferUnaryOp(callee, op));
                return callee;
            }
            // StringBuffer.replace(int start, int end, String replacement)
            else if (methodName.equals("replace") && numArgs == 3) {
                BinaryOperation op = new Replace5();
                Variable rvar = valueOf(expr.getArgBox(2), arguments.get(2), 10, target.getParameterType(2), factory);
                factory.addStatement(new StringBufferBinaryOp(callee, op, rvar));
                return callee;
            }
            // StringBuffer.reverse()
            else if (methodName.equals("reverse") && numArgs == 0) {
                UnaryOperation op = new Reverse();
                factory.addStatement(new StringBufferUnaryOp(callee, op));
                return callee;
            }
            // StringBuffer.setCharAt(int, char)  [NOTE: This method returns void]
            else if (methodName.equals("setCharAt") && numArgs == 2) {
                Integer c = trackInteger(expr.getArg(1));
                if (c == null) {
                    UnaryOperation op = new SetCharAt2();
                    factory.addStatement(new StringBufferUnaryOp(callee, op));
                } else {
                    UnaryOperation op = new SetCharAt1((char) c.intValue());
                    factory.addStatement(new StringBufferUnaryOp(callee, op));
                }
                return factory.getNothing();
            }
            // StringBuffer.setLength(int)      [NOTE: This method returns void]
            else if (methodName.equals("setLength") && numArgs == 1) {
                UnaryOperation op = new SetLength();
                factory.addStatement(new StringBufferUnaryOp(callee, op));
                return factory.getNothing();// returns void
            }
            // StringBuffer.substring(int)      [NOTE: Returns a string]
            else if (methodName.equals("substring") && numArgs == 1) {
                UnaryOperation op = new Postfix();

                Variable result = factory.createVariable(VariableType.STRING);
               
                // clone the stringbuffer
                Variable clone = makeStringBufferClone(callee, factory);
               
                // perform the substring operation on the clone
                factory.addStatement(new StringBufferUnaryOp(clone, op));
               
                // now put the clone's value back into the result variable
                factory.addStatement(new StringFromStringBuffer(result, clone));
               
                return result;
           
            // StringBuffer.substring(int,int)    [NOTE: Returns a string]
            else if (methodName.equals("substring") && numArgs == 2) {
                UnaryOperation op;
                Integer arg1 = trackInteger(expr.getArg(0));
                if (arg1 != null && arg1.intValue() == 0) {
                    op = new Prefix();
                } else {
                    op = new Substring();
                }
               
                Variable result = factory.createVariable(VariableType.STRING);
               
                // clone the stringbuffer
                Variable clone = makeStringBufferClone(callee, factory);
               
                // perform the substring operation on the clone
                factory.addStatement(new StringBufferUnaryOp(clone, op));
               
                // now put the clone's value back into the result variable
                factory.addStatement(new StringFromStringBuffer(result, clone));
               
                return result;
            }
     
            else if (STRING_BUFFER_IGNORED_METHODS_AUTO.run(methodName)) {
View Full Code Here


                return true;
      }  
     
      // new String(StringBuffer); new String(StringBuilder)
            if (numArgs == 1 && isBufferOrBuilder(method.getParameterType(0))) {
        factory.addStatement(new StringFromStringBuffer(callee, arguments.get(0)));
                return true;
            }
     
          // unsupported constructor
          // make any string. this is slightly better than returning false,
View Full Code Here

        Value val = box.getValue();
        Variable result = factory.createVariable(VariableType.STRING);
        switch (argument.getType()) {
        case STRINGBUFFER:
          if (isStringBuffer(val.getType()) || isStringBuilder(val.getType())) {
            factory.addStatement(new StringFromStringBuffer(result, argument));
          } else {
            // if the type implements Appendable, we can't really know its value
            factory.addStatement(new StringInit(result, Basic.makeAnyString()));
          }
          break;
         
        case ARRAY:
          if (val.getType() instanceof ArrayType) {  // the argument's type only says it *might* be an array
            // TODO because of covariant arrays, we might not actually know it is a String array??
            factory.addStatement(new StringInit(result, ARRAY_OBJECT_AUTOMATON));
          } else {
            factory.addStatement(new StringInit(result, Basic.makeAnyString()));
          }
          break;
         
        case OBJECT:
          Automaton auto = Basic.makeAnyString();
          if (val.getType() instanceof RefType) {
            Automaton resolved = factory.resolveToStringMethod(((RefType)val.getType()).getSootClass());
            if (resolved != null)
              auto = resolved;
          }
          factory.addStatement(new StringInit(result, auto));
          break;
         
        case NULL:
          factory.addStatement(new StringInit(result, Automatons.getNull()));
          break;
         
        case NONE:
            if (val instanceof Constant) {
              String s = constantToString((Constant) val, radix, type);
              if (s != null)
                factory.addStatement(new StringInit(result, Basic.makeConstString(s)));
              else
                factory.addStatement(new StringInit(result, Basic.makeAnyString()));
               
            } else if (radix == 10 && Automatons.fromType(type.toString()) != null) { // TODO: handle radix!=10
             
              factory.addStatement(new StringInit(result, Automatons.fromType(type.toString())));
             
            } else if (val.getType() instanceof RefType) {
                // Call the corresponding toString method
                Method tostring_method = factory.getToStringMethod(((RefType) val.getType()).getSootClass());
                if (tostring_method != null && !isInterface(val.getType())) {
                  factory.addStatement(new Call(result, tostring_method, new Variable[0]));
                } else {
                  factory.addStatement(new StringInit(result, Basic.makeAnyString()));
                }
            } else { // If all else fails, give any string
              factory.addStatement(new StringInit(result, Basic.makeAnyString())); // not currently reachable, but good to have here
            }
           
            break;
           
        case PRIMITIVE:
            // TODO: If type is another primitive type, add a UnaryOp to extract this primitive type as a boolean, int, etc.
            if (val instanceof Constant) {
                // NOTE: This for char-variables, this does the same as the if-clause below, but is a bit more efficient
                factory.addStatement(new StringInit(result, Basic.makeConstString(constantToString((Constant) val, radix, type))));
               
            } else if (type.equals(CharType.v())) {
                // create a string buffer, append the char, convert it to a string and return that
                Variable tmp = factory.createVariable(VariableType.STRINGBUFFER);
                factory.addStatement(new StringBufferInit(tmp, makeStringConstant("", factory)));
                factory.addStatement(new StringBufferAppendChar(tmp, argument));
                factory.addStatement(new StringFromStringBuffer(result, tmp));
           
            } else if (type.equals(BooleanType.v())) {
                // use a BooleanToString operation
                factory.addStatement(new BasicUnaryOp(result, argument, new BooleanToString()));
               
View Full Code Here

      if (buffer == null || buffer.getType() != VariableType.STRINGBUFFER) {
        throw new IllegalArgumentException("Can only clone STRINGBUFFER variables");
      }
      // get the current possible values of the buffer
      Variable currentValue = factory.createVariable(VariableType.STRING);
      factory.addStatement(new StringFromStringBuffer(currentValue, buffer));
     
      // create a new string buffer with this initial value
      Variable clone = factory.createVariable(VariableType.STRINGBUFFER);
      factory.addStatement(new StringBufferInit(clone, currentValue));
     
View Full Code Here

TOP

Related Classes of dk.brics.string.intermediate.StringFromStringBuffer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.