Example usage for org.apache.commons.lang ArrayUtils subarray

List of usage examples for org.apache.commons.lang ArrayUtils subarray

Introduction

In this page you can find the example usage for org.apache.commons.lang ArrayUtils subarray.

Prototype

public static boolean[] subarray(boolean[] array, int startIndexInclusive, int endIndexExclusive) 

Source Link

Document

Produces a new boolean array containing the elements between the start and end indices.

Usage

From source file:org.xwiki.platform.patchservice.impl.PositionImpl.java

/**
 * {@inheritDoc}/*from  w w w  .j  a  v a2 s  . c  om*/
 */
public String getTextAfterPosition(String text) {
    String[] rows = StringUtils.splitPreserveAllTokens(text, SEPARATOR);
    if (ArrayUtils.getLength(rows) <= this.row) {
        return "";
    }
    String textAfter = StringUtils.substring(rows[this.row], this.column)
            + ((this.row + 1 < rows.length) ? SEPARATOR : "")
            + StringUtils.join(ArrayUtils.subarray(rows, this.row + 1, rows.length), SEPARATOR);
    return (this.span <= 0) ? textAfter : StringUtils.substring(textAfter, this.span);
}

From source file:org.zaproxy.zap.extension.httppanel.view.impl.models.http.request.RequestByteHttpPanelViewModel.java

@Override
public void setData(byte[] data) {
    int pos = findHeaderLimit(data);

    if (pos == -1) {
        logger.warn("Could not Save Header, limit not found. Header: " + new String(data));
        return;/*w  ww.java2  s  .  com*/
    }

    try {
        httpMessage.setRequestHeader(new String(data, 0, pos));
    } catch (HttpMalformedHeaderException e) {
        logger.warn("Could not Save Header: " + new String(data, 0, pos), e);
    }

    httpMessage.getRequestBody().setBody(ArrayUtils.subarray(data, pos, data.length));
    HttpPanelViewModelUtils.updateRequestContentLength(httpMessage);
}

From source file:org.zaproxy.zap.extension.httppanel.view.impl.models.http.response.ResponseByteHttpPanelViewModel.java

@Override
public void setData(byte[] data) {
    int pos = findHeaderLimit(data);

    if (pos == -1) {
        logger.warn("Could not Save Header, limit not found. Header: " + new String(data));
        return;//from w w w .  ja va2  s . c  om
    }

    try {
        httpMessage.setResponseHeader(new String(data, 0, pos));
    } catch (HttpMalformedHeaderException e) {
        logger.warn("Could not Save Header: " + Arrays.toString(data), e);
    }

    httpMessage.getResponseBody().setBody(ArrayUtils.subarray(data, pos, data.length));
    HttpPanelViewModelUtils.updateResponseContentLength(httpMessage);
}

From source file:org.zaproxy.zap.extension.httppanel.view.models.request.RequestByteHttpPanelViewModel.java

@Override
public void setData(byte[] data) {
    int pos = findHeaderLimit(data);

    if (pos == -1) {
        log.warn("Could not Save Header, limit not found. Header: " + new String(data));
        return;/*  ww w .  ja v  a2 s . co m*/
    }

    try {
        httpMessage.setRequestHeader(new String(data, 0, pos));
    } catch (HttpMalformedHeaderException e) {
        log.warn("Could not Save Header: " + new String(data, 0, pos), e);
    }

    httpMessage.getRequestBody().setBody(ArrayUtils.subarray(data, pos, data.length));
}

From source file:org.zaproxy.zap.extension.httppanel.view.models.response.ResponseByteHttpPanelViewModel.java

@Override
public void setData(byte[] data) {
    int pos = findHeaderLimit(data);

    if (pos == -1) {
        log.warn("Could not Save Header, limit not found. Header: " + new String(data));
        return;//from w w w .j a  va2  s  .c om
    }

    try {
        httpMessage.setResponseHeader(new String(data, 0, pos));
    } catch (HttpMalformedHeaderException e) {
        log.warn("Could not Save Header: " + Arrays.toString(data), e);
    }

    httpMessage.getResponseBody().setBody(ArrayUtils.subarray(data, pos, data.length));
}

