Package org.jboss.util

Examples of org.jboss.util.JBossStringBuilder


      result.addConstructor(constructor);
     
      CtClass object = pool.get(Object.class.getName());
     
      // GET
      JBossStringBuilder buffer = new JBossStringBuilder();
     
      // Signature
      buffer.append("public Object get(Object target) throws Throwable {");

      boolean isInstanceField= Modifier.isStatic(ctField.getModifiers()) == false;

      // Check for null target
      if (check && isInstanceField)
      {
         buffer.append("if (target == null) throw new IllegalArgumentException(\"Null target");
         buffer.append(" for ").append(ctField.getName()).append("\");");
      }

      // Check the target
      CtClass declaring = ctField.getDeclaringClass();
      boolean needsCast = isInstanceField && object.equals(declaring) == false;
      if (check && needsCast)
      {
         buffer.append("if (target instanceof ").append(declaring.getName()).append(" == false) ");
         buffer.append("throw new IllegalArgumentException(\"Target \" + target + \"");
         buffer.append(" is not an instance of ").append(declaring.getName());
         buffer.append(" for ").append(ctField.getName()).append("\");");
      }
     
      // Add a return and box the return type if necessary
      CtClass type = ctField.getType();
      boolean isPrimitive = type.isPrimitive();
      buffer.append("return ");
      if (isPrimitive)
         buffer.append("new ").append(getBoxedType(type)).append('(');

      // Instance field
      if (isInstanceField)
      {
         buffer.append('(');
         if (needsCast)
            buffer.append("(").append(declaring.getName()).append(')');
        
         buffer.append("target).");
      }
      else
      {
         // Static field
         buffer.append(declaring.getName()).append('.');
      }

      // Add the field name
      buffer.append(ctField.getName());

      // Complete the boxing of the return value
      if (isPrimitive)
         buffer.append(')');
     
      buffer.append(";}");

      // Compile it
      String code = buffer.toString();
      CtMethod get = CtNewMethod.make(code, result);
      try
      {
         result.addMethod(get);
      }
      catch (Exception e)
      {
         throw new RuntimeException("Cannot compile " + code, e);
      }
     
      // SET
      buffer = new JBossStringBuilder();
     
      // Signature
      buffer.append("public void set(Object target, Object value) throws Throwable {");

      // Check for null target
      if (check && isInstanceField)
      {
         buffer.append("if (target == null) throw new IllegalArgumentException(\"Null target");
         buffer.append(" for ").append(ctField.getName()).append("\");");
      }

      // Check the target
      if (check && needsCast)
      {
         buffer.append("if (target instanceof ").append(declaring.getName()).append(" == false) ");
         buffer.append("throw new IllegalArgumentException(\"Target \" + target + \"");
         buffer.append(" is not an instance of ").append(declaring.getName());
         buffer.append(" for ").append(ctField.getName()).append("\");");
      }

      // Check the parameter
      if (check)
      {
         if (object.equals(type) == false)
         {
            String paramType = getBoxedType(type);
            // Primitives can't be null
            if (type.isPrimitive())
            {
               buffer.append("if (type == null) ");
               buffer.append("throw new IllegalArgumentException(\"Value ");
               buffer.append(" cannot be null for ").append(type.getName());
               buffer.append(" for ").append(ctField.getName()).append("\");");
            }
            // Check the parameter types
            buffer.append("if (value != null && ");
            buffer.append("value instanceof ").append(paramType).append(" == false) ");
            buffer.append("throw new IllegalArgumentException(\"Value \" + value + \"");
            buffer.append(" is not an instance of ").append(paramType);
            buffer.append(" for ").append(ctField.getName()).append("\");");
         }
      }

      // Instance Field
      if (isInstanceField)
      {
         buffer.append('(');
         if (needsCast)
            buffer.append("(").append(declaring.getName()).append(')');
        
         buffer.append("target).");
      }
      else
      {
         // Static field
         buffer.append(declaring.getName()).append('.');
      }

      // Add the field name
      buffer.append(ctField.getName()).append("=");

      // Add the value
      buffer.append('(');
      // Cast the value
      if (object.equals(type) == false)
          buffer.append("(").append(getBoxedType(type)).append(')');
      buffer.append("value)");
      // Unbox primitive parameters
      if (type.isPrimitive())
         unbox(buffer, type);
     
      buffer.append(";}");

      // Compile it
      code = buffer.toString();
      try
      {
         CtMethod set = CtNewMethod.make(code, result);
         result.addMethod(set);
      }
