Java Code Examples for org.eclipse.jdt.core.IType#createMethod()

The following examples show how to use org.eclipse.jdt.core.IType#createMethod() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HtmlBasedUiBinderResourceCreator.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void createOwnerClassMembers(IType ownerClass, ImportsManager imports,
    boolean addComments, boolean addSampleContent, IProgressMonitor monitor)
    throws JavaModelException {
  String uiBinderDecl = createUiBinderSubtype(ownerClass,
      "com.google.gwt.dom.client.Element", imports);
  ownerClass.createType(uiBinderDecl, null, false, monitor);

  String uiBinderField = createUiBinderStaticField(ownerClass, imports);
  ownerClass.createField(uiBinderField, null, false, monitor);

  String ctorSrc = createCtor(ownerClass, addSampleContent, addComments);
  IMethod ctor = ownerClass.createMethod(ctorSrc, null, false, monitor);

  if (addSampleContent) {
    String uiField = createUiField("com.google.gwt.dom.client.SpanElement",
        "nameSpan", imports);
    ownerClass.createField(uiField, ctor, false, monitor);
  }
}
 
Example 2
Source File: MethodGeneratorImpl.java    From jenerate with Eclipse Public License 1.0 6 votes vote down vote up
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 : 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);
}
 
Example 3
Source File: NewReducerWizard.java    From RDFS with Apache License 2.0 6 votes vote down vote up
@Override
protected void createTypeMembers(IType newType, ImportsManager imports,
    IProgressMonitor monitor) throws CoreException {
  super.createTypeMembers(newType, imports, monitor);
  imports.addImport("java.io.IOException");
  imports.addImport("org.apache.hadoop.io.WritableComparable");
  imports.addImport("org.apache.hadoop.mapred.OutputCollector");
  imports.addImport("org.apache.hadoop.mapred.Reporter");
  imports.addImport("java.util.Iterator");
  newType
      .createMethod(
          "public void reduce(WritableComparable _key, Iterator values, OutputCollector output, Reporter reporter) throws IOException \n{\n"
              + "\t// replace KeyType with the real type of your key\n"
              + "\tKeyType key = (KeyType) _key;\n\n"
              + "\twhile (values.hasNext()) {\n"
              + "\t\t// replace ValueType with the real type of your value\n"
              + "\t\tValueType value = (ValueType) values.next();\n\n"
              + "\t\t// process value\n" + "\t}\n" + "}\n", null, false,
          monitor);
}
 
Example 4
Source File: NewReducerWizard.java    From hadoop-gpu with Apache License 2.0 6 votes vote down vote up
@Override
protected void createTypeMembers(IType newType, ImportsManager imports,
    IProgressMonitor monitor) throws CoreException {
  super.createTypeMembers(newType, imports, monitor);
  imports.addImport("java.io.IOException");
  imports.addImport("org.apache.hadoop.io.WritableComparable");
  imports.addImport("org.apache.hadoop.mapred.OutputCollector");
  imports.addImport("org.apache.hadoop.mapred.Reporter");
  imports.addImport("java.util.Iterator");
  newType
      .createMethod(
          "public void reduce(WritableComparable _key, Iterator values, OutputCollector output, Reporter reporter) throws IOException \n{\n"
              + "\t// replace KeyType with the real type of your key\n"
              + "\tKeyType key = (KeyType) _key;\n\n"
              + "\twhile (values.hasNext()) {\n"
              + "\t\t// replace ValueType with the real type of your value\n"
              + "\t\tValueType value = (ValueType) values.next();\n\n"
              + "\t\t// process value\n" + "\t}\n" + "}\n", null, false,
          monitor);
}
 
Example 5
Source File: WidgetBasedUiBinderResourceCreator.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void createOwnerClassMembers(IType ownerClass, ImportsManager imports,
    boolean addComments, boolean addSampleContent, IProgressMonitor monitor)
    throws JavaModelException {
  String uiBinderDecl = createUiBinderSubtype(ownerClass,
      "com.google.gwt.user.client.ui.Widget", imports);
  ownerClass.createType(uiBinderDecl, null, false, monitor);

  String uiBinderField = createUiBinderStaticField(ownerClass, imports);
  ownerClass.createField(uiBinderField, null, false, monitor);

  String defaultCtorSrc = createDefaultCtor(ownerClass, addComments);
  ownerClass.createMethod(defaultCtorSrc, null, false, monitor);

  if (addSampleContent) {
    String ctorSrc = createCtor(ownerClass, addSampleContent, addComments);
    IMethod ctor = ownerClass.createMethod(ctorSrc, null, false, monitor);

    String uiField = createUiField("com.google.gwt.user.client.ui.Button",
        "button", imports);
    ownerClass.createField(uiField, ctor, false, monitor);

    String eventHandler = createEventHandler(imports);
    ownerClass.createMethod(eventHandler, null, false, monitor);

    String getterSrc = createGetter(ownerClass);
    ownerClass.createMethod(getterSrc, null, false, monitor);

    String setterSrc = createSetter(ownerClass, addComments);
    ownerClass.createMethod(setterSrc, null, false, monitor);
  }
}
 
