public static IWorkspaceRoot setupWorkspace()
      throws CoreException, IOException, InvocationTargetException, InterruptedException {
    if (isSetup) {
      clearDoiModel();
      return workspaceRoot;
    }
    taskscape = new InteractionContext(HELPER_CONTEXT_ID, new InteractionContextScaling());

    workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();

    project1 = ContextTestUtil.createJavaPluginProjectFromZip("project1", "project1.zip");
    project2 = ContextTestUtil.createJavaPluginProjectFromZip("project2", "project2.zip");

    jdtCoreDomProject = new TestJavaProject("workspace-helper-project");
    IPackageFragment jdtCoreDomPkg = jdtCoreDomProject.createPackage("org.eclipse.jdt.core.dom");
    IType astNodeType =
        jdtCoreDomProject.createType(jdtCoreDomPkg, "ASTNode.java", "public class ASTNode { }");
    astNodeType.createMethod(
        "public final void setSourceRange(int startPosition, int length) { }", null, false, null);
    isSetup = true;

    project1.open(new NullProgressMonitor());
    project2.open(new NullProgressMonitor());
    jdtCoreDomProject.getJavaProject().open(new NullProgressMonitor());

    return workspaceRoot;
  }
 @Override
 public void createTypeMembers(
     Map<String, Object> options, IType newType, ImportsManager imports, IProgressMonitor monitor)
     throws CoreException {
   imports.addImport("org.mule.api.annotations.Connector");
   imports.addImport("org.mule.api.annotations.Processor");
   newType.createMethod("@Processor public void foo(){ }", null, true, monitor);
 }
