/*
   * Extract method arguments using unique key for parameterized methods
   * and type parameters for non-generic ones.
   */
  char[][] extractMethodArguments(IMethod method) {

    // Use bind key if the element is resolved
    if (method.isResolved()) {
      BindingKey bindingKey = new BindingKey(method.getKey());
      if (bindingKey.isParameterizedMethod()) {
        String[] argumentsSignatures = bindingKey.getTypeArguments();
        int length = argumentsSignatures.length;
        if (length > 0) {
          char[][] methodArguments = new char[length][];
          for (int i = 0; i < length; i++) {
            methodArguments[i] = argumentsSignatures[i].toCharArray();
            CharOperation.replace(methodArguments[i], new char[] {'$', '/'}, '.');
          }
          return methodArguments;
        }
      }
      return null;
    }

    // Try to get the argument using the JavaModel info
    try {
      ITypeParameter[] parameters = method.getTypeParameters();
      if (parameters != null) {
        int length = parameters.length;
        if (length > 0) {
          char[][] arguments = new char[length][];
          for (int i = 0; i < length; i++) {
            arguments[i] =
                Signature.createTypeSignature(parameters[i].getElementName(), false).toCharArray();
          }
          return arguments;
        }
      }
    } catch (JavaModelException jme) {
      // do nothing
    }
    return null;
  }
예제 #2
0
  // Dump classfiles onto disk for all compilation units that where successful
  // and do not carry a -d none spec, either directly or inherited from Main.
  @Override
  public void outputClassFiles(CompilationResult unitResult) {
    if (!((unitResult == null) || (unitResult.hasErrors() && !this.proceedOnError))) {
      ClassFile[] classFiles = unitResult.getClassFiles();
      boolean generateClasspathStructure =
          this.fileManager.hasLocation(StandardLocation.CLASS_OUTPUT);
      String currentDestinationPath = this.destinationPath;
      File outputLocation = null;
      if (currentDestinationPath != null) {
        outputLocation = new File(currentDestinationPath);
        outputLocation.mkdirs();
      }
      for (int i = 0, fileCount = classFiles.length; i < fileCount; i++) {
        // retrieve the key and the corresponding classfile
        ClassFile classFile = classFiles[i];
        char[] filename = classFile.fileName();
        int length = filename.length;
        char[] relativeName = new char[length + 6];
        System.arraycopy(filename, 0, relativeName, 0, length);
        System.arraycopy(SuffixConstants.SUFFIX_class, 0, relativeName, length, 6);
        CharOperation.replace(relativeName, '/', File.separatorChar);
        String relativeStringName = new String(relativeName);
        if (this.compilerOptions.verbose) {
          EclipseCompilerImpl.this.out.println(
              Messages.bind(
                  Messages.compilation_write,
                  new String[] {
                    String.valueOf(this.exportedClassFilesCounter + 1), relativeStringName
                  }));
        }
        try {
          JavaFileObject javaFileForOutput =
              this.fileManager.getJavaFileForOutput(
                  StandardLocation.CLASS_OUTPUT,
                  new String(filename),
                  JavaFileObject.Kind.CLASS,
                  this.javaFileObjectMap.get(unitResult.compilationUnit));

          if (generateClasspathStructure) {
            if (currentDestinationPath != null) {
              int index = CharOperation.lastIndexOf(File.separatorChar, relativeName);
              if (index != -1) {
                File currentFolder =
                    new File(currentDestinationPath, relativeStringName.substring(0, index));
                currentFolder.mkdirs();
              }
            } else {
              // create the subfolfers is necessary
              // need a way to retrieve the folders to create
              String path = javaFileForOutput.toUri().getPath();
              int index = path.lastIndexOf('/');
              if (index != -1) {
                File file = new File(path.substring(0, index));
                file.mkdirs();
              }
            }
          }

          OutputStream openOutputStream = javaFileForOutput.openOutputStream();
          BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(openOutputStream);
          bufferedOutputStream.write(classFile.header, 0, classFile.headerOffset);
          bufferedOutputStream.write(classFile.contents, 0, classFile.contentsOffset);
          bufferedOutputStream.flush();
          bufferedOutputStream.close();
        } catch (IOException e) {
          this.logger.logNoClassFileCreated(currentDestinationPath, relativeStringName, e);
        }
        this.logger.logClassFile(
            generateClasspathStructure, currentDestinationPath, relativeStringName);
        this.exportedClassFilesCounter++;
      }
      this.batchCompiler.lookupEnvironment.releaseClassFiles(classFiles);
    }
  }