Examples of ImportDeclaration


Examples of com.bacoder.parser.java.api.ImportDeclaration

    super(adapters);
  }

  @Override
  public ImportDeclaration adapt(ImportDeclarationContext context) {
    ImportDeclaration importDeclaration = createNode(context);
    importDeclaration.setStatic(hasTerminalNode(context, JavaParser.STATIC));
    importDeclaration.setAsterisk(hasTerminalNode(context, JavaParser.MUL));

    QualifiedNameContext qualifiedNameContext = getChild(context, QualifiedNameContext.class);
    if (qualifiedNameContext != null) {
      importDeclaration.setQualifiedName(
          getAdapter(QualifiedNameAdapter.class).adapt(qualifiedNameContext));
    }

    return importDeclaration;
  }
View Full Code Here

Examples of com.github.antlrjavaparser.api.ImportDeclaration

        Validate.notNull(
                compilationUnit.getImports(),
                "Compilation unit imports should be non-null when producing type '%s'",
                cid.getName());
        for (final ImportMetadata importType : cid.getRegisteredImports()) {
            ImportDeclaration importDeclaration;

            if (!importType.isAsterisk()) {
                NameExpr typeToImportExpr;
                if (importType.getImportType().getEnclosingType() == null) {
                    typeToImportExpr = new QualifiedNameExpr(new NameExpr(
                            importType.getImportType().getPackage()
                                    .getFullyQualifiedPackageName()),
                            importType.getImportType().getSimpleTypeName());
                }
                else {
                    typeToImportExpr = new QualifiedNameExpr(new NameExpr(
                            importType.getImportType().getEnclosingType()
                                    .getFullyQualifiedTypeName()), importType
                            .getImportType().getSimpleTypeName());
                }

                importDeclaration = new ImportDeclaration(typeToImportExpr,
                        importType.isStatic(), false);
            }
            else {
                importDeclaration = new ImportDeclaration(new NameExpr(
                        importType.getImportPackage()
                                .getFullyQualifiedPackageName()),
                        importType.isStatic(), importType.isAsterisk());
            }

            JavaParserCommentMetadataBuilder.updateCommentsToJavaParser(
                    importDeclaration, importType.getCommentStructure());

            compilationUnit.getImports().add(importDeclaration);
        }

        // Create a class or interface declaration to represent this actual type
        final int javaParserModifier = JavaParserUtils
                .getJavaParserModifier(cid.getModifier());
        TypeDeclaration typeDeclaration;
        ClassOrInterfaceDeclaration classOrInterfaceDeclaration;

        // Implements handling
        final List<ClassOrInterfaceType> implementsList = new ArrayList<ClassOrInterfaceType>();
        for (final JavaType current : cid.getImplementsTypes()) {
            implementsList.add(JavaParserUtils.getResolvedName(cid.getName(),
                    current, compilationUnit));
        }

        if (cid.getPhysicalTypeCategory() == PhysicalTypeCategory.INTERFACE
                || cid.getPhysicalTypeCategory() == PhysicalTypeCategory.CLASS) {
            final boolean isInterface = cid.getPhysicalTypeCategory() == PhysicalTypeCategory.INTERFACE;

            if (parent == null) {
                // Top level type
                typeDeclaration = new ClassOrInterfaceDeclaration(
                        javaParserModifier, isInterface, cid
                                .getName()
                                .getNameIncludingTypeParameters()
                                .replace(
                                        cid.getName().getPackage()
                                                .getFullyQualifiedPackageName()
                                                + ".", ""));
                classOrInterfaceDeclaration = (ClassOrInterfaceDeclaration) typeDeclaration;
            }
            else {
                // Inner type
                typeDeclaration = new ClassOrInterfaceDeclaration(
                        javaParserModifier, isInterface, cid.getName()
                                .getSimpleTypeName());
                classOrInterfaceDeclaration = (ClassOrInterfaceDeclaration) typeDeclaration;

                if (cid.getName().getParameters().size() > 0) {
                    classOrInterfaceDeclaration
                            .setTypeParameters(new ArrayList<TypeParameter>());

                    for (final JavaType param : cid.getName().getParameters()) {
                        NameExpr pNameExpr = JavaParserUtils
                                .importTypeIfRequired(cid.getName(),
                                        compilationUnit.getImports(), param);
                        final String tempName = StringUtils.replace(
                                pNameExpr.toString(), param.getArgName()
                                        + " extends ", "", 1);
                        pNameExpr = new NameExpr(tempName);
                        final ClassOrInterfaceType pResolvedName = JavaParserUtils
                                .getClassOrInterfaceType(pNameExpr);
                        classOrInterfaceDeclaration.getTypeParameters().add(
                                new TypeParameter(param.getArgName()
                                        .getSymbolName(), Collections
                                        .singletonList(pResolvedName)));
                    }
                }
            }

            // Superclass handling
            final List<ClassOrInterfaceType> extendsList = new ArrayList<ClassOrInterfaceType>();
            for (final JavaType current : cid.getExtendsTypes()) {
                if (!OBJECT.equals(current)) {
                    extendsList.add(JavaParserUtils.getResolvedName(
                            cid.getName(), current, compilationUnit));
                }
            }
            if (extendsList.size() > 0) {
                classOrInterfaceDeclaration.setExtends(extendsList);
            }

            // Implements handling
            if (implementsList.size() > 0) {
                classOrInterfaceDeclaration.setImplements(implementsList);
            }
        }
        else {
            typeDeclaration = new EnumDeclaration(javaParserModifier, cid
                    .getName().getSimpleTypeName());
        }
        typeDeclaration.setMembers(new ArrayList<BodyDeclaration>());

        Validate.notNull(typeDeclaration.getName(),
                "Missing type declaration name for '%s'", cid.getName());

        // If adding a new top-level type, must add it to the compilation unit
        // types
        Validate.notNull(
                compilationUnit.getTypes(),
                "Compilation unit types must not be null when attempting to add '%s'",
                cid.getName());

        if (parent == null) {
            // Top-level class
            compilationUnit.getTypes().add(typeDeclaration);
        }
        else {
            // Inner class
            parent.add(typeDeclaration);
        }

        // If the enclosing CompilationUnitServices was not provided a default
        // CompilationUnitServices needs to be created
        if (enclosingCompilationUnitServices == null) {
            // Create a compilation unit so that we can use JavaType*Metadata
            // static methods directly
            enclosingCompilationUnitServices = new CompilationUnitServices() {
                @Override
                public JavaPackage getCompilationUnitPackage() {
                    return cid.getName().getPackage();
                }

                @Override
                public JavaType getEnclosingTypeName() {
                    return cid.getName();
                }

                @Override
                public List<ImportDeclaration> getImports() {
                    return compilationUnit.getImports();
                }

                @Override
                public List<TypeDeclaration> getInnerTypes() {
                    return compilationUnit.getTypes();
                }

                @Override
                public PhysicalTypeCategory getPhysicalTypeCategory() {
                    return cid.getPhysicalTypeCategory();
                }
            };
        }

        final CompilationUnitServices finalCompilationUnitServices = enclosingCompilationUnitServices;
        // A hybrid CompilationUnitServices must be provided that references the
        // enclosing types imports and package
        final CompilationUnitServices compilationUnitServices = new CompilationUnitServices() {
            @Override
            public JavaPackage getCompilationUnitPackage() {
                return finalCompilationUnitServices.getCompilationUnitPackage();
            }

            @Override
            public JavaType getEnclosingTypeName() {
                return cid.getName();
            }

            @Override
            public List<ImportDeclaration> getImports() {
                return finalCompilationUnitServices.getImports();
            }

            @Override
            public List<TypeDeclaration> getInnerTypes() {
                return compilationUnit.getTypes();
            }

            @Override
            public PhysicalTypeCategory getPhysicalTypeCategory() {
                return cid.getPhysicalTypeCategory();
            }
        };

        // Add type annotations
        final List<AnnotationExpr> annotations = new ArrayList<AnnotationExpr>();
        typeDeclaration.setAnnotations(annotations);
        for (final AnnotationMetadata candidate : cid.getAnnotations()) {
            JavaParserAnnotationMetadataBuilder.addAnnotationToList(
                    compilationUnitServices, annotations, candidate);
        }

        // Add enum constants and interfaces
        if (typeDeclaration instanceof EnumDeclaration
                && cid.getEnumConstants().size() > 0) {
            final EnumDeclaration enumDeclaration = (EnumDeclaration) typeDeclaration;

            final List<EnumConstantDeclaration> constants = new ArrayList<EnumConstantDeclaration>();
            enumDeclaration.setEntries(constants);

            for (final JavaSymbolName constant : cid.getEnumConstants()) {
                addEnumConstant(constants, constant);
            }

            // Implements handling
            if (implementsList.size() > 0) {
                enumDeclaration.setImplements(implementsList);
            }
        }

        // Add fields
        for (final FieldMetadata candidate : cid.getDeclaredFields()) {
            JavaParserFieldMetadataBuilder.addField(compilationUnitServices,
                    typeDeclaration.getMembers(), candidate);
        }

        // Add constructors
        for (final ConstructorMetadata candidate : cid
                .getDeclaredConstructors()) {
            JavaParserConstructorMetadataBuilder.addConstructor(
                    compilationUnitServices, typeDeclaration.getMembers(),
                    candidate, null);
        }

        // Add methods
        for (final MethodMetadata candidate : cid.getDeclaredMethods()) {
            JavaParserMethodMetadataBuilder.addMethod(compilationUnitServices,
                    typeDeclaration.getMembers(), candidate, null);
        }

        // Add inner types
        for (final ClassOrInterfaceTypeDetails candidate : cid
                .getDeclaredInnerTypes()) {
            updateOutput(compilationUnit, compilationUnitServices, candidate,
                    typeDeclaration.getMembers());
        }

        final HashSet<String> imported = new HashSet<String>();
        final ArrayList<ImportDeclaration> imports = new ArrayList<ImportDeclaration>();
        for (final ImportDeclaration importDeclaration : compilationUnit
                .getImports()) {
            JavaPackage importPackage = null;
            JavaType importType = null;
            if (importDeclaration.isAsterisk()) {
                importPackage = new JavaPackage(importDeclaration.getName()
                        .toString());
            }
            else {
                importType = new JavaType(importDeclaration.getName()
                        .toString());
                importPackage = importType.getPackage();
            }

            if (importPackage.equals(cid.getName().getPackage())
                    && importDeclaration.isAsterisk()) {
                continue;
            }

            if (importPackage.equals(cid.getName().getPackage())
                    && importType != null
                    && importType.getEnclosingType() == null) {
                continue;
            }

            if (importType != null && importType.equals(cid.getName())) {
                continue;
            }

            if (!imported.contains(importDeclaration.getName().toString())) {
                imports.add(importDeclaration);
                imported.add(importDeclaration.getName().toString());
            }
        }

        Collections.sort(imports, new Comparator<ImportDeclaration>() {
            @Override
            public int compare(final ImportDeclaration importDeclaration,
                    final ImportDeclaration importDeclaration1) {
                return importDeclaration.getName().toString()
                        .compareTo(importDeclaration1.getName().toString());
            }
        });

        compilationUnit.setImports(imports);
