Example usage for org.eclipse.jdt.internal.core.util Util getNameWithoutJavaLikeExtension

List of usage examples for org.eclipse.jdt.internal.core.util Util getNameWithoutJavaLikeExtension

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.core.util Util getNameWithoutJavaLikeExtension.

Prototype

public static String getNameWithoutJavaLikeExtension(String fileName) 

Source Link

Document

Returns the substring of the given file name, ending at the start of a Java like extension.

Usage

From source file:com.codenvy.ide.ext.java.server.internal.core.NameLookup.java

License:Open Source License

public NameLookup(IPackageFragmentRoot[] packageFragmentRoots, HashtableOfArrayToObject packageFragments,
        ICompilationUnit[] workingCopies, Map rootToResolvedEntries, JavaModelManager manager) {
    this.manager = manager;
    long start = -1;
    if (VERBOSE) {
        Util.verbose(" BUILDING NameLoopkup"); //$NON-NLS-1$
        Util.verbose(" -> pkg roots size: " + (packageFragmentRoots == null ? 0 : packageFragmentRoots.length)); //$NON-NLS-1$
        Util.verbose(" -> pkgs size: " + (packageFragments == null ? 0 : packageFragments.size())); //$NON-NLS-1$
        Util.verbose(" -> working copy size: " + (workingCopies == null ? 0 : workingCopies.length)); //$NON-NLS-1$
        start = System.currentTimeMillis();
    }//  w  w  w.ja  va2  s  .  co m
    this.packageFragmentRoots = packageFragmentRoots;
    if (workingCopies == null) {
        this.packageFragments = packageFragments;
    } else {
        // clone tables as we're adding packages from working copies
        try {
            this.packageFragments = (HashtableOfArrayToObject) packageFragments.clone();
        } catch (CloneNotSupportedException e1) {
            // ignore (implementation of HashtableOfArrayToObject supports cloning)
        }
        this.typesInWorkingCopies = new HashMap();
        HashtableOfObjectToInt rootPositions = new HashtableOfObjectToInt();
        for (int i = 0, length = packageFragmentRoots.length; i < length; i++) {
            rootPositions.put(packageFragmentRoots[i], i);
        }
        for (int i = 0, length = workingCopies.length; i < length; i++) {
            ICompilationUnit workingCopy = workingCopies[i];
            PackageFragment pkg = (PackageFragment) workingCopy.getParent();
            IPackageFragmentRoot root = (IPackageFragmentRoot) pkg.getParent();
            int rootPosition = rootPositions.get(root);
            if (rootPosition == -1)
                continue; // working copy is not visible from this project (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=169970)
            HashMap typeMap = (HashMap) this.typesInWorkingCopies.get(pkg);
            if (typeMap == null) {
                typeMap = new HashMap();
                this.typesInWorkingCopies.put(pkg, typeMap);
            }
            try {
                IType[] types = workingCopy.getTypes();
                int typeLength = types.length;
                if (typeLength == 0) {
                    String typeName = Util.getNameWithoutJavaLikeExtension(workingCopy.getElementName());
                    typeMap.put(typeName, NO_TYPES);
                } else {
                    for (int j = 0; j < typeLength; j++) {
                        IType type = types[j];
                        String typeName = type.getElementName();
                        Object existing = typeMap.get(typeName);
                        if (existing == null) {
                            typeMap.put(typeName, type);
                        } else if (existing instanceof IType) {
                            typeMap.put(typeName, new IType[] { (IType) existing, type });
                        } else {
                            IType[] existingTypes = (IType[]) existing;
                            int existingTypeLength = existingTypes.length;
                            System.arraycopy(existingTypes, 0,
                                    existingTypes = new IType[existingTypeLength + 1], 0, existingTypeLength);
                            existingTypes[existingTypeLength] = type;
                            typeMap.put(typeName, existingTypes);
                        }
                    }
                }
            } catch (JavaModelException e) {
                // working copy doesn't exist -> ignore
            }

            // add root of package fragment to cache
            String[] pkgName = pkg.names;
            Object existing = this.packageFragments.get(pkgName);
            if (existing == null || existing == JavaProjectElementInfo.NO_ROOTS) {
                this.packageFragments.put(pkgName, root);
                // ensure super packages (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=119161)
                // are also in the map
                JavaProjectElementInfo.addSuperPackageNames(pkgName, this.packageFragments);
            } else {
                if (existing instanceof PackageFragmentRoot) {
                    int exisitingPosition = rootPositions.get(existing);
                    if (rootPosition != exisitingPosition) { // if not equal
                        this.packageFragments.put(pkgName,
                                exisitingPosition < rootPosition
                                        ? new IPackageFragmentRoot[] { (PackageFragmentRoot) existing, root }
                                        : new IPackageFragmentRoot[] { root, (PackageFragmentRoot) existing });
                    }
                } else {
                    // insert root in the existing list
                    IPackageFragmentRoot[] roots = (IPackageFragmentRoot[]) existing;
                    int rootLength = roots.length;
                    int insertionIndex = 0;
                    for (int j = 0; j < rootLength; j++) {
                        int existingPosition = rootPositions.get(roots[j]);
                        if (rootPosition > existingPosition) {
                            // root is after this index
                            insertionIndex = j;
                        } else if (rootPosition == existingPosition) {
                            // root already in the existing list
                            insertionIndex = -1;
                            break;
                        } else if (rootPosition < existingPosition) {
                            // root is before this index (thus it is at the insertion index)
                            break;
                        }
                    }
                    if (insertionIndex != -1) {
                        IPackageFragmentRoot[] newRoots = new IPackageFragmentRoot[rootLength + 1];
                        System.arraycopy(roots, 0, newRoots, 0, insertionIndex);
                        newRoots[insertionIndex] = root;
                        System.arraycopy(roots, insertionIndex, newRoots, insertionIndex + 1,
                                rootLength - insertionIndex);
                        this.packageFragments.put(pkgName, newRoots);
                    }
                }
            }
        }
    }

    this.rootToResolvedEntries = rootToResolvedEntries;
    if (VERBOSE) {
        Util.verbose(" -> spent: " + (System.currentTimeMillis() - start) + "ms"); //$NON-NLS-1$ //$NON-NLS-2$
    }
}

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

