Example usage for org.eclipse.jdt.core.compiler CharOperation splitOn

List of usage examples for org.eclipse.jdt.core.compiler CharOperation splitOn

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.compiler CharOperation splitOn.

Prototype

public static final char[][] splitOn(char divider, char[] array, int start, int end) 

Source Link

Document

Return a new array which is the split of the given array using the given divider.

Usage

From source file:com.codenvy.ide.ext.java.server.internal.codeassist.SelectionEngine.java

License:Open Source License

/**
 * Asks the engine to compute the selection of the given type
 * from the given context//from   ww w  .j  av  a 2s. c  o  m
 *
 *  @param typeName char[]
 *      a type name which is to be resolved in the context of a compilation unit.
 *      NOTE: the type name is supposed to be correctly reduced (no whitespaces, no unicodes left)
 *
 *  @param context org.eclipse.jdt.core.IType
 *      the context in which code assist is invoked.
 */
public void selectType(char[] typeName, IType context) throws JavaModelException {
    try {
        this.acceptedAnswer = false;

        // only the type erasure are returned by IType.resolvedType(...)
        if (CharOperation.indexOf('<', typeName) != -1) {
            char[] typeSig = Signature.createCharArrayTypeSignature(typeName, false/*not resolved*/);
            typeSig = Signature.getTypeErasure(typeSig);
            typeName = Signature.toCharArray(typeSig);
        }

        CompilationUnitDeclaration parsedUnit = null;
        TypeDeclaration typeDeclaration = null;
        org.eclipse.jdt.core.ICompilationUnit cu = context.getCompilationUnit();
        if (cu != null) {
            IType[] topLevelTypes = cu.getTypes();
            int length = topLevelTypes.length;
            SourceTypeElementInfo[] topLevelInfos = new SourceTypeElementInfo[length];
            for (int i = 0; i < length; i++) {
                topLevelInfos[i] = (SourceTypeElementInfo) ((SourceType) topLevelTypes[i]).getElementInfo();
            }
            CompilationResult result = new CompilationResult(
                    (org.eclipse.jdt.internal.compiler.env.ICompilationUnit) cu, 1, 1,
                    this.compilerOptions.maxProblemsPerUnit);
            int flags = SourceTypeConverter.FIELD_AND_METHOD | SourceTypeConverter.MEMBER_TYPE;
            if (context.isAnonymous() || context.isLocal())
                flags |= SourceTypeConverter.LOCAL_TYPE;
            parsedUnit = SourceTypeConverter.buildCompilationUnit(topLevelInfos, flags,
                    this.parser.problemReporter(), result);
            if (parsedUnit != null && parsedUnit.types != null) {
                if (DEBUG) {
                    System.out.println("SELECTION - Diet AST :"); //$NON-NLS-1$
                    System.out.println(parsedUnit.toString());
                }
                // find the type declaration that corresponds to the original source type
                typeDeclaration = new ASTNodeFinder(parsedUnit).findType(context);
            }
        } else { // binary type
            ClassFile classFile = (ClassFile) context.getClassFile();
            ClassFileReader reader = (ClassFileReader) classFile.getBinaryTypeInfo(
                    (IFile) null/*classFile.resource()*/,
                    false/*don't fully initialize so as to keep constant pool (used below)*/);
            CompilationResult result = new CompilationResult(reader.getFileName(), 1, 1,
                    this.compilerOptions.maxProblemsPerUnit);
            parsedUnit = new CompilationUnitDeclaration(this.parser.problemReporter(), result, 0);
            HashSetOfCharArrayArray typeNames = new HashSetOfCharArrayArray();

            BinaryTypeConverter converter = new BinaryTypeConverter(this.parser.problemReporter(), result,
                    typeNames);
            typeDeclaration = converter.buildTypeDeclaration(context, parsedUnit);
            parsedUnit.imports = converter.buildImports(reader);
        }

        if (typeDeclaration != null) {

            // add fake field with the type we're looking for
            // note: since we didn't ask for fields above, there is no field defined yet
            FieldDeclaration field = new FieldDeclaration();
            int dot;
            if ((dot = CharOperation.lastIndexOf('.', typeName)) == -1) {
                this.selectedIdentifier = typeName;
                field.type = new SelectionOnSingleTypeReference(typeName, -1);
                // position not used
            } else {
                char[][] previousIdentifiers = CharOperation.splitOn('.', typeName, 0, dot);
                char[] selectionIdentifier = CharOperation.subarray(typeName, dot + 1, typeName.length);
                this.selectedIdentifier = selectionIdentifier;
                field.type = new SelectionOnQualifiedTypeReference(previousIdentifiers, selectionIdentifier,
                        new long[previousIdentifiers.length + 1]);
            }
            field.name = "<fakeField>".toCharArray(); //$NON-NLS-1$
            typeDeclaration.fields = new FieldDeclaration[] { field };

            // build bindings
            this.lookupEnvironment.buildTypeBindings(parsedUnit, null /*no access restriction*/);
            if ((this.unitScope = parsedUnit.scope) != null) {
                try {
                    // build fields
                    // note: this builds fields only in the parsed unit (the buildFieldsAndMethods flag is not passed along)
                    this.lookupEnvironment.completeTypeBindings(parsedUnit, true);

                    // resolve
                    parsedUnit.scope.faultInTypes();
                    parsedUnit.resolve();
                } catch (SelectionNodeFound e) {
                    if (e.binding != null) {
                        if (DEBUG) {
                            System.out.println("SELECTION - Selection binding :"); //$NON-NLS-1$
                            System.out.println(e.binding.toString());
                        }
                        // if null then we found a problem in the selection node
                        selectFrom(e.binding, parsedUnit, e.isDeclaration);
                    }
                }
            }
        }
        if (this.noProposal && this.problem != null) {
            this.requestor.acceptError(this.problem);
        }
    } catch (AbortCompilation e) { // ignore this exception for now since it typically means we cannot find java.lang.Object
    } finally {
        reset(true);
    }
}

