Examples of CompilationUnit


Examples of org.eclipse.jdt.core.dom.CompilationUnit

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

Examples of org.eclipse.jdt.core.dom.CompilationUnit

    private void removeUnusedImports(ICompilationUnit cu, Set<String> existingImports, boolean needsSave) throws CoreException {
        ASTParser parser = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
        parser.setSource(cu);
        parser.setResolveBindings(true);

        CompilationUnit root = (CompilationUnit) parser.createAST(null);
        if (root.getProblems().length == 0) {
            return;
        }

        @SuppressWarnings("unchecked")
        List<ImportDeclaration> importsDecls = root.imports();
        if (importsDecls.isEmpty()) {
            return;
        }
        ImportsManager imports = new ImportsManager(root);

        int importsEnd = ASTNodes.getExclusiveEnd(importsDecls.get(importsDecls.size() - 1));
        IProblem[] problems = root.getProblems();
        for (int i = 0; i < problems.length; i++) {
            IProblem curr = problems[i];
            if (curr.getSourceEnd() < importsEnd) {
                int id = curr.getID();
                if (id == IProblem.UnusedImport || id == IProblem.NotVisibleType) { // not visible problems hide unused -> remove both
View Full Code Here

Examples of org.eclipse.jdt.core.dom.CompilationUnit

        String content = CodeGeneration.getCompilationUnitContent(cu, fileComment, typeComment, typeContent, lineDelimiter);
        if (content != null) {
            ASTParser parser = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
            parser.setProject(cu.getJavaProject());
            parser.setSource(content.toCharArray());
            CompilationUnit unit = (CompilationUnit) parser.createAST(null);
            if ((pack.isDefaultPackage() || unit.getPackage() != null) && !unit.types().isEmpty()) {
                return content;
            }
        }
        StringBuffer buf = new StringBuffer();
        if (!pack.isDefaultPackage()) {
View Full Code Here

Examples of org.eclipse.jdt.core.dom.CompilationUnit

        CodeGenerationSettings settings = JavaPreferencesSettings.getCodeGenerationSettings(type.getJavaProject());
        settings.createComments = isAddComments();
        ASTParser parser = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
        parser.setResolveBindings(true);
        parser.setSource(cu);
        CompilationUnit unit = (CompilationUnit) parser.createAST(new SubProgressMonitor(monitor, 1));
        final ITypeBinding binding = ASTNodes.getTypeBinding(unit, type);
        if (binding != null) {
            if (doUnimplementedMethods) {
                AddUnimplementedMethodsOperation operation = new AddUnimplementedMethodsOperation(unit, binding, null, -1, false, true, false);
                operation.setCreateComments(isAddComments());
View Full Code Here

Examples of org.eclipse.jdt.core.dom.CompilationUnit

      return converter.getModel(false);
  }

  public IValue createM3FromFile(ISourceLocation loc, IString javaVersion, IEvaluatorContext eval) {
    try {
      CompilationUnit cu = this.getCompilationUnit(loc.getPath(), getFileContents(loc, eval), true, javaVersion);

      TypeStore store = new TypeStore();
      store.extendStore(eval.getHeap().getModule("lang::java::m3::Core").getStore());
      store.extendStore(eval.getHeap().getModule("lang::java::m3::AST").getStore());
      SourceConverter converter = new SourceConverter(store);

      converter.set(cu);
      converter.set(loc);
      cu.accept(converter);
      for (Iterator it = cu.getCommentList().iterator(); it.hasNext();) {
        Comment comment = (Comment) it.next();
        if (comment.isDocComment())
          continue;
        comment.accept(converter);
      }
View Full Code Here

Examples of org.eclipse.jdt.core.dom.CompilationUnit

    }
  }
 
  public IValue createM3FromString(ISourceLocation loc, IString contents, IString javaVersion, IEvaluatorContext eval) {
    try {
        CompilationUnit cu = this.getCompilationUnit(loc.getPath(), contents.getValue().toCharArray(), true, javaVersion);

        TypeStore store = new TypeStore();
        store.extendStore(eval.getHeap().getModule("lang::java::m3::Core").getStore());
        store.extendStore(eval.getHeap().getModule("lang::java::m3::AST").getStore());
        SourceConverter converter = new SourceConverter(store);

        converter.set(cu);
        converter.set(loc);
        cu.accept(converter);
        for (Iterator it = cu.getCommentList().iterator(); it.hasNext();) {
          Comment comment = (Comment) it.next();
          if (comment.isDocComment())
            continue;
          comment.accept(converter);
        }
View Full Code Here

Examples of org.eclipse.jdt.core.dom.CompilationUnit

   * Creates Rascal ASTs for Java source files
   */
  public IValue createAstFromFile(ISourceLocation loc, IBool collectBindings, IString javaVersion,
      IEvaluatorContext eval) {
    try {
      CompilationUnit cu = getCompilationUnit(loc.getPath(), getFileContents(loc, eval), collectBindings.getValue(), javaVersion);

      TypeStore store = new TypeStore();
      store.extendStore(eval.getHeap().getModule("lang::java::m3::AST").getStore());
      ASTConverter converter = new ASTConverter(store, collectBindings.getValue());

      converter.set(cu);
      converter.set(loc);
      cu.accept(converter);
     
      converter.insertCompilationUnitMessages(true, null);
      return converter.getValue();
    } catch (IOException e) {
      throw RuntimeExceptionFactory.io(VF.string(e.getMessage()), null, null);
View Full Code Here

Examples of org.eclipse.jdt.core.dom.CompilationUnit

  }
 
  public IValue createAstFromString(ISourceLocation loc, IString contents, IBool collectBindings, IString javaVersion,
      IEvaluatorContext eval) {
    try {
        CompilationUnit cu = getCompilationUnit(loc.getPath(), contents.getValue().toCharArray(), collectBindings.getValue(), javaVersion);

        TypeStore store = new TypeStore();
        store.extendStore(eval.getHeap().getModule("lang::java::m3::AST").getStore());
        ASTConverter converter = new ASTConverter(store, collectBindings.getValue());

        converter.set(cu);
        converter.set(loc);
        cu.accept(converter);
       
        converter.insertCompilationUnitMessages(true, null);
        return converter.getValue();
      } catch (IOException e) {
        throw RuntimeExceptionFactory.io(VF.string(e.getMessage()), null, null);
View Full Code Here

Examples of org.eclipse.jdt.internal.compiler.batch.CompilationUnit

          problemFactory, options, reportLocalDeclarations,
          optimizeStringLiterals);

      File sourceFile = findSourceFile(klass);
      sourceContents = read(sourceFile);
      CompilationUnit unit = new CompilationUnit(sourceContents,
          sourceFile.getName(), null);

      parser.parseCompilationUnit(unit, true);
    }
View Full Code Here

Examples of org.eclipse.jdt.internal.core.CompilationUnit

        Field fMovesPrivateField = moveModifications.getClass().getDeclaredField("fMoves");
        fMovesPrivateField.setAccessible(true);
        ArrayList<Object> fmoves = (ArrayList<Object>) fMovesPrivateField.get(moveModifications);
        for (Object fmove : fmoves) {
            if (fmove instanceof CompilationUnit) {
                CompilationUnit cu = (CompilationUnit)fmove;
                IPackageDeclaration[] packageDeclarations = cu.getPackageDeclarations();
                for (int i = 0; i < packageDeclarations.length; i++)
                    currentName = packageDeclarations[i].getElementName();
            }
        }
    }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.