View Full Code Here

Examples of com.strobel.decompiler.languages.java.ast.ImportDeclaration

        if (removedImports.isEmpty()) {
            return;
        }

        final ImportDeclaration lastRemoved = removedImports.get(removedImports.size() - 1);

        for (final String packageName : newImports) {
            compilationUnit.insertChildAfter(
                lastRemoved,
                new ImportDeclaration(PackageReference.parse(packageName)),
                CompilationUnit.IMPORT_ROLE
            );
        }

        for (final ImportDeclaration removedImport : removedImports) {
View Full Code Here

Examples of com.stuffwithstuff.magpie.ast.ImportDeclaration

          getPattern(expr, "pattern"),
          getExpr(expr, "body"));
    } else if (exprClass == getClass("ImportExpression")) {
      List<ImportDeclaration> declarations = new ArrayList<ImportDeclaration>();
      for (Obj declaration : expr.getField("declarations").asList()) {
        declarations.add(new ImportDeclaration(
            getBool(declaration, "isExport"),
            getString(declaration, "name"),
            getString(declaration, "rename")));
      }
View Full Code Here

Examples of japa.parser.ast.ImportDeclaration

        return this;
    }

    public CompilationUnitBuilder buildImports(String[] imports) {
        List<ImportDeclaration> importList = new ArrayList<ImportDeclaration>();
        importList.add(new ImportDeclaration(new NameExpr("org.testng.annotations.Test"), false, false));
        cu.setImports(importList);
        return this;
    }