From source file:padl.creator.classfile.AbstractClassFileCreator.java

private void createEntities(final ICodeLevelModel aCodeLevelModel, final List listOfSourceConstituents,
        final ConstituentsRepository constituentsRepository, final ClassFile[] listOfEntities) {

    // Yann 2006/02/03: Spring cleaning.
    // Handling member classes makes it harder and harder to
    // analyse entities using the legacy code below. I rewrite
    // this algorithm to perform the creation of entities
    // (including membre entitie) and their connection
    // (inheritance and implementation) in two pass:
    // 1. I create all the entities, including member entities.
    //    I put the entities in the right packages.
    // 2. I connect the entities with one another through
    //    inheritance and implementation, as appropriate.

    // 1.// w  w w. j  av  a  2s . co  m
    Iterator iterator = listOfSourceConstituents.iterator();
    while (iterator.hasNext()) {
        final ClassFile classFile = (ClassFile) iterator.next();
        final String entityDisplayID = classFile.getName();
        char[] entityID = entityDisplayID.toCharArray();

        // I assume that the classfiles are sorted such as member
        // entities appear *after* their containing entities.

        // Yann 2011/10/01: Local entity!
        // There is an entity with ID: 
        //   javax.swing.JSlider$1$SmartHashtable$LabelUIResource
        // that must be dealt with as a local entity.
        if (!Utils.isAnonymousOrLocalEntity(entityID) && !Utils.isLocalOrLocalMemberEntity(entityID)) {
            if (!Utils.isMemberEntity(entityID)) {
                final IFirstClassEntity firstClassEntity;
                if (Utils.isClass(classFile)) {
                    firstClassEntity = aCodeLevelModel.getFactory().createClass(entityID,
                            Utils.computeSimpleName(entityID));
                } else {
                    firstClassEntity = aCodeLevelModel.getFactory().createInterface(entityID,
                            Utils.computeSimpleName(entityID));
                }
                firstClassEntity.setVisibility(classFile.getAccess());

                final IPackage enclosingPackage = Utils.getPackage(this.mapOfPackageNamesPackages, entityID);
                enclosingPackage.addConstituent(firstClassEntity);
                this.mapOfIDsEntities.put(entityDisplayID, firstClassEntity);
            } else {
                final IFirstClassEntity memberEntity;
                if (Utils.isClass(classFile)) {
                    memberEntity = aCodeLevelModel.getFactory().createMemberClass(entityID,
                            Utils.computeSimpleName(entityID));
                } else {
                    memberEntity = aCodeLevelModel.getFactory().createMemberInterface(entityID,
                            Utils.computeSimpleName(entityID));
                }
                memberEntity.setVisibility(classFile.getAccess());

                final char[] enclosingEntityID = ArrayUtils.subarray(entityID, 0,
                        ArrayUtils.lastIndexOf(entityID, '$'));
                Utils.searchForEntity(aCodeLevelModel, enclosingEntityID)
                        .addConstituent((IConstituentOfEntity) memberEntity);
                this.mapOfIDsEntities.put(entityDisplayID, memberEntity);
            }
        }
    }

    // 2. At this point, all missing entities must be ghosts!
    iterator = listOfSourceConstituents.iterator();
    while (iterator.hasNext()) {
        final ClassFile classFile = (ClassFile) iterator.next();
        final String entityDisplayName = classFile.getName();
        final char[] entityName = entityDisplayName.toCharArray();

        if (!Utils.isAnonymousOrLocalEntity(entityName) && !Utils.isLocalOrLocalMemberEntity(entityName)) {

            // Yann 2008/11/06: Performance.
            // To increase the performance when building a model
            // from Java code, I maintain internally a map of
            // IDs and entities.
            final IFirstClassEntity firstClassEntity = Utils.getEntityOrCreateGhost(aCodeLevelModel, entityName,
                    this.mapOfIDsEntities);

            // Yann 2006/02/09: java.lang.Object
            // The java.lang.Object class does not have a superclass.
            // Hence, its superclass name would be "" but I don't
            // want to create a Ghost with no name...
            // Yann 2014/04/11: Object!
            // Wow, for the longest time, I was not adding Object 
            // as super-entity for interfaces: weird that it never 
            // caused problem before???
            if (!(firstClassEntity.equals(Factory.getInstance().createHierarchyRoot()))) {

                final String superClassDisplayName = classFile.getSuperName();
                final char[] superClassName = superClassDisplayName.toCharArray();
                final IFirstClassEntity superEntity = Utils.getEntityOrCreateGhost(aCodeLevelModel,
                        superClassName, this.mapOfIDsEntities);
                ((IFirstClassEntity) firstClassEntity).addInheritedEntity(superEntity);
            }

            final InterfaceList interfaceList = classFile.getInterfaces();
            for (int i = 0; i < interfaceList.length(); i++) {
                final String superInterfaceDisplayName = interfaceList.get(i);
                final char[] superInterfaceName = superInterfaceDisplayName.toCharArray();
                final IFirstClassEntity superEntity = Utils.getEntityOrCreateGhost(aCodeLevelModel,
                        superInterfaceName, this.mapOfIDsEntities);

                if (firstClassEntity instanceof IClass) {
                    ((IClass) firstClassEntity).addImplementedInterface((IInterfaceActor) superEntity);
                } else {
                    ((IInterfaceActor) firstClassEntity).addInheritedEntity(superEntity);
                }
            }
        }
    }
}