View Full Code Here


      {
         if (Character.isUpperCase(name.charAt(1)))
            return name;
      }

      JBossStringBuilder buffer = new JBossStringBuilder(name.length());
      buffer.append(Character.toLowerCase(name.charAt(0)));
      if (name.length() > 1)
         buffer.append(name.substring(1));
      return buffer.toString();
   }
View Full Code Here

      return annotations == null || annotations.length == 0;
   }

   public String toString()
   {
      JBossStringBuilder buffer = new JBossStringBuilder();
      Strings.defaultToString(buffer, this);
      buffer.append("[");
      buffer.append(annotated);
      buffer.append("]");
      return buffer.toString();
   }
View Full Code Here

      constructor.setBody("super();");
      result.addConstructor(constructor);
     
      CtClass object = pool.get(Object.class.getName());
     
      JBossStringBuilder buffer = new JBossStringBuilder();
     
      // Signature
      buffer.append("public Object invoke(Object target, Object[] args) throws Throwable {");

      boolean isInstanceMethod = Modifier.isStatic(ctMethod.getModifiers()) == false;

      // Check for null target
      if (check && isInstanceMethod)
      {
         buffer.append("if (target == null) throw new IllegalArgumentException(\"Null target for ");
         buffer.append(ctMethod.getName()).append(ctMethod.getSignature()).append("\");");
      }

      // Check the target
      CtClass declaring = ctMethod.getDeclaringClass();
      boolean needsCast = isInstanceMethod && object.equals(declaring) == false;
      if (check && needsCast)
      {
         buffer.append("if (target instanceof ").append(declaring.getName()).append(" == false) ");
         buffer.append("throw new IllegalArgumentException(\"Target \" + target + \"");
         buffer.append(" is not an instance of ").append(declaring.getName());
         buffer.append(" for ").append(ctMethod.getName()).append(ctMethod.getSignature()).append("\");");
      }

      // Check the parameters
      CtClass[] params = ctMethod.getParameterTypes();
      if (check)
      {
         // Wrong number of args?
         if (params.length != 0)
         {
            buffer.append("if (args == null || args.length != ").append(params.length).append(") ");
            buffer.append("throw new IllegalArgumentException(\"Expected ").append(params.length).append(" parameter(s)");
            buffer.append(" for ").append(ctMethod.getName()).append(ctMethod.getSignature()).append("\");");
         }
         else
         {
            // Didn't expect args
            buffer.append("if (args != null && args.length != 0)");
            buffer.append("throw new IllegalArgumentException(\"Expected no parameters");
            buffer.append(" for ").append(ctMethod.getName()).append(ctMethod.getSignature()).append("\");");
         }
         for (int i = 0; i < params.length; ++i)
         {
            if (object.equals(params[i]) == false)
            {
               String paramType = getBoxedType(params[i]);
               // Primitives can't be null
               if (params[i].isPrimitive())
               {
                  buffer.append("if (args[").append(i).append("] == null) ");
                  buffer.append("throw new IllegalArgumentException(\"Parameter ").append(i);
                  buffer.append(" cannot be null for ").append(params[i].getName());
                  buffer.append(" for ").append(ctMethod.getName()).append(ctMethod.getSignature()).append("\");");
               }
               // Check the parameter types
               buffer.append("if (args[").append(i).append("] != null && ");
               buffer.append("args[").append(i).append("] instanceof ").append(paramType).append(" == false) ");
               buffer.append("throw new IllegalArgumentException(\"Parameter ").append(i).append(" \" + args[").append(i).append("] + \"");
               buffer.append(" is not an instance of ").append(paramType);
               buffer.append(" for ").append(ctMethod.getName()).append(ctMethod.getSignature()).append("\");");
            }
         }
      }
     
      // Add a return and box the return type if necessary
      CtClass returnType = ctMethod.getReturnType();
      boolean isVoid = CtClass.voidType.equals(returnType);
      boolean isPrimitive = returnType.isPrimitive();
      if (isVoid == false)
      {
         buffer.append("return ");
         if (isPrimitive)
            buffer.append("new ").append(getBoxedType(returnType)).append('(');
      }

      // Instance method
      if (isInstanceMethod)
      {
         buffer.append('(');
         if (needsCast)
            buffer.append("(").append(declaring.getName()).append(')');
        
         buffer.append("target).");
      }
      else
      {
         // Static method
         buffer.append(declaring.getName()).append('.');
      }

      // Add the method name
      buffer.append(ctMethod.getName()).append('(');

      // Add the parameters
      for (int i = 0; i < params.length; ++i)
      {
         buffer.append('(');
         // Cast the parameters
         if (object.equals(params[i]) == false)
             buffer.append("(").append(getBoxedType(params[i])).append(')');
         buffer.append("args[").append(i).append("])");
         // Unbox primitive parameters
         if (params[i].isPrimitive())
            unbox(buffer, params[i]);
        
         if (i < params.length - 1)
            buffer.append(", ");
      }
      buffer.append(')');

      // Complete the boxing of the return value
      if (isVoid == false && isPrimitive)
         buffer.append(')');
     
      buffer.append(';');
     
      // Add a return null if there is no return value
      if (isVoid)
         buffer.append("return null;");
      buffer.append('}');

      // Compile it
      String code = buffer.toString();
      try
      {
         CtMethod invoke = CtNewMethod.make(code, result);
         result.addMethod(invoke);
      }