License:Open Source License

public JavaSearchNameEnvironment(JavaProject javaProject, org.eclipse.jdt.core.ICompilationUnit[] copies) {
    this.javaProject = javaProject;
    computeClasspathLocations(javaProject);
    try {// www  . j a  va2 s.c  o  m
        int length = copies == null ? 0 : copies.length;
        this.workingCopies = new HashMap(length);
        if (copies != null) {
            for (int i = 0; i < length; i++) {
                org.eclipse.jdt.core.ICompilationUnit workingCopy = copies[i];
                IPackageDeclaration[] pkgs = workingCopy.getPackageDeclarations();
                String pkg = pkgs.length > 0 ? pkgs[0].getElementName() : ""; //$NON-NLS-1$
                String cuName = workingCopy.getElementName();
                String mainTypeName = Util.getNameWithoutJavaLikeExtension(cuName);
                String qualifiedMainTypeName = pkg.length() == 0 ? mainTypeName
                        : pkg.replace('.', '/') + '/' + mainTypeName;
                this.workingCopies.put(qualifiedMainTypeName, workingCopy);
            }
        }
    } catch (JavaModelException e) {
        // working copy doesn't exist: cannot happen
    }
}

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

License:Open Source License

private char[] getQualifiedName() {
    if (this.openable instanceof CompilationUnit) {
        // get file name
        String fileName = this.openable.getElementName(); // working copy on a .class file may not have a resource, so use the element name
        // get main type name
        char[] mainTypeName = Util.getNameWithoutJavaLikeExtension(fileName).toCharArray();
        CompilationUnit cu = (CompilationUnit) this.openable;
        return cu.getType(new String(mainTypeName)).getFullyQualifiedName().toCharArray();
    } else if (this.openable instanceof ClassFile) {
        String fileName = getSourceFileName();
        if (fileName == NO_SOURCE_FILE_NAME)
            return ((ClassFile) this.openable).getType().getFullyQualifiedName('.').toCharArray();

        // Class file may have a source file name with ".java" extension (see bug 73784)
        int index = Util.indexOfJavaLikeExtension(fileName);
        String simpleName = index == -1 ? fileName : fileName.substring(0, index);
        PackageFragment pkg = (PackageFragment) this.openable.getParent();
        return Util.concatWith(pkg.names, simpleName, '.').toCharArray();
    }//  ww w  .  j a  va  2  s .  c  om
    return null;
}