From source file:padl.creator.classfile.AbstractClassFileCreator.java

public List recognizeConstructor(final List aListOfSubmittedConstituents,
        final ICodeLevelModel aCodeLevelModel) {

    final List notRecognized = new ArrayList();
    final Iterator iterator = aListOfSubmittedConstituents.iterator();

    while (iterator.hasNext()) {
        final Object element = iterator.next();
        if (element instanceof ExtendedMethodInfo) {
            ExtendedMethodInfo currentExtendedMethod = (ExtendedMethodInfo) element;
            // Yann 2004/03/28: Constructors.
            // If the method is a constructor, I take care
            // of giving it the proper name and return type.
            final char[] name = currentExtendedMethod.getName();

            // Yann 2005/08/15: Inner classes!
            // I now manage inner and anonymous classes...
            //   if (Utils.isSpecialMethod(name)
            //      && !Misc.isAnonymousClass(
            //         currentExtendedMethod.getDeclaringClassName())) {
            if (Utils.isSpecialMethod(name)) {
                final IConstructor currentConstructor = aCodeLevelModel.getFactory().createConstructor(
                        Utils.computeSignature(currentExtendedMethod),
                        Util.computeSimpleName(currentExtendedMethod.getDeclaringClassName()));

                currentConstructor.setVisibility(currentExtendedMethod.getVisibility());

                final char[][] detectedParameters = currentExtendedMethod.getParameters();
                for (int i = 0; i < detectedParameters.length; i++) {
                    // Yann 2004/01/25: Arrays and primitive types!
                    // I must be careful with arrays (remove the
                    // brackets) and primitive types (do not attempt
                    // to create a Ghost from them).
                    // Yann 2009/05/02: No String anymore!
                    // Cool, eh?
                    char[] paramType = detectedParameters[i];
                    int cardinality = 1;
                    final int bracketIndex = ArrayUtils.indexOf(paramType, '[');
                    if (bracketIndex > -1) {
                        cardinality = (paramType.length - bracketIndex) / 2 + 1;
                        paramType = ArrayUtils.subarray(paramType, 0, bracketIndex);
                    }//ww w  . ja va2  s  . c o  m

                    final IParameter parameter = this.createParameter(aCodeLevelModel, paramType, cardinality);
                    currentConstructor.addConstituent(parameter);
                }

                // Yann 2005/12/29: Duplicate...
                // Actually, the RelationshipAnalyser might already have created
                // a method in an entity if this method was called from another
                // entity. Thus, there would be an attempt to add a "duplicate"
                // element, which is legitimate but taken care of by the new condition.
                final IFirstClassEntity firstClassEntity = Utils.getEntityOrCreateGhost(aCodeLevelModel,
                        currentExtendedMethod.getDeclaringClassName(), this.mapOfIDsEntities);
                if (!firstClassEntity.doesContainConstituentWithID(currentConstructor.getID())) {

                    firstClassEntity.addConstituent(currentConstructor);
                }
            }
        } else {
            notRecognized.add(element);
            // This is not a method, next...
        }
    }

    return notRecognized;
}