View Full Code Here

Examples of lombok.ast.ImportDeclaration

    if (tail != null) for (Node n : tail) if (n != null) decl.rawParts().addToEnd(n);
    return posify(decl);
  }
 
  public Node createImportDeclaration(String staticKeyword, Node head, List<Node> tail, String dotStar) {
    ImportDeclaration decl = new ImportDeclaration();
    if (head != null) decl.rawParts().addToEnd(head);
    if (tail != null) for (Node n : tail) decl.rawParts().addToEnd(n);
    if (staticKeyword != null && staticKeyword.length() > 0) decl.astStaticImport(true);
    if (dotStar != null && dotStar.length() > 0) decl.astStarImport(true);
    return posify(decl);
  }
View Full Code Here

Examples of org.aspectj.org.eclipse.jdt.core.dom.ImportDeclaration

    if (decls.isEmpty()) {
      return;
    }       
    PackageEntry currPackage= null;
     
    ImportDeclaration curr= (ImportDeclaration) decls.get(0);
    int currOffset= curr.getStartPosition();
    int currLength= curr.getLength();
    int currEndLine= root.getLineNumber(currOffset + currLength);
   
    for (int i= 1; i < decls.size(); i++) {
      boolean isStatic= curr.isStatic();
      String name= getFullName(curr);
      String packName= getQualifier(curr);
      if (currPackage == null || currPackage.compareTo(packName, isStatic) != 0) {
        currPackage= new PackageEntry(packName, null, isStatic);
        this.packageEntries.add(currPackage);
      }

      ImportDeclaration next= (ImportDeclaration) decls.get(i);
      int nextOffset= next.getStartPosition();
      int nextLength= next.getLength();
      int nextOffsetLine= root.getLineNumber(nextOffset);

      // if next import is on a different line, modify the end position to the next line begin offset
      if (currEndLine < nextOffsetLine) {
        currEndLine++;
View Full Code Here

Examples of org.aspectj.org.eclipse.jdt.core.dom.ImportDeclaration

  }
     
  private IRegion evaluateReplaceRange(CompilationUnit root) {
    List imports= root.imports();
    if (!imports.isEmpty()) {
      ImportDeclaration first= (ImportDeclaration) imports.get(0);
      ImportDeclaration last= (ImportDeclaration) imports.get(imports.size() - 1);
     
      int startPos= first.getStartPosition(); // no extended range for first: bug 121428
      int endPos= root.getExtendedStartPosition(last) + root.getExtendedLength(last);
      int endLine= root.getLineNumber(endPos);
      if (endLine > 0) {
View Full Code Here

Examples of org.aspectj.org.eclipse.jdt.core.dom.ImportDeclaration

  String importActualName = this.importName;
  if (onDemand) {
    importActualName = this.importName.substring(0, this.importName.length() - 2);
  }
  while (imports.hasNext()) {
    ImportDeclaration importDeclaration = (ImportDeclaration) imports.next();
    if (importActualName.equals(importDeclaration.getName().getFullyQualifiedName())
        && (onDemand == importDeclaration.isOnDemand())
        && (Flags.isStatic(this.flags) == importDeclaration.isStatic())) {
      this.creationOccurred = false;
      return null;
    }
  }
 
  AST ast = this.cuAST.getAST();
  ImportDeclaration importDeclaration = ast.newImportDeclaration();
  importDeclaration.setStatic(Flags.isStatic(this.flags));
  // split import name into individual fragments, checking for on demand imports
  char[][] charFragments = CharOperation.splitOn('.', importActualName.toCharArray(), 0, importActualName.length());
  int length = charFragments.length;
  String[] strFragments = new String[length];
  for (int i = 0; i < length; i++) {
    strFragments[i] = String.valueOf(charFragments[i]);
  }
  Name name = ast.newName(strFragments);
  importDeclaration.setName(name);
  if (onDemand) importDeclaration.setOnDemand(true);
  return importDeclaration;
}
View Full Code Here

Examples of org.codehaus.janino.Java.CompilationUnit.ImportDeclaration

        // Compile non-static import declarations. (Must be done here in the constructor and not
        // down in "compileUnit()" because otherwise "resolve()" cannot resolve type names.)
        this.typeImportsOnDemand = new ArrayList();
        this.typeImportsOnDemand.add(new String[] { "java", "lang" });
        for (Iterator it = this.compilationUnit.importDeclarations.iterator(); it.hasNext();) {
            ImportDeclaration id = (ImportDeclaration) it.next();
            class UCE extends RuntimeException { final CompileException ce; UCE(CompileException ce) { this.ce = ce; } }
            try {
                id.accept(new ImportVisitor() {
                    // CHECKSTYLE(LineLengthCheck):OFF
                    public void visitSingleTypeImportDeclaration(SingleTypeImportDeclaration stid)          { try { UnitCompiler.this.import2(stid)} catch (CompileException e) { throw new UCE(e); } }
                    public void visitTypeImportOnDemandDeclaration(TypeImportOnDemandDeclaration tiodd)     {       UnitCompiler.this.import2(tiodd);                                                    }
                    public void visitSingleStaticImportDeclaration(SingleStaticImportDeclaration ssid)      {}
                    public void visitStaticImportOnDemandDeclaration(StaticImportOnDemandDeclaration siodd) {}
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.