From source file:org.eclipse.jdt.internal.core.CompilationUnit.java

License:Open Source License

/**
 * @see ICompilationUnit#findPrimaryType()
 *//*from w w w .  ja  va  2 s.  co m*/
public IType findPrimaryType() {
    String typeName = Util.getNameWithoutJavaLikeExtension(getElementName());
    IType primaryType = getType(typeName);
    if (primaryType.exists()) {
        return primaryType;
    }
    return null;
}

From source file:org.eclipse.jdt.internal.core.CompilationUnit.java

License:Open Source License

/**
 * @see org.eclipse.jdt.internal.compiler.env.ICompilationUnit#getMainTypeName()
 *///from   ww w .j  a va  2 s.  c  o  m
public char[] getMainTypeName() {
    return Util.getNameWithoutJavaLikeExtension(getElementName()).toCharArray();
}

From source file:org.eclipse.jdt.internal.core.CopyResourceElementsOperation.java

License:Open Source License

/**
 * Copies/moves a compilation unit with the name <code>newCUName</code>
 * to the destination package.<br>
 * The package statement in the compilation unit is updated if necessary.
 * The main type of the compilation unit is renamed if necessary.
 *
 * @exception JavaModelException if the operation is unable to
 * complete/*from   w w  w. j a v  a  2s  .  co  m*/
 */
private void processCompilationUnitResource(ICompilationUnit source, PackageFragment dest)
        throws JavaModelException {
    String newCUName = getNewNameFor(source);
    String destName = (newCUName != null) ? newCUName : source.getElementName();
    TextEdit edit = updateContent(source, dest, newCUName); // null if unchanged

    // TODO (frederic) remove when bug 67606 will be fixed (bug 67823)
    // store encoding (fix bug 66898)
    IFile sourceResource = (IFile) source.getResource();
    String sourceEncoding = null;
    try {
        sourceEncoding = sourceResource.getCharset(false);
    } catch (CoreException ce) {
        // no problem, use default encoding
    }
    // end todo
    // copy resource
    IContainer destFolder = (IContainer) dest.getResource(); // can be an IFolder or an IProject
    IFile destFile = destFolder.getFile(new Path(destName));
    // GROOVY start
    /* old {
    org.eclipse.jdt.internal.core.CompilationUnit destCU = new org.eclipse.jdt.internal.core.CompilationUnit(dest, destName, DefaultWorkingCopyOwner.PRIMARY);
    } new */
    org.eclipse.jdt.internal.core.CompilationUnit destCU = LanguageSupportFactory.newCompilationUnit(dest,
            destName, DefaultWorkingCopyOwner.PRIMARY);
    // GROOVY end

    if (!destFile.equals(sourceResource)) {
        try {
            if (!destCU.isWorkingCopy()) {
                if (destFile.exists()) {
                    if (this.force) {
                        // we can remove it
                        deleteResource(destFile, IResource.KEEP_HISTORY);
                        destCU.close(); // ensure the in-memory buffer for the dest CU is closed
                    } else {
                        // abort
                        throw new JavaModelException(
                                new JavaModelStatus(IJavaModelStatusConstants.NAME_COLLISION, Messages.bind(
                                        Messages.status_nameCollision, destFile.getFullPath().toString())));
                    }
                }
                int flags = this.force ? IResource.FORCE : IResource.NONE;
                if (isMove()) {
                    flags |= IResource.KEEP_HISTORY;
                    sourceResource.move(destFile.getFullPath(), flags, getSubProgressMonitor(1));
                } else {
                    if (edit != null)
                        flags |= IResource.KEEP_HISTORY;
                    sourceResource.copy(destFile.getFullPath(), flags, getSubProgressMonitor(1));
                }
                setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE);
            } else {
                destCU.getBuffer().setContents(source.getBuffer().getContents());
            }
        } catch (JavaModelException e) {
            throw e;
        } catch (CoreException e) {
            throw new JavaModelException(e);
        }

        // update new resource content
        if (edit != null) {
            boolean wasReadOnly = destFile.isReadOnly();
            try {
                saveContent(dest, destName, edit, sourceEncoding, destFile);
            } catch (CoreException e) {
                if (e instanceof JavaModelException)
                    throw (JavaModelException) e;
                throw new JavaModelException(e);
            } finally {
                Util.setReadOnly(destFile, wasReadOnly);
            }
        }

        // register the correct change deltas
        prepareDeltas(source, destCU, isMove());
        if (newCUName != null) {
            //the main type has been renamed
            String oldName = Util.getNameWithoutJavaLikeExtension(source.getElementName());
            String newName = Util.getNameWithoutJavaLikeExtension(newCUName);
            prepareDeltas(source.getType(oldName), destCU.getType(newName), isMove());
        }
    } else {
        if (!this.force) {
            throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.NAME_COLLISION,
                    Messages.bind(Messages.status_nameCollision, destFile.getFullPath().toString())));
        }
        // update new resource content
        // in case we do a saveas on the same resource we have to simply update the contents
        // see http://dev.eclipse.org/bugs/show_bug.cgi?id=9351
        if (edit != null) {
            saveContent(dest, destName, edit, sourceEncoding, destFile);
        }
    }
}