From source file:padl.creator.classfile.AbstractClassFileCreator.java

public List recognizeField(final List aListOfSubmittedConstituents, final ICodeLevelModel aCodeLevelModel) {

    final List notRecognized = new ArrayList();
    final Iterator enumarator = aListOfSubmittedConstituents.iterator();
    while (enumarator.hasNext()) {
        final Object element = enumarator.next();
        if (element instanceof ExtendedFieldInfo) {
            final ExtendedFieldInfo currentField = (ExtendedFieldInfo) element;
            char[] fieldType = currentField.getType();
            final int cardinality = Util.isArrayOrCollection(fieldType) ? Constants.CARDINALITY_MANY
                    : Constants.CARDINALITY_ONE;

            // Yann 2003/11/29: Array!
            // I must take care of removing brackets for array.
            int index;
            if ((index = ArrayUtils.indexOf(fieldType, '[')) == -1) {
                index = fieldType.length;
            }//from w w w. jav  a  2s. c  o  m
            fieldType = ArrayUtils.subarray(fieldType, 0, index);

            // Yann 2002/08/01: Ghost!
            // From now on, if an entity cannot be found in the
            // list of entities, I create a ghost for it...
            // Yann 2003/11/29: Primitive type.
            // I must take care not to add primitive type.
            final IField field;
            if (Util.isPrimtiveType(fieldType)) {
                field = aCodeLevelModel.getFactory().createField(currentField.getName(), currentField.getName(),
                        fieldType, cardinality);
            } else {
                final IFirstClassEntity targetEntity = Utils.getEntityOrCreateGhost(aCodeLevelModel, fieldType,
                        this.mapOfIDsEntities);
                // Yann 2006/02/08: Members!
                // The getConstituentFromID() method takes care of members and ghosts...
                //   if (targetEntity == null
                //      && !Utils.isAnonymousEntity(fieldType)) {
                //
                //      targetEntity =
                //         aCodeLevelModel.getFactory().createGhost(
                //            fieldType);
                //      aCodeLevelModel.addConstituent(targetEntity);
                //   }
                field = aCodeLevelModel.getFactory().createField(currentField.getName(), currentField.getName(),
                        targetEntity.getID(), cardinality);
            }
            field.setVisibility(currentField.getVisibility());

            final IFirstClassEntity firstClassEntity = Utils.getEntityOrCreateGhost(aCodeLevelModel,
                    currentField.getDeclaringClassName(), this.mapOfIDsEntities);
            firstClassEntity.addConstituent(field);
        } else {
            // this is not a field, next ...
            notRecognized.add(element);
        }
    }

    return notRecognized;
}

From source file:padl.creator.classfile.AbstractClassFileCreator.java