View Full Code Here

      constructor.setBody("super();");
      result.addConstructor(constructor);
     
      CtClass object = pool.get(Object.class.getName());
     
      JBossStringBuilder buffer = new JBossStringBuilder();
     
      // Signature
      buffer.append("public Object newInstance(Object[] args) throws Throwable {");

      String declaring = ctConstructor.getDeclaringClass().getName();
     
      // Check the parameters
      CtClass[] params = ctConstructor.getParameterTypes();
      if (check)
      {
         // Wrong number of args?
         if (params.length != 0)
         {
            buffer.append("if (args == null || args.length != ").append(params.length).append(") ");
            buffer.append("throw new IllegalArgumentException(\"Expected ").append(params.length).append(" parameter(s)");
            buffer.append(" for ").append("new ").append(declaring).append(ctConstructor.getSignature()).append("\");");
         }
         else
         {
            // Didn't expect args
            buffer.append("if (args != null && args.length != 0)");
            buffer.append("throw new IllegalArgumentException(\"Expected no parameters");
            buffer.append(" for ").append("new ").append(declaring).append(ctConstructor.getSignature()).append("\");");
         }
         for (int i = 0; i < params.length; ++i)
         {
            if (object.equals(params[i]) == false)
            {
               String paramType = getBoxedType(params[i]);
               // Primitives can't be null
               if (params[i].isPrimitive())
               {
                  buffer.append("if (args[").append(i).append("] == null) ");
                  buffer.append("throw new IllegalArgumentException(\"Parameter ").append(i);
                  buffer.append(" cannot be null for ").append(params[i].getName());
                  buffer.append(" for ").append("new ").append(declaring).append(ctConstructor.getSignature()).append("\");");
               }
               // Check the parameter types
               buffer.append("if (args[").append(i).append("] != null && ");
               buffer.append("args[").append(i).append("] instanceof ").append(paramType).append(" == false) ");
               buffer.append("throw new IllegalArgumentException(\"Parameter ").append(i).append(" \" + args[").append(i).append("] + \"");
               buffer.append(" is not an instance of ").append(paramType);
               buffer.append(" for ").append("new ").append(declaring).append(ctConstructor.getSignature()).append("\");");
            }
         }
      }
     
      // Add the return new
      buffer.append("return new ").append(declaring).append('(');

      // Add the parameters
      for (int i = 0; i < params.length; ++i)
      {
         buffer.append('(');
         // Cast the parameters
         if (object.equals(params[i]) == false)
             buffer.append("(").append(getBoxedType(params[i])).append(')');
         buffer.append("args[").append(i).append("])");
         // Unbox primitive parameters
         if (params[i].isPrimitive())
            unbox(buffer, params[i]);
        
         if (i < params.length - 1)
            buffer.append(", ");
      }
      buffer.append(");}");

      // Compile it
      String code = buffer.toString();
      try
      {
         CtMethod newInstance = CtNewMethod.make(code, result);
         result.addMethod(newInstance);
      }