Ejemplo n.º 3
0
  private void generateCode(
      Shell parentShell, IType objectClass, U data, LinkedHashSet<Method<T, U>> methods)
      throws Exception {
    IJavaElement currentPosition = data.getElementPosition();
    for (Method<T, U> method : methods) {
      MethodContent<T, U> methodContent = method.getMethodContent();
      T methodSkeleton = method.getMethodSkeleton();
      String methodContentString = methodContent.getMethodContent(objectClass, data);
      String source = methodSkeleton.getMethod(objectClass, data, methodContentString);

      for (String libraryToImport : methodSkeleton.getLibrariesToImport()) {
        objectClass.getCompilationUnit().createImport(libraryToImport, null, null);
      }
      for (String libraryToImport : methodContent.getLibrariesToImport(data)) {
        objectClass.getCompilationUnit().createImport(libraryToImport, null, null);
      }
      String formattedContent = format(parentShell, objectClass, source);
      currentPosition = objectClass.createMethod(formattedContent, currentPosition, true, null);
    }
    javaUiCodeAppender.revealInEditor(objectClass, currentPosition);
  }
  public void run(IMarker marker) {
    IFile file = (IFile) javaDeclaration.getResource();
    try {
      ICompilationUnit original = EclipseUtil.getCompilationUnit(file);
      if (original == null) {
        return;
      }
      ICompilationUnit compilationUnit = original.getWorkingCopy(new NullProgressMonitor());

      String lineDelim = JavaPropertyGenerator.getLineDelimiterUsed(compilationUnit);

      Hashtable<String, String> options = JavaCore.getOptions();

      int tabSize = new Integer(options.get("org.eclipse.jdt.core.formatter.tabulation.size"));

      StringBuffer tabBuf = new StringBuffer();

      for (int i = 0; i < tabSize; i++) tabBuf.append(" ");

      String tab = tabBuf.toString();

      IType type = compilationUnit.findPrimaryType();

      IField field = type.getField(property.getName());

      String propertyType = "";
      if (field != null && field.exists()) {
        propertyType = field.getTypeSignature();
      } else {
        propertyType = "String"; // $NON-NLS-1$

        StringBuffer buf = new StringBuffer();

        buf.append(
            tab
                + "private "
                + propertyType
                + " "
                + property.getName()
                + ";"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        buf.append(lineDelim);

        field = type.createField(buf.toString(), null, false, new NullProgressMonitor());
        if (field != null) {
          IBuffer buffer = compilationUnit.getBuffer();

          buffer.replace(
              field.getSourceRange().getOffset(),
              field.getSourceRange().getLength(),
              buf.toString());
          synchronized (compilationUnit) {
            compilationUnit.reconcile(ICompilationUnit.NO_AST, true, null, null);
          }
        }
      }

      IMethod oldMethod = GetterSetterUtil.getSetter(field);
      if (oldMethod == null || !oldMethod.exists()) {
        String setterName = GetterSetterUtil.getSetterName(field, null);
        // JavaPropertyGenerator.createSetter(compilationUnit, type, "public",
        // field.getTypeSignature(), setterName, lineDelim);

        String stub = GetterSetterUtil.getSetterStub(field, setterName, true, Flags.AccPublic);
        IMethod newMethod = type.createMethod(stub, null, false, new NullProgressMonitor());
        if (newMethod != null) {
          IBuffer buffer = compilationUnit.getBuffer();
          // format
          StringBuffer buf = new StringBuffer();

          buf.append(lineDelim);
          buf.append(
              tab
                  + "public void "
                  + setterName
                  + "("
                  + propertyType
                  + " "
                  + property.getName()
                  + "){"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
          buf.append(lineDelim);
          buf.append(tab + tab + "this." + property.getName() + " = " + property.getName() + ";");
          buf.append(lineDelim);
          buf.append(tab + "}");
          buf.append(lineDelim);

          buffer.replace(
              newMethod.getSourceRange().getOffset(),
              newMethod.getSourceRange().getLength(),
              buf.toString());
        }
      }

      compilationUnit.commitWorkingCopy(false, new NullProgressMonitor());
      compilationUnit.discardWorkingCopy();

    } catch (CoreException ex) {
      SeamGuiPlugin.getPluginLog().logError(ex);
    }
  }
Ejemplo n.º 5
0
  /** {@inheritDoc} */
  public Object execute(CommandLine commandLine) throws Exception {
    String project = commandLine.getValue(Options.PROJECT_OPTION);
    String file = commandLine.getValue(Options.FILE_OPTION);
    String propertiesOption = commandLine.getValue(Options.PROPERTIES_OPTION);
    String[] properties = {};
    if (propertiesOption != null) {
      properties = StringUtils.split(propertiesOption, ',');
    }
    int offset = getOffset(commandLine);

    // validate supplied fields.
    ICompilationUnit src = JavaUtils.getCompilationUnit(project, file);
    IType type = TypeUtils.getType(src, offset);
    for (int ii = 0; ii < properties.length; ii++) {
      if (!type.getField(properties[ii]).exists()) {
        throw new RuntimeException(
            Services.getMessage("field.not.found", properties[ii], type.getElementName()));
      }
    }

    // check if constructor already exists.
    IMethod method = null;
    if (properties.length == 0) {
      method = type.getMethod(type.getElementName(), null);
    } else {
      String[] fieldSigs = new String[properties.length];
      for (int ii = 0; ii < properties.length; ii++) {
        fieldSigs[ii] = type.getField(properties[ii]).getTypeSignature();
      }
      method = type.getMethod(type.getElementName(), fieldSigs);
    }
    if (method.exists()) {
      throw new RuntimeException(
          Services.getMessage(
              "constructor.already.exists",
              type.getElementName() + " (" + buildParams(type, properties) + ")"));
    }

    // find the sibling to insert before.
    IJavaElement sibling = null;
    IMethod[] methods = type.getMethods();
    for (int ii = 0; ii < methods.length; ii++) {
      if (methods[ii].isConstructor()) {
        sibling = ii < methods.length - 1 ? methods[ii + 1] : null;
      }
    }
    // insert before any other methods or inner classes if any.
    if (sibling == null) {
      if (methods.length > 0) {
        sibling = methods[0];
      } else {
        IType[] types = type.getTypes();
        sibling = types != null && types.length > 0 ? types[0] : null;
      }
    }

    HashMap<String, Object> values = new HashMap<String, Object>();
    values.put("type", type.getElementName());
    boolean hasProperties = properties != null && properties.length > 0;
    values.put("fields", hasProperties ? properties : null);
    values.put("params", hasProperties ? buildParams(type, properties) : StringUtils.EMPTY);

    PluginResources resources = (PluginResources) Services.getPluginResources(PluginResources.NAME);
    String constructor = TemplateUtils.evaluate(resources, TEMPLATE, values);
    Position position =
        TypeUtils.getPosition(type, type.createMethod(constructor, sibling, false, null));
    JavaUtils.format(
        src, CodeFormatter.K_COMPILATION_UNIT, position.getOffset(), position.getLength());

    return null;
  }