public List recognizeMethod(final List aListOfSubmittedConstituents, final ICodeLevelModel aCodeLevelModel) {

    final List notRecognized = new ArrayList();
    final Iterator iterator = aListOfSubmittedConstituents.iterator();

    while (iterator.hasNext()) {
        final Object element = iterator.next();
        if (element instanceof ExtendedMethodInfo) {
            ExtendedMethodInfo currentExtendedMethod = (ExtendedMethodInfo) element;
            // Yann 2004/03/28: Constructors.
            // If the method is a constructor, I take care
            // of giving it the proper name and return type.
            final char[] name = currentExtendedMethod.getName();
            if (!Utils.isSpecialMethod(name)) {
                final IMethod currentMethod = aCodeLevelModel.getFactory()
                        .createMethod(Utils.computeSignature(currentExtendedMethod), name);

                // Farouk 2004/02/19: Constituent ID.
                // The method makeSignature is the new way to give an
                // actor ID to a method, instead of using toString()
                // from MethodInfo.

                currentMethod.setReturnType(currentExtendedMethod.getReturnType());

                currentMethod.setVisibility(currentExtendedMethod.getVisibility());

                final char[][] detectedParameters = currentExtendedMethod.getParameters();
                for (int i = 0; i < detectedParameters.length; i++) {
                    // Yann 2004/01/25: Arrays and primitive types!
                    // I must be careful with arrays (remove the
                    // brackets) and primitive types (do not attempt
                    // to create a Ghost from them).
                    // Yann 2006/02/08: Members!
                    // The searchForEntity() method takes care of members and ghosts...
                    //   if (anEntity == null
                    //      && !padl.util.Util.isPrimtiveType(paramType)
                    //      && !Utils.isAnonymousEntity(paramType)) {
                    //
                    //      anEntity =
                    //         aCodeLevelModel.getFactory().createGhost(
                    //            paramType);
                    //      aCodeLevelModel.addConstituent(anEntity);
                    //   }
                    //
                    // Yann 2004/08/07: Parameters!
                    // Parameters now take care of their
                    // cardinlity by themselves...
                    // Yann 2007/08/31: Member...
                    // I should not replace the $ sign by
                    // a . because then I cannot know that
                    // the type is a member type anymore...
                    // this would not happen if IEntity
                    // was used instead of String!
                    //
                    // Yann 2009/05/02: No String anymore!
                    // Cool, eh?
                    char[] paramType = detectedParameters[i];
                    int cardinality = 1;
                    final int bracketIndex = ArrayUtils.indexOf(paramType, '[');
                    if (bracketIndex > -1) {
                        cardinality = (paramType.length - bracketIndex) / 2 + 1;
                        paramType = ArrayUtils.subarray(paramType, 0, bracketIndex);
                    }/* w w w .  ja  v a 2  s.  co  m*/

                    final IParameter parameter = this.createParameter(aCodeLevelModel, paramType, cardinality);
                    currentMethod.addConstituent(parameter);
                }

                // Yann 2005/12/29: Duplicate...
                // Actually, the RelationshipAnalyser might already have created
                // a method in an entity if this method was called from another
                // entity. Thus, there would be an attempt to add a "duplicate"
                // element, which is legitimate but taken care of by the new condition.
                final IFirstClassEntity firstClassEntity = Utils.getEntityOrCreateGhost(aCodeLevelModel,
                        currentExtendedMethod.getDeclaringClassName(), this.mapOfIDsEntities);
                if (!firstClassEntity.doesContainConstituentWithID(currentMethod.getID())) {

                    firstClassEntity.addConstituent(currentMethod);
                }
            }
        } else {
            notRecognized.add(element);
            // This is not a method, next...
        }
    }

    return notRecognized;
}

From source file:padl.creator.classfile.relationship.RelationshipAnalyzer.java