View Full Code Here

      result.addConstructor(constructor);
     
      CtClass object = pool.get(Object.class.getName());
     
      // GET
      JBossStringBuilder buffer = new JBossStringBuilder();
     
      // Signature
      buffer.append("public Object get(Object target) throws Throwable {");

      boolean isInstanceField= Modifier.isStatic(ctField.getModifiers()) == false;

      // Check for null target
      if (check && isInstanceField)
      {
         buffer.append("if (target == null) throw new IllegalArgumentException(\"Null target");
         buffer.append(" for ").append(ctField.getName()).append("\");");
      }

      // Check the target
      CtClass declaring = ctField.getDeclaringClass();
      boolean needsCast = isInstanceField && object.equals(declaring) == false;
      if (check && needsCast)
      {
         buffer.append("if (target instanceof ").append(declaring.getName()).append(" == false) ");
         buffer.append("throw new IllegalArgumentException(\"Target \" + target + \"");
         buffer.append(" is not an instance of ").append(declaring.getName());
         buffer.append(" for ").append(ctField.getName()).append("\");");
      }
     
      // Add a return and box the return type if necessary
      CtClass type = ctField.getType();
      boolean isPrimitive = type.isPrimitive();
      buffer.append("return ");
      if (isPrimitive)
         buffer.append("new ").append(getBoxedType(type)).append('(');

      // Instance field
      if (isInstanceField)
      {
         buffer.append('(');
         if (needsCast)
            buffer.append("(").append(declaring.getName()).append(')');
        
         buffer.append("target).");
      }
      else
      {
         // Static field
         buffer.append(declaring.getName()).append('.');
      }

      // Add the field name
      buffer.append(ctField.getName());

      // Complete the boxing of the return value
      if (isPrimitive)
         buffer.append(')');
     
      buffer.append(";}");

      // Compile it
      String code = buffer.toString();
      CtMethod get = CtNewMethod.make(code, result);
      try
      {
         result.addMethod(get);
      }
      catch (Exception e)
      {
         throw new RuntimeException("Cannot compile " + code, e);
      }
     
      // SET
      buffer = new JBossStringBuilder();
     
      // Signature
      buffer.append("public void set(Object target, Object value) throws Throwable {");

      // Check for null target
      if (check && isInstanceField)
      {
         buffer.append("if (target == null) throw new IllegalArgumentException(\"Null target");
         buffer.append(" for ").append(ctField.getName()).append("\");");
      }

      // Check the target
      if (check && needsCast)
      {
         buffer.append("if (target instanceof ").append(declaring.getName()).append(" == false) ");
         buffer.append("throw new IllegalArgumentException(\"Target \" + target + \"");
         buffer.append(" is not an instance of ").append(declaring.getName());
         buffer.append(" for ").append(ctField.getName()).append("\");");
      }

      // Check the parameter
      if (check)
      {
         if (object.equals(type) == false)
         {
            String paramType = getBoxedType(type);
            // Primitives can't be null
            if (type.isPrimitive())
            {
               buffer.append("if (type == null) ");
               buffer.append("throw new IllegalArgumentException(\"Value ");
               buffer.append(" cannot be null for ").append(type.getName());
               buffer.append(" for ").append(ctField.getName()).append("\");");
            }
            // Check the parameter types
            buffer.append("if (value != null && ");
            buffer.append("value instanceof ").append(paramType).append(" == false) ");
            buffer.append("throw new IllegalArgumentException(\"Value \" + value + \"");
            buffer.append(" is not an instance of ").append(paramType);
            buffer.append(" for ").append(ctField.getName()).append("\");");
         }
      }

      // Instance Field
      if (isInstanceField)
      {
         buffer.append('(');
         if (needsCast)
            buffer.append("(").append(declaring.getName()).append(')');
        
         buffer.append("target).");
      }
      else
      {
         // Static field
         buffer.append(declaring.getName()).append('.');
      }

      // Add the field name
      buffer.append(ctField.getName()).append("=");

      // Add the value
      buffer.append('(');
      // Cast the value
      if (object.equals(type) == false)
          buffer.append("(").append(getBoxedType(type)).append(')');
      buffer.append("value)");
      // Unbox primitive parameters
      if (type.isPrimitive())
         unbox(buffer, type);
     
      buffer.append(";}");

      // Compile it
      code = buffer.toString();
      try
      {
         CtMethod set = CtNewMethod.make(code, result);
         result.addMethod(set);
      }