From source file:org.eclipse.jdt.internal.core.CopyResourceElementsOperation.java

License:Open Source License

/**
* Renames the main type in <code>cu</code>.
*///from   w  ww  .  j  a  v  a 2s .c  o  m
private void updateTypeName(ICompilationUnit cu, CompilationUnit astCU, String oldName, String newName,
        ASTRewrite rewriter) throws JavaModelException {
    if (newName != null) {
        String oldTypeName = Util.getNameWithoutJavaLikeExtension(oldName);
        String newTypeName = Util.getNameWithoutJavaLikeExtension(newName);
        AST ast = astCU.getAST();
        // update main type name
        IType[] types = cu.getTypes();
        for (int i = 0, max = types.length; i < max; i++) {
            IType currentType = types[i];
            if (currentType.getElementName().equals(oldTypeName)) {
                AbstractTypeDeclaration typeNode = (AbstractTypeDeclaration) ((JavaElement) currentType)
                        .findNode(astCU);
                if (typeNode != null) {
                    // rename type
                    rewriter.replace(typeNode.getName(), ast.newSimpleName(newTypeName), null);
                    // rename constructors
                    Iterator bodyDeclarations = typeNode.bodyDeclarations().iterator();
                    while (bodyDeclarations.hasNext()) {
                        Object bodyDeclaration = bodyDeclarations.next();
                        if (bodyDeclaration instanceof MethodDeclaration) {
                            MethodDeclaration methodDeclaration = (MethodDeclaration) bodyDeclaration;
                            if (methodDeclaration.isConstructor()) {
                                SimpleName methodName = methodDeclaration.getName();
                                if (methodName.getIdentifier().equals(oldTypeName)) {
                                    rewriter.replace(methodName, ast.newSimpleName(newTypeName), null);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

From source file:org.eiichiro.gig.eclipse.ui.wizards.NewComponentWizardPage.java

License:Open Source License

@Override
protected String constructCUContent(ICompilationUnit cu, String typeContent, String lineDelimiter)
        throws CoreException {
    String name = cu.getType(Util.getNameWithoutJavaLikeExtension(cu.getElementName())).getFullyQualifiedName();
    return super.constructCUContent(cu, "@Name(\"" + name + "\")" + lineDelimiter + typeContent, lineDelimiter);
}

From source file:org.eiichiro.gig.eclipse.ui.wizards.NewWebEndpointWizardPage.java

License:Open Source License

@Override
protected String constructCUContent(ICompilationUnit cu, String typeContent, String lineDelimiter)
        throws CoreException {
    String name = cu.getType(Util.getNameWithoutJavaLikeExtension(cu.getElementName())).getFullyQualifiedName();
    return super.constructCUContent(cu,
            "@Endpoint" + lineDelimiter + "@Name(\"" + name + "\")" + lineDelimiter + typeContent,
            lineDelimiter);//from  ww w.  ja va2s  . c  om
}

From source file:org.grails.ide.eclipse.editor.groovy.elements.GrailsProject.java

License:Open Source License

protected String getPrimaryTypeName(ICompilationUnit unit) {
    String elementName = unit.getElementName();
    // we already know that this unit ends with .groovy, so no need to check
    return Util.getNameWithoutJavaLikeExtension(elementName);
}