From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.ConstructorDeclarationPattern.java

License:Open Source License

public void decodeIndexKey(char[] key) {
    int last = key.length - 1;
    int slash = CharOperation.indexOf(SEPARATOR, key, 0);
    this.declaringSimpleName = CharOperation.subarray(key, 0, slash);

    int start = slash + 1;
    slash = CharOperation.indexOf(SEPARATOR, key, start);
    last = slash - 1;//from  ww  w  .j a  v  a 2  s .  com

    boolean isDefaultConstructor = key[last] == '#';
    if (isDefaultConstructor) {
        this.parameterCount = -1;
    } else {
        this.parameterCount = 0;
        int power = 1;
        for (int i = last; i >= start; i--) {
            if (i == last) {
                this.parameterCount = key[i] - '0';
            } else {
                power *= 10;
                this.parameterCount += power * (key[i] - '0');
            }
        }
    }

    slash = slash + 3;
    last = slash - 1;

    int typeModifiersWithExtraFlags = key[last - 1] + (key[last] << 16);
    this.declaringTypeModifiers = decodeModifers(typeModifiersWithExtraFlags);
    this.extraFlags = decodeExtraFlags(typeModifiersWithExtraFlags);

    // initialize optional fields
    this.declaringPackageName = null;
    this.modifiers = 0;
    this.signature = null;
    this.parameterTypes = null;
    this.parameterNames = null;

    boolean isMemberType = (this.extraFlags & ExtraFlags.IsMemberType) != 0;

    if (!isMemberType) {
        start = slash + 1;
        if (this.parameterCount == -1) {
            slash = key.length;
            last = slash - 1;
        } else {
            slash = CharOperation.indexOf(SEPARATOR, key, start);
        }
        last = slash - 1;

        this.declaringPackageName = CharOperation.subarray(key, start, slash);

        start = slash + 1;
        if (this.parameterCount == 0) {
            slash = slash + 3;
            last = slash - 1;

            this.modifiers = key[last - 1] + (key[last] << 16);
        } else if (this.parameterCount > 0) {
            slash = CharOperation.indexOf(SEPARATOR, key, start);
            last = slash - 1;

            boolean hasParameterStoredAsSignature = (this.extraFlags
                    & ExtraFlags.ParameterTypesStoredAsSignature) != 0;
            if (hasParameterStoredAsSignature) {
                this.signature = CharOperation.subarray(key, start, slash);
                CharOperation.replace(this.signature, '\\', SEPARATOR);
            } else {
                this.parameterTypes = CharOperation.splitOn(PARAMETER_SEPARATOR, key, start, slash);
            }
            start = slash + 1;
            slash = CharOperation.indexOf(SEPARATOR, key, start);
            last = slash - 1;

            if (slash != start) {
                this.parameterNames = CharOperation.splitOn(PARAMETER_SEPARATOR, key, start, slash);
            }

            slash = slash + 3;
            last = slash - 1;

            this.modifiers = key[last - 1] + (key[last] << 16);
        } else {
            this.modifiers = ClassFileConstants.AccPublic;
        }
    }

    removeInternalFlags(); // remove internal flags
}

