Search in sources :

Example 41 with ASTParser

use of org.eclipse.jdt.core.dom.ASTParser in project bndtools by bndtools.

the class NewTypeWizardPage method typeNameChanged.

/**
     * Hook method that gets called when the type name has changed. The method validates the type name and returns the
     * status of the validation.
     * <p>
     * Subclasses may extend this method to perform their own validation.
     * </p>
     *
     * @return the status of the validation
     */
protected IStatus typeNameChanged() {
    StatusInfo status = new StatusInfo();
    fCurrType = null;
    String typeNameWithParameters = getTypeName();
    // must not be empty
    if (typeNameWithParameters.length() == 0) {
        status.setError(NewWizardMessages.NewTypeWizardPage_error_EnterTypeName);
        return status;
    }
    String typeName = getTypeNameWithoutParameters();
    if (typeName.indexOf('.') != -1) {
        status.setError(NewWizardMessages.NewTypeWizardPage_error_QualifiedName);
        return status;
    }
    IJavaProject project = getJavaProject();
    IStatus val = validateJavaTypeName(typeName, project);
    if (val.getSeverity() == IStatus.ERROR) {
        status.setError(Messages.format(NewWizardMessages.NewTypeWizardPage_error_InvalidTypeName, val.getMessage()));
        return status;
    } else if (val.getSeverity() == IStatus.WARNING) {
        status.setWarning(Messages.format(NewWizardMessages.NewTypeWizardPage_warning_TypeNameDiscouraged, val.getMessage()));
    // continue checking
    }
    // must not exist
    if (!isEnclosingTypeSelected()) {
        IPackageFragment pack = getPackageFragment();
        if (pack != null) {
            ICompilationUnit cu = pack.getCompilationUnit(getCompilationUnitName(typeName));
            fCurrType = cu.getType(typeName);
            IResource resource = cu.getResource();
            if (resource.exists()) {
                status.setError(NewWizardMessages.NewTypeWizardPage_error_TypeNameExists);
                return status;
            }
            if (!ResourcesPlugin.getWorkspace().validateFiltered(resource).isOK()) {
                status.setError(NewWizardMessages.NewTypeWizardPage_error_TypeNameFiltered);
                return status;
            }
            URI location = resource.getLocationURI();
            if (location != null) {
                try {
                    IFileStore store = EFS.getStore(location);
                    if (store.fetchInfo().exists()) {
                        status.setError(NewWizardMessages.NewTypeWizardPage_error_TypeNameExistsDifferentCase);
                        return status;
                    }
                } catch (CoreException e) {
                    status.setError(Messages.format(NewWizardMessages.NewTypeWizardPage_error_uri_location_unkown, BasicElementLabels.getURLPart(Resources.getLocationString(resource))));
                }
            }
        }
    } else {
        IType type = getEnclosingType();
        if (type != null) {
            fCurrType = type.getType(typeName);
            if (fCurrType.exists()) {
                status.setError(NewWizardMessages.NewTypeWizardPage_error_TypeNameExists);
                return status;
            }
        }
    }
    if (!typeNameWithParameters.equals(typeName) && project != null) {
        if (!JavaModelUtil.is50OrHigher(project)) {
            status.setError(NewWizardMessages.NewTypeWizardPage_error_TypeParameters);
            return status;
        }
        //$NON-NLS-1$//$NON-NLS-2$
        String typeDeclaration = "class " + typeNameWithParameters + " {}";
        ASTParser parser = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
        parser.setSource(typeDeclaration.toCharArray());
        parser.setProject(project);
        CompilationUnit compilationUnit = (CompilationUnit) parser.createAST(null);
        IProblem[] problems = compilationUnit.getProblems();
        if (problems.length > 0) {
            status.setError(Messages.format(NewWizardMessages.NewTypeWizardPage_error_InvalidTypeName, problems[0].getMessage()));
            return status;
        }
    }
    return status;
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IStatus(org.eclipse.core.runtime.IStatus) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) URI(java.net.URI) IProblem(org.eclipse.jdt.core.compiler.IProblem) IType(org.eclipse.jdt.core.IType) IJavaProject(org.eclipse.jdt.core.IJavaProject) CoreException(org.eclipse.core.runtime.CoreException) StatusInfo(org.eclipse.jdt.internal.ui.dialogs.StatusInfo) IFileStore(org.eclipse.core.filesystem.IFileStore) ASTParser(org.eclipse.jdt.core.dom.ASTParser) IResource(org.eclipse.core.resources.IResource)

Example 42 with ASTParser

use of org.eclipse.jdt.core.dom.ASTParser in project bndtools by bndtools.

the class BaselineErrorHandler method findPackageInfoJavaVersionLocation.

ISourceRange findPackageInfoJavaVersionLocation(String packageName, ICompilationUnit compUnit) throws JavaModelException {
    ISourceRange range = null;
    IPackageDeclaration[] pkgDecls = compUnit.getPackageDeclarations();
    if (pkgDecls != null) {
        for (IPackageDeclaration pkgDecl : pkgDecls) {
            if (packageName.equals(pkgDecl.getElementName())) {
                IAnnotation[] annots = pkgDecl.getAnnotations();
                for (IAnnotation annot : annots) {
                    String name = annot.getElementName();
                    if (ANNOTATION_VERSION_NO_PKG.equals(name) || ANNOTATION_VERSION_OSGI.equals(name) || ANNOTATION_VERSION_BND.equals(name)) {
                        ASTParser parser = ASTParser.newParser(AST.JLS8);
                        parser.setKind(ASTParser.K_COMPILATION_UNIT);
                        parser.setSource(compUnit);
                        parser.setResolveBindings(true);
                        CompilationUnit ast = (CompilationUnit) parser.createAST(null);
                        if (ast != null) {
                            MemberValuePairLocationRetriever mvpRetriever = new MemberValuePairLocationRetriever(annot, new Predicate<String>() {

                                @Override
                                public boolean test(String t) {
                                    return ANNOTATION_VERSION_BND.equals(t) || ANNOTATION_VERSION_OSGI.equals(t);
                                }
                            }, "value");
                            ast.accept(mvpRetriever);
                            range = mvpRetriever.getMemberValuePairSourceRange();
                        }
                    }
                }
            }
        }
    }
    return range;
}
Also used : IAnnotation(org.eclipse.jdt.core.IAnnotation) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ASTParser(org.eclipse.jdt.core.dom.ASTParser) MemberValuePairLocationRetriever(org.bndtools.builder.utils.MemberValuePairLocationRetriever) IPackageDeclaration(org.eclipse.jdt.core.IPackageDeclaration) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Aggregations

ASTParser (org.eclipse.jdt.core.dom.ASTParser)42 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)34 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)24 ASTNode (org.eclipse.jdt.core.dom.ASTNode)11 RefactoringASTParser (org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser)9 IBinding (org.eclipse.jdt.core.dom.IBinding)7 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 IType (org.eclipse.jdt.core.IType)5 IProblem (org.eclipse.jdt.core.compiler.IProblem)5 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)5 ASTRequestor (org.eclipse.jdt.core.dom.ASTRequestor)4 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)4 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)3 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)3 ISourceRange (org.eclipse.jdt.core.ISourceRange)3 ArrayType (org.eclipse.jdt.core.dom.ArrayType)3 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)3 Type (org.eclipse.jdt.core.dom.Type)3 Map (java.util.Map)2