Example 6
Source File: NewAsyncRemoteServiceInterfaceCreationWizardPage.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected void createTypeMembers(IType newType, final ImportsManager imports,
    IProgressMonitor monitor) throws CoreException {
  TypeDeclaration asyncTypeDeclaration = JavaASTUtils.findTypeDeclaration(
      newType.getJavaProject(), newType.getFullyQualifiedName('.'));
  ITypeBinding asyncTypeBinding = asyncTypeDeclaration.resolveBinding();

  ImportManagerAdapter importAdapter = new ImportManagerAdapter() {

    public String addImport(ITypeBinding typeBinding) {
      return imports.addImport(typeBinding);
    }

    public String addImport(String qualifiedTypeName) {
      return imports.addImport(qualifiedTypeName);
    }
  };

  List<IMethodBinding> syncMethodsToConvert = computeSyncMethodsThatNeedAsyncVersions(
      syncTypeBinding, asyncTypeBinding);
  for (IMethodBinding overridableSyncMethod : syncMethodsToConvert) {
    String methodContents = createMethodContents(newType, importAdapter,
        overridableSyncMethod, isAddComments());

    // Create the new method
    newType.createMethod(methodContents, null, false, monitor);
  }
}
 
Example 7
Source File: ClientBundleResource.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
public void addToClientBundle(IType clientBundle, ImportsManager imports, boolean addComments,
    IProgressMonitor monitor) throws CoreException {
  String methodSource =
      MessageFormat.format("{0} {1}();", Signature.getSimpleName(getReturnTypeName()), getMethodName());

  String sourceAnnotation = getSourceAnnotationValue(clientBundle);
  if (sourceAnnotation != null) {
    // Insert the annotation above the method declaration
    methodSource = MessageFormat.format("@Source(\"{0}\")", sourceAnnotation)
        + StubUtility.getLineDelimiterUsed(clientBundle.getJavaProject()) + methodSource;
  }

  clientBundle.createMethod(methodSource, null, false, monitor);
  imports.addImport(getReturnTypeName());
}
 
Example 8
Source File: NewMapperWizard.java    From RDFS with Apache License 2.0 5 votes vote down vote up
@Override
protected void createTypeMembers(IType newType, ImportsManager imports,
    IProgressMonitor monitor) throws CoreException {
  super.createTypeMembers(newType, imports, monitor);
  imports.addImport("java.io.IOException");
  imports.addImport("org.apache.hadoop.io.WritableComparable");
  imports.addImport("org.apache.hadoop.io.Writable");
  imports.addImport("org.apache.hadoop.mapred.OutputCollector");
  imports.addImport("org.apache.hadoop.mapred.Reporter");
  newType
      .createMethod(
          "public void map(WritableComparable key, Writable values, OutputCollector output, Reporter reporter) throws IOException \n{\n}\n",
          null, false, monitor);
}
 
Example 9
Source File: NewMapperWizard.java    From hadoop-gpu with Apache License 2.0 5 votes vote down vote up
@Override
protected void createTypeMembers(IType newType, ImportsManager imports,
    IProgressMonitor monitor) throws CoreException {
  super.createTypeMembers(newType, imports, monitor);
  imports.addImport("java.io.IOException");
  imports.addImport("org.apache.hadoop.io.WritableComparable");
  imports.addImport("org.apache.hadoop.io.Writable");
  imports.addImport("org.apache.hadoop.mapred.OutputCollector");
  imports.addImport("org.apache.hadoop.mapred.Reporter");
  newType
      .createMethod(
          "public void map(WritableComparable key, Writable values, OutputCollector output, Reporter reporter) throws IOException \n{\n}\n",
          null, false, monitor);
}
 