From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.QualifiedTypeDeclarationPattern.java

License:Open Source License

public void decodeIndexKey(char[] key) {
    int slash = CharOperation.indexOf(SEPARATOR, key, 0);
    this.simpleName = CharOperation.subarray(key, 0, slash);

    int start = ++slash;
    if (key[start] == SEPARATOR) {
        this.pkg = CharOperation.NO_CHAR;
    } else {//ww w .  java  2 s.  co m
        slash = CharOperation.indexOf(SEPARATOR, key, start);
        this.pkg = internedPackageNames.add(CharOperation.subarray(key, start, slash));
    }
    this.qualification = this.pkg;

    // Continue key read by the end to decode modifiers
    int last = key.length - 1;
    this.secondary = key[last] == 'S';
    if (this.secondary) {
        last -= 2;
    }
    this.modifiers = key[last - 1] + (key[last] << 16);
    decodeModifiers();

    // Retrieve enclosing type names
    start = slash + 1;
    last -= 2; // position of ending slash
    if (start == last) {
        this.enclosingTypeNames = CharOperation.NO_CHAR_CHAR;
    } else {
        int length = this.qualification.length;
        int size = last - start;
        System.arraycopy(this.qualification, 0, this.qualification = new char[length + 1 + size], 0, length);
        this.qualification[length] = '.';
        if (last == (start + 1) && key[start] == ZERO_CHAR) {
            this.enclosingTypeNames = ONE_ZERO_CHAR;
            this.qualification[length + 1] = ZERO_CHAR;
        } else {
            this.enclosingTypeNames = CharOperation.splitOn('.', key, start, last);
            System.arraycopy(key, start, this.qualification, length + 1, size);
        }
    }
}

From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.SuperTypeReferencePattern.java

License:Open Source License

public void decodeIndexKey(char[] key) {
    int slash = CharOperation.indexOf(SEPARATOR, key, 0);
    this.superSimpleName = CharOperation.subarray(key, 0, slash);

    // some values may not have been know when indexed so decode as null
    int start = slash + 1;
    slash = CharOperation.indexOf(SEPARATOR, key, start);
    this.superQualification = slash == start ? null : CharOperation.subarray(key, start, slash);

    slash = CharOperation.indexOf(SEPARATOR, key, start = slash + 1);
    this.simpleName = CharOperation.subarray(key, start, slash);

    start = ++slash;/*  w ww  . j  a va 2s  . c o m*/
    if (key[start] == SEPARATOR) {
        this.enclosingTypeName = null;
    } else {
        slash = CharOperation.indexOf(SEPARATOR, key, start);
        if (slash == (start + 1) && key[start] == ZERO_CHAR) {
            this.enclosingTypeName = ONE_ZERO;
        } else {
            char[] names = CharOperation.subarray(key, start, slash);
            this.enclosingTypeName = names;
        }
    }

    start = ++slash;
    if (key[start] == SEPARATOR) {
        this.typeParameterSignatures = null;
    } else {
        slash = CharOperation.indexOf(SEPARATOR, key, start);
        this.typeParameterSignatures = CharOperation.splitOn(',', key, start, slash);
    }

    start = ++slash;
    if (key[start] == SEPARATOR) {
        this.pkgName = null;
    } else {
        slash = CharOperation.indexOf(SEPARATOR, key, start);
        if (slash == (start + 1) && key[start] == ZERO_CHAR) {
            this.pkgName = this.superQualification;
        } else {
            char[] names = CharOperation.subarray(key, start, slash);
            this.pkgName = names;
        }
    }

    this.superClassOrInterface = key[slash + 1];
    this.classOrInterface = key[slash + 2];
    this.modifiers = key[slash + 3]; // implicit cast to int type
}

From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.TypeDeclarationPattern.java

License:Open Source License

