Package org.eclipse.jdt.core

Examples of org.eclipse.jdt.core.ICompilationUnit


     * @return the created methods.
     * @throws CoreException
     *             thrown when the creation fails.
     */
    protected IMethod[] createInheritedMethods(IType type, boolean doConstructors, boolean doUnimplementedMethods, ImportsManager imports, IProgressMonitor monitor) throws CoreException {
        final ICompilationUnit cu = type.getCompilationUnit();
        JavaModelUtil.reconcile(cu);
        IMethod[] typeMethods = type.getMethods();
        Set<String> handleIds = new HashSet<String>(typeMethods.length);
        for (int index = 0; index < typeMethods.length; index++)
            handleIds.add(typeMethods[index].getHandleIdentifier());
View Full Code Here


    CompilationUnit createAST(IJavaProject javaProject, String className) throws JavaModelException {
        IType type = javaProject.findType(className);
        if (type == null)
            return null;

        final ICompilationUnit cunit = type.getCompilationUnit();
        if (cunit == null)
            return null; // not a source type

        ASTParser parser = ASTParser.newParser(AST.JLS4);
        parser.setKind(ASTParser.K_COMPILATION_UNIT);
View Full Code Here

      return;
    }

    try
    {
      ICompilationUnit cunit = context.getWorkingCopy();

      if( cunit.isStructureKnown() )
      {
        MockASTVisitor visitor = new MockASTVisitor(cunit);
        context.getAST3().accept(visitor);
        CategorizedProblem[] probs = visitor.getProblems();
View Full Code Here

      {
        IFile file = f.getFile();

        if( file.isAccessible() && !file.isDerived(IResource.CHECK_ANCESTORS) )
        {
          ICompilationUnit cunit = JavaCore.createCompilationUnitFrom(file);

          try
          {
            mon.setTaskName(taskName + " - " + cunit.getElementName());
            analyseFile(file, cunit, mon);
          }
          catch (Exception e)
          {
            Activator.log(e);
View Full Code Here

  @Override
  public final List<ICompletionProposal> computeCompletionProposals(final ContentAssistInvocationContext context,
      final IProgressMonitor mon)
  {
    ITypeBinding mockType = null, paramType = null;
    ICompilationUnit cunit = getCompilationUnit(context);
    CompilationUnit astRoot = null;

    if (cunit != null)
    {
      astRoot = ASTUtil.getAstOrParse(cunit, mon);
View Full Code Here

    return Collections.emptyList();
  }

  public final ICompilationUnit getCompilationUnit(final ContentAssistInvocationContext context)
  {
    ICompilationUnit cunit = null;
    if (context instanceof JavaContentAssistInvocationContext)
    {
      JavaContentAssistInvocationContext jcontext = (JavaContentAssistInvocationContext) context;

      if (jcontext.getCoreContext() != null)
View Full Code Here

            if (typeInCU != null) {
                if (typeInCU.getCompilationUnit() != null) {
                    enclosingType = typeInCU;
                }
            } else {
                ICompilationUnit cu = (ICompilationUnit) elem.getAncestor(IJavaElement.COMPILATION_UNIT);
                if (cu != null) {
                    enclosingType = cu.findPrimaryType();
                }
            }

            try {
                IType type = null;
View Full Code Here

        // 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;
                }
View Full Code Here

        } else {
            monitorInternal.worked(1);
        }

        boolean needsSave;
        ICompilationUnit connectedCU = null;

        try {
            String typeName = getTypeNameWithoutParameters();

            boolean isInnerClass = isEnclosingTypeSelected();

            IType createdType;
            ImportsManager imports;
            int indent = 0;

            Set<String> existingImports;

            String lineDelimiter = null;
            if (!isInnerClass) {
                lineDelimiter = StubUtility.getLineDelimiterUsed(pack.getJavaProject());

                String cuName = getCompilationUnitName(typeName);
                ICompilationUnit parentCU = pack.createCompilationUnit(cuName, "", false, new SubProgressMonitor(monitorInternal, 2)); //$NON-NLS-1$
                // create a working copy with a new owner

                needsSave = true;
                parentCU.becomeWorkingCopy(new SubProgressMonitor(monitorInternal, 1)); // cu is now a (primary) working copy
                connectedCU = parentCU;

                IBuffer buffer = parentCU.getBuffer();

                String simpleTypeStub = constructSimpleTypeStub();
                String cuContent = constructCUContent(parentCU, simpleTypeStub, lineDelimiter);
                buffer.setContents(cuContent);

                CompilationUnit astRoot = createASTForImports(parentCU);
                existingImports = getExistingImports(astRoot);

                imports = new ImportsManager(astRoot);
                // add an import that will be removed again. Having this import solves 14661
                imports.addImport(JavaModelUtil.concatenateName(pack.getElementName(), typeName));

                String typeContent = constructTypeStub(parentCU, imports, lineDelimiter);

                int index = cuContent.lastIndexOf(simpleTypeStub);
                if (index == -1) {
                    AbstractTypeDeclaration typeNode = (AbstractTypeDeclaration) astRoot.types().get(0);
                    int start = ((ASTNode) typeNode.modifiers().get(0)).getStartPosition();
                    int end = typeNode.getStartPosition() + typeNode.getLength();
                    buffer.replace(start, end - start, typeContent);
                } else {
                    buffer.replace(index, simpleTypeStub.length(), typeContent);
                }

                createdType = parentCU.getType(typeName);
            } else {
                IType enclosingType = getEnclosingType();

                ICompilationUnit parentCU = enclosingType.getCompilationUnit();

                needsSave = !parentCU.isWorkingCopy();
                parentCU.becomeWorkingCopy(new SubProgressMonitor(monitorInternal, 1)); // cu is now for sure (primary) a working copy
                connectedCU = parentCU;

                CompilationUnit astRoot = createASTForImports(parentCU);
                imports = new ImportsManager(astRoot);
                existingImports = getExistingImports(astRoot);

                // add imports that will be removed again. Having the imports solves 14661
                IType[] topLevelTypes = parentCU.getTypes();
                for (int i = 0; i < topLevelTypes.length; i++) {
                    imports.addImport(topLevelTypes[i].getFullyQualifiedName('.'));
                }

                lineDelimiter = StubUtility.getLineDelimiterUsed(enclosingType);
                StringBuffer content = new StringBuffer();

                String comment = getTypeComment(parentCU, lineDelimiter);
                if (comment != null) {
                    content.append(comment);
                    content.append(lineDelimiter);
                }

                content.append(constructTypeStub(parentCU, imports, lineDelimiter));
                IJavaElement sibling = null;
                if (enclosingType.isEnum()) {
                    IField[] fields = enclosingType.getFields();
                    if (fields.length > 0) {
                        for (int i = 0, max = fields.length; i < max; i++) {
                            if (!fields[i].isEnumConstant()) {
                                sibling = fields[i];
                                break;
                            }
                        }
                    }
                } else {
                    IJavaElement[] elems = enclosingType.getChildren();
                    sibling = elems.length > 0 ? elems[0] : null;
                }

                createdType = enclosingType.createType(content.toString(), sibling, false, new SubProgressMonitor(monitorInternal, 2));

                indent = StubUtility.getIndentUsed(enclosingType) + 1;
            }
            if (monitorInternal.isCanceled()) {
                throw new InterruptedException();
            }

            // add imports for superclass/interfaces, so types can be resolved correctly

            ICompilationUnit cu = createdType.getCompilationUnit();

            imports.create(false, new SubProgressMonitor(monitorInternal, 1));

            JavaModelUtil.reconcile(cu);

            if (monitorInternal.isCanceled()) {
                throw new InterruptedException();
            }

            // set up again
            CompilationUnit astRoot = createASTForImports(imports.getCompilationUnit());
            imports = new ImportsManager(astRoot);

            createTypeMembers(createdType, imports, new SubProgressMonitor(monitorInternal, 1));

            // add imports
            imports.create(false, new SubProgressMonitor(monitorInternal, 1));

            removeUnusedImports(cu, existingImports, false);

            JavaModelUtil.reconcile(cu);

            ISourceRange range = createdType.getSourceRange();

            IBuffer buf = cu.getBuffer();
            String originalContent = buf.getText(range.getOffset(), range.getLength());

            String formattedContent = CodeFormatterUtil.format(CodeFormatter.K_CLASS_BODY_DECLARATIONS, originalContent, indent, lineDelimiter, pack.getJavaProject());
            formattedContent = Strings.trimLeadingTabsAndSpaces(formattedContent);
            buf.replace(range.getOffset(), range.getLength(), formattedContent);
            if (!isInnerClass) {
                String fileComment = getFileComment(cu);
                if (fileComment != null && fileComment.length() > 0) {
                    buf.replace(0, 0, fileComment + lineDelimiter);
                }
            }
            fCreatedType = createdType;

            if (needsSave) {
                cu.commitWorkingCopy(true, new SubProgressMonitor(monitorInternal, 1));
            } else {
                monitorInternal.worked(1);
            }

        } finally {
View Full Code Here

     * @return the created methods.
     * @throws CoreException
     *             thrown when the creation fails.
     */
    protected IMethod[] createInheritedMethods(IType type, boolean doConstructors, boolean doUnimplementedMethods, ImportsManager imports, IProgressMonitor monitor) throws CoreException {
        final ICompilationUnit cu = type.getCompilationUnit();
        JavaModelUtil.reconcile(cu);
        IMethod[] typeMethods = type.getMethods();
        Set<String> handleIds = new HashSet<String>(typeMethods.length);
        for (int index = 0; index < typeMethods.length; index++)
            handleIds.add(typeMethods[index].getHandleIdentifier());
View Full Code Here

TOP

Related Classes of org.eclipse.jdt.core.ICompilationUnit

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.