private IOperation searchForMethod(final char[] usedMethodName, final char[] returnType,
        final IFirstClassEntity entityDeclaringMethod) {

    IOperation calledMethod = null;//from  w w  w . ja v a2  s.c om

    // Yann 2006/07/29: Compatiblity!
    // There is a compatibility issue between classfiles v1.1-
    // and v1.2+, i.e., compiled with a 1.3- compiler and with
    // a 1.4+ compiler: the methodref pointed by the index in
    // a invokevirtual bytecode has changed. In the case of two
    // classes A and B such as A -<|- B, A defines foo(), B
    // defines bar() that calls this.foo(), the 1.1- bytcode 
    // looks like this (from BCEL):
    //
    //   [Code(max_stack = 1, max_locals = 1, code_length = 5)
    //   0:    aload_0
    //   1:    invokevirtual   padl.test.method.A.foo ()V (17)
    //   4:    return
    //   
    //   Attribute(s) = 
    //   LineNumber(0, 5), LineNumber(4, 6)
    //   LocalVariable(start_pc = 0, length = 5, index = 0:padl.test.method.B this)
    //   ]
    //
    // while the 1.2+ looks like that:
    //   [Code(max_stack = 1, max_locals = 1, code_length = 5)
    //   0:    aload_0
    //   1:    invokevirtual   padl.test.method.B.foo ()V (17)
    //   4:    return
    //   
    //   Attribute(s) = 
    //   LineNumber(0, 5), LineNumber(4, 6)
    //   LocalVariable(start_pc = 0, length = 5, index = 0:padl.test.method.B this)
    //   ]
    //
    // Note the change in the methodref: with 1.1- it is:
    //      padl.test.method.A.foo
    // while with 1.2+ it is:
    //      padl.test.method.B.foo
    // and that actually makes a hell of a difference,  because we 
    // were assuming that if the method did not exist we should add 
    // it, regardless if this was to a IGhost or a IClass.
    //
    // Thus, with 1.2+ bytecode, class B would define an additional
    // method foo() because of "padl.test.method.B.foo": indeed,
    // method foo() does not exist in B and thus it would be added!
    // I modified the test below to add the "instanceof", which
    // fixes nicely the problem.
    if (!entityDeclaringMethod.doesContainConstituentWithID(usedMethodName)
            && entityDeclaringMethod instanceof IGhost) {

        // Yann 2004/04/03: Constituent ID!
        // When creating a new method, we give it a unique actorID.
        // We must also give it a name without extra parentheses.
        // TODO: Refactor to remove the call to substring().
        // Yann 2004/04/09: Modifiers!
        // When creating a new method, we must also set its modifier:
        // Test case padl.test.example.Composite1 fails because the
        // print() method built should be set abstract.
        // Yann 2004/05/20: Constructors.
        // If the method is a constructor, its name is
        // its declaring class name.
        final char[] name = ArrayUtils.subarray(usedMethodName, 0, ArrayUtils.indexOf(usedMethodName, '('));
        if (Utils.isSpecialMethod(name)) {
            // Creates a new constructor that will be added to the ghost
            calledMethod = this.createConstructor(entityDeclaringMethod, usedMethodName);
        } else {
            // Creates a new method that will be added to the ghost
            calledMethod = this.createMethod(entityDeclaringMethod, usedMethodName, returnType, name);
        }
        entityDeclaringMethod.addConstituent(calledMethod);
    } else {
        calledMethod = (IOperation) entityDeclaringMethod.getConstituentFromID(usedMethodName);
    }

    // Yann 2006/08/03: Compatibility!
    // Now that I don't add a method in silly cases, I must
    // look for this method in the hierarchy of the current 
    // entity, because it can be defined higher up in the
    // hierarchy...
    // We are in Java, only one super-class is allowed.
    // Yann 2007/03/16: Interfaces...
    // But we could be dealing with an interface,
    // with no super-interfaces...
    // TODO: Anyhow, it is strange to deal with interfaces here...
    Iterator iterator = entityDeclaringMethod.getIteratorOnInheritedEntities();
    if (calledMethod == null && iterator.hasNext()) {
        /*
         * Alban : 2008/09/30
         * Iterate on all parents to find method
         */
        while (iterator.hasNext() && calledMethod == null) {
            calledMethod = this.searchForMethod(usedMethodName, returnType,
                    (IFirstClassEntity) iterator.next());
        }
    }

    if (entityDeclaringMethod instanceof IClass) {
        iterator = ((IClass) entityDeclaringMethod).getIteratorOnImplementedInterfaces();
        if (calledMethod == null && iterator.hasNext()) {
            /*
            * Aminata & Venera : 2011/08/19 
            * Iterate on all interfaces to find method
            */
            while (iterator.hasNext() && calledMethod == null) {
                calledMethod = this.searchForMethod(usedMethodName, returnType,
                        (IFirstClassEntity) iterator.next());
            }
        }
    }

    return calledMethod;
}