public void decodeIndexKey(char[] key) {
    int slash = CharOperation.indexOf(SEPARATOR, key, 0);
    this.simpleName = CharOperation.subarray(key, 0, slash);

    int start = ++slash;
    if (key[start] == SEPARATOR) {
        this.pkg = CharOperation.NO_CHAR;
    } else {//from  www  . jav a2s  .c  om
        slash = CharOperation.indexOf(SEPARATOR, key, start);
        this.pkg = internedPackageNames.add(CharOperation.subarray(key, start, slash));
    }

    // Continue key read by the end to decode modifiers
    int last = key.length - 1;
    this.secondary = key[last] == 'S';
    if (this.secondary) {
        last -= 2;
    }
    this.modifiers = key[last - 1] + (key[last] << 16);
    decodeModifiers();

    // Retrieve enclosing type names
    start = slash + 1;
    last -= 2; // position of ending slash
    if (start == last) {
        this.enclosingTypeNames = CharOperation.NO_CHAR_CHAR;
    } else {
        if (last == (start + 1) && key[start] == ZERO_CHAR) {
            this.enclosingTypeNames = ONE_ZERO_CHAR;
        } else {
            this.enclosingTypeNames = CharOperation.splitOn('.', key, start, last);
        }
    }
}

From source file:com.ibm.research.tours.content.url.Signature.java

License:Open Source License

/**
 * Extracts the class and interface bounds from the given formal type
 * parameter signature. The class bound, if present, is listed before
 * the interface bounds. The signature is expected to be dot-based.
 *
 * @param formalTypeParameterSignature the formal type parameter signature
 * @return the (possibly empty) list of type signatures for the bounds
 * @exception IllegalArgumentException if the signature is syntactically
 *   incorrect/*from   w  ww. ja  v a  2 s  .  c om*/
 * @since 3.0
 */
public static char[][] getTypeParameterBounds(char[] formalTypeParameterSignature)
        throws IllegalArgumentException {
    int p1 = CharOperation.indexOf(C_COLON, formalTypeParameterSignature);
    if (p1 < 0) {
        // no ":" means can't be a formal type parameter signature
        throw new IllegalArgumentException();
    }
    if (p1 == formalTypeParameterSignature.length - 1) {
        // no class or interface bounds
        return CharOperation.NO_CHAR_CHAR;
    }
    int p2 = CharOperation.indexOf(C_COLON, formalTypeParameterSignature, p1 + 1);
    char[] classBound;
    if (p2 < 0) {
        // no interface bounds
        classBound = CharOperation.subarray(formalTypeParameterSignature, p1 + 1,
                formalTypeParameterSignature.length);
        return new char[][] { classBound };
    }
    if (p2 == p1 + 1) {
        // no class bound, but 1 or more interface bounds
        classBound = null;
    } else {
        classBound = CharOperation.subarray(formalTypeParameterSignature, p1 + 1, p2);
    }
    char[][] interfaceBounds = CharOperation.splitOn(C_COLON, formalTypeParameterSignature, p2 + 1,
            formalTypeParameterSignature.length);
    if (classBound == null) {
        return interfaceBounds;
    }
    int resultLength = interfaceBounds.length + 1;
    char[][] result = new char[resultLength][];
    result[0] = classBound;
    System.arraycopy(interfaceBounds, 0, result, 1, interfaceBounds.length);
    return result;
}

From source file:net.sf.j2s.core.builder.SourceFile.java

License:Open Source License

public char[][] getPackageName() {
    char[] typeName = this.initialTypeName.toCharArray();
    int lastIndex = CharOperation.lastIndexOf('/', typeName);
    return CharOperation.splitOn('/', typeName, 0, lastIndex);
}

From source file:org.eclipse.jdt.internal.core.hierarchy.HierarchyResolver.java

License:Open Source License

public ReferenceBinding setFocusType(char[][] compoundName) {
    if (compoundName == null || this.lookupEnvironment == null)
        return null;
    this.focusType = this.lookupEnvironment.getCachedType(compoundName);
    if (this.focusType == null) {
        this.focusType = this.lookupEnvironment.askForType(compoundName);
        if (this.focusType == null) {
            int length = compoundName.length;
            char[] typeName = compoundName[length - 1];
            int firstDollar = CharOperation.indexOf('$', typeName);
            if (firstDollar != -1) {
                compoundName[length - 1] = CharOperation.subarray(typeName, 0, firstDollar);
                this.focusType = this.lookupEnvironment.askForType(compoundName);
                if (this.focusType != null) {
                    char[][] memberTypeNames = CharOperation.splitOn('$', typeName, firstDollar + 1,
                            typeName.length);
                    for (int i = 0; i < memberTypeNames.length; i++) {
                        this.focusType = this.focusType.getMemberType(memberTypeNames[i]);
                    }/*from  ww  w .  ja  v a2 s  .co  m*/
                }
            }
        }
    }
    return this.focusType;
}