Example 10
Source File: GeneratedCssResource.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 4 votes vote down vote up
private void createType(IPackageFragment pckg, boolean addComments)
    throws CoreException {
  IJavaProject javaProject = pckg.getJavaProject();
  final IProgressMonitor monitor = new NullProgressMonitor();

  // Method name should already have been sanitized and validated, so all we
  // should have to do to get a type name is just capitalize it
  String simpleName = StringUtilities.capitalize(getMethodName());

  // See if the type name is already used
  String qualifiedName = JavaModelUtil.concatenateName(pckg.getElementName(),
      simpleName);
  IType existingType = JavaModelSearch.findType(javaProject, qualifiedName);
  if (existingType != null) {
    if (ClientBundleUtilities.isCssResource(javaProject, existingType)) {
      // If the existing type is a CssResource, we'll assume that it wraps
      // this CSS file and use it for our ClientBundle accessor return type
      // instead of trying to generate another CssResource here.
      customCssResourceType = existingType;
      return;
    } else {
      // If it's not a CssResource, then we'll need to generate a CssResource
      // ourself, but we can't use the name. So, let's compute a similar name
      // that is not already in use.
      simpleName = StringUtilities.computeUniqueName(
          getExistingTopLevelTypeNames(pckg), simpleName);
    }
  }

  // Parse the CSS and see if there were problems
  CssParseResult result = parseCss();
  final IStatus status = result.getStatus();

  // Bail out when errors occur
  if (status.getSeverity() == IStatus.ERROR) {
    throw new CoreException(status);
  }

  // For warnings, just display them in a dialog (on the UI thread of course)
  // TODO: would nice if we could aggregate these and show them all at the end
  if (status.getSeverity() == IStatus.WARNING) {
    Display.getDefault().syncExec(new Runnable() {
      public void run() {
        MessageDialog.openWarning(null, "CSS Parsing", status.getMessage());
      }
    });
  }

  // Extract the CSS class names
  final Set<String> cssClassNames = ExtractClassNamesVisitor.exec(result.getStylesheet());

  TypeCreator gen = new TypeCreator(pckg, simpleName,
      TypeCreator.ElementType.INTERFACE,
      new String[] {ClientBundleUtilities.CSS_RESOURCE_TYPE_NAME},
      addComments) {
    @Override
    protected void createTypeMembers(IType newType, ImportRewrite imports)
        throws CoreException {
      // Create an accessor method for each CSS class
      for (String cssClass : cssClassNames) {
        newType.createMethod(computeCssClassMethodSource(newType, cssClass),
            null, true, monitor);
      }
    }
  };
  customCssResourceType = gen.createType();
}
 
Example 11
Source File: NewClassWizardPage.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
protected void createTypeMembers(IType type, ImportsManager imports, IProgressMonitor monitor) throws CoreException {
	boolean doMain= isCreateMain();
	boolean doConstr= isCreateConstructors();
	boolean doInherited= isCreateInherited();
	createInheritedMethods(type, doConstr, doInherited, imports, new SubProgressMonitor(monitor, 1));

	if (doMain) {
		StringBuffer buf= new StringBuffer();
		final String lineDelim= "\n"; // OK, since content is formatted afterwards //$NON-NLS-1$
		if (isAddComments()) {
			String comment= CodeGeneration.getMethodComment(type.getCompilationUnit(), type.getTypeQualifiedName('.'), "main", new String[] { "args" }, new String[0], Signature.createTypeSignature("void", true), null, lineDelim); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
			if (comment != null) {
				buf.append(comment);
				buf.append(lineDelim);
			}
		}
		buf.append("public static void main("); //$NON-NLS-1$
		buf.append(imports.addImport("java.lang.String")); //$NON-NLS-1$
		buf.append("[] args) {"); //$NON-NLS-1$
		buf.append(lineDelim);
		final String content= CodeGeneration.getMethodBodyContent(type.getCompilationUnit(), type.getTypeQualifiedName('.'), "main", false, "", lineDelim); //$NON-NLS-1$ //$NON-NLS-2$
		if (content != null && content.length() != 0)
			buf.append(content);
		buf.append(lineDelim);
		buf.append("}"); //$NON-NLS-1$
		type.createMethod(buf.toString(), null, false, null);
	}

	IDialogSettings dialogSettings= getDialogSettings();
	if (dialogSettings != null) {
		IDialogSettings section= dialogSettings.getSection(PAGE_NAME);
		if (section == null) {
			section= dialogSettings.addNewSection(PAGE_NAME);
		}
		section.put(SETTINGS_CREATECONSTR, isCreateConstructors());
		section.put(SETTINGS_CREATEUNIMPLEMENTED, isCreateInherited());
	}

	if (monitor != null) {
		monitor.done();
	}
}