View Full Code Here

      {
         if (Character.isUpperCase(name.charAt(1)))
            return name;
      }

      JBossStringBuilder buffer = new JBossStringBuilder(name.length());
      buffer.append(Character.toLowerCase(name.charAt(0)));
      if (name.length() > 1)
         buffer.append(name.substring(1));
      return buffer.toString();
   }
View Full Code Here

      return annotations == null || annotations.length == 0;
   }

   public String toString()
   {
      JBossStringBuilder buffer = new JBossStringBuilder();
      Strings.defaultToString(buffer, this);
      buffer.append("[");
      buffer.append(annotated);
      buffer.append("]");
      return buffer.toString();
   }
View Full Code Here

               if (ctx.getState().equals(ControllerState.ERROR))
                  errors.add(ctx);
               else
                  incomplete.add(ctx);
            }
            JBossStringBuilder buffer = new JBossStringBuilder();
            buffer.append("Incompletely deployed:\n");
            if (errors.size() != 0)
            {
               buffer.append("\n*** DEPLOYMENTS IN ERROR: Name -> Error\n");
               for (ControllerContext ctx : errors)
               {
                  buffer.append(ctx.getName()).append(" -> ").append(ctx.getError().toString()).append('\n');
               }
            }
            if (incomplete.size() != 0)
            {
               buffer.append("\n*** DEPLOYMENTS MISSING DEPENDENCIES: Name -> Dependency{Required State:Actual State}\n");
               for (ControllerContext ctx : incomplete)
               {
                  Object name = ctx.getName();
                  buffer.append(name).append(" -> ");
                  DependencyInfo dependsInfo = ctx.getDependencyInfo();
                  Set<DependencyItem> depends = dependsInfo.getIDependOn(null);
                  boolean first = true;
                  for (DependencyItem item : depends)
                  {
                     ControllerState dependentState = item.getDependentState();
                     ControllerState otherState = null;
                     ControllerContext other = null;
                     Object iDependOn = item.getIDependOn();
                     if (iDependOn != null)
                     {
                        other = controller.getContext(iDependOn, null);
                        if (other != null)
                           otherState = other.getState();
                     }

                     boolean print = true;
                     if (name.equals(iDependOn))
                        print = false;
                     if (otherState != null && otherState.equals(ControllerState.ERROR) == false)
                     {
                        int index1 = states.indexOf(dependentState);
                        int index2 = states.indexOf(otherState);
                        if (index2 >= index1)
                           print = false;
                     }
                     
                     if (print)
                     {
                        if (first)
                           first = false;
                        else
                           buffer.append(", ");
                       
                        buffer.append(iDependOn).append('{').append(dependentState.getStateString());
                        buffer.append(':');
                        if (iDependOn == null)
                        {
                           buffer.append("** UNRESOLVED " + item.toHumanReadableString() + " **");
                        }
                        else
                        {
                           if (other == null)
                              buffer.append("** NOT FOUND **");
                           else
                              buffer.append(otherState.getStateString());
                        }
                        buffer.append('}');
                     }
                  }
                  buffer.append('\n');
               }
            }
            throw new IllegalStateException(buffer.toString());
         }
      }
   }
View Full Code Here

      {
         if (Character.isUpperCase(name.charAt(1)))
            return name;
      }

      JBossStringBuilder buffer = new JBossStringBuilder(name.length());
      buffer.append(Character.toLowerCase(name.charAt(0)));
      if (name.length() > 1)
         buffer.append(name.substring(1));
      return buffer.toString();
   }
View Full Code Here

TOP

Related Classes of org.jboss.util.JBossStringBuilder

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.