Search in sources :

Example 16 with CodeTemplateContext

use of org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext in project flux by eclipse.

the class StubUtility method getMethodBodyContent.

/*
	 * Don't use this method directly, use CodeGeneration.
	 */
public static String getMethodBodyContent(boolean isConstructor, IJavaProject project, String destTypeName, String methodName, String bodyStatement, String lineDelimiter) throws CoreException {
    String templateName = isConstructor ? CodeTemplateContextType.CONSTRUCTORSTUB_ID : CodeTemplateContextType.METHODSTUB_ID;
    Template template = getCodeTemplate(templateName, project);
    if (template == null) {
        return bodyStatement;
    }
    CodeTemplateContext context = new CodeTemplateContext(template.getContextTypeId(), project, lineDelimiter);
    context.setVariable(CodeTemplateContextType.ENCLOSING_METHOD, methodName);
    context.setVariable(CodeTemplateContextType.ENCLOSING_TYPE, destTypeName);
    context.setVariable(CodeTemplateContextType.BODY_STATEMENT, bodyStatement);
    String str = evaluateTemplate(context, template, new String[] { CodeTemplateContextType.BODY_STATEMENT });
    if (str == null && !Strings.containsOnlyWhitespaces(bodyStatement)) {
        return bodyStatement;
    }
    return str;
}
Also used : CodeTemplateContext(org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext) Template(org.eclipse.jface.text.templates.Template)

Example 17 with CodeTemplateContext

use of org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext in project flux by eclipse.

the class StubUtility method getTypeBody.

/**
	 * Don't use this method directly, use CodeGeneration.
	 * 
	 * @param templateID the template id of the type body to get. Valid id's are
	 *            {@link CodeTemplateContextType#CLASSBODY_ID},
	 *            {@link CodeTemplateContextType#INTERFACEBODY_ID},
	 *            {@link CodeTemplateContextType#ENUMBODY_ID},
	 *            {@link CodeTemplateContextType#ANNOTATIONBODY_ID},
	 * @param cu the compilation unit to which the template is added
	 * @param typeName the type name
	 * @param lineDelim the line delimiter to use
	 * @return return the type body template or <code>null</code>
	 * @throws CoreException thrown if the template could not be evaluated
	 */
public static String getTypeBody(String templateID, ICompilationUnit cu, String typeName, String lineDelim) throws CoreException {
    if (!VALID_TYPE_BODY_TEMPLATES.contains(templateID)) {
        //$NON-NLS-1$
        throw new IllegalArgumentException("Invalid code template ID: " + templateID);
    }
    Template template = getCodeTemplate(templateID, cu.getJavaProject());
    if (template == null) {
        return null;
    }
    CodeTemplateContext context = new CodeTemplateContext(template.getContextTypeId(), cu.getJavaProject(), lineDelim);
    context.setCompilationUnitVariables(cu);
    context.setVariable(CodeTemplateContextType.TYPENAME, typeName);
    return evaluateTemplate(context, template);
}
Also used : CodeTemplateContext(org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext) Template(org.eclipse.jface.text.templates.Template)

Example 18 with CodeTemplateContext

use of org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext in project che by eclipse.

the class UnimplementedCodeCleanUp method getOverridingMethodComment.

private String getOverridingMethodComment() {
    String templateName = CodeTemplateContextType.OVERRIDECOMMENT_ID;
    Template template = getCodeTemplate(templateName);
    if (template == null)
        //$NON-NLS-1$
        return "";
    //$NON-NLS-1$
    CodeTemplateContext context = new CodeTemplateContext(template.getContextTypeId(), null, "\n");
    //$NON-NLS-1$
    context.setVariable(CodeTemplateContextType.FILENAME, "Face.java");
    //$NON-NLS-1$
    context.setVariable(CodeTemplateContextType.PACKAGENAME, "test");
    //$NON-NLS-1$
    context.setVariable(CodeTemplateContextType.PROJECTNAME, "TestProject");
    //$NON-NLS-1$
    context.setVariable(CodeTemplateContextType.ENCLOSING_TYPE, "Face");
    //$NON-NLS-1$
    context.setVariable(CodeTemplateContextType.ENCLOSING_METHOD, "method");
    //$NON-NLS-1$
    context.setVariable(CodeTemplateContextType.RETURN_TYPE, "void");
    //$NON-NLS-1$
    context.setVariable(CodeTemplateContextType.SEE_TO_OVERRIDDEN_TAG, "test.IFace#foo()");
    return evaluateTemplate(template, context);
}
Also used : CodeTemplateContext(org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext) Template(org.eclipse.jface.text.templates.Template)

Example 19 with CodeTemplateContext

use of org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext in project che by eclipse.

the class StubUtility method getTypeBody.

/**
	 * Don't use this method directly, use CodeGeneration.
	 * 
	 * @param templateID the template id of the type body to get. Valid id's are
	 *            {@link CodeTemplateContextType#CLASSBODY_ID},
	 *            {@link CodeTemplateContextType#INTERFACEBODY_ID},
	 *            {@link CodeTemplateContextType#ENUMBODY_ID},
	 *            {@link CodeTemplateContextType#ANNOTATIONBODY_ID},
	 * @param cu the compilation unit to which the template is added
	 * @param typeName the type name
	 * @param lineDelim the line delimiter to use
	 * @return return the type body template or <code>null</code>
	 * @throws CoreException thrown if the template could not be evaluated
	 * @see org.eclipse.jdt.ui.CodeGeneration#getTypeBody(String, ICompilationUnit, String, String)
	 */
public static String getTypeBody(String templateID, ICompilationUnit cu, String typeName, String lineDelim) throws CoreException {
    if (!VALID_TYPE_BODY_TEMPLATES.contains(templateID)) {
        //$NON-NLS-1$
        throw new IllegalArgumentException("Invalid code template ID: " + templateID);
    }
    Template template = getCodeTemplate(templateID, cu.getJavaProject());
    if (template == null) {
        return null;
    }
    CodeTemplateContext context = new CodeTemplateContext(template.getContextTypeId(), cu.getJavaProject(), lineDelim);
    context.setCompilationUnitVariables(cu);
    context.setVariable(CodeTemplateContextType.TYPENAME, typeName);
    return evaluateTemplate(context, template);
}
Also used : CodeTemplateContext(org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext) Template(org.eclipse.jface.text.templates.Template)

Example 20 with CodeTemplateContext

use of org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext in project che by eclipse.

the class StubUtility method getSetterComment.

/*
	 * Don't use this method directly, use CodeGeneration.
	 * @see org.eclipse.jdt.ui.CodeGeneration#getSetterComment(ICompilationUnit, String, String, String, String, String, String, String)
	 */
public static String getSetterComment(ICompilationUnit cu, String typeName, String methodName, String fieldName, String fieldType, String paramName, String bareFieldName, String lineDelimiter) throws CoreException {
    String templateName = CodeTemplateContextType.SETTERCOMMENT_ID;
    Template template = getCodeTemplate(templateName, cu.getJavaProject());
    if (template == null) {
        return null;
    }
    CodeTemplateContext context = new CodeTemplateContext(template.getContextTypeId(), cu.getJavaProject(), lineDelimiter);
    context.setCompilationUnitVariables(cu);
    context.setVariable(CodeTemplateContextType.ENCLOSING_TYPE, typeName);
    context.setVariable(CodeTemplateContextType.ENCLOSING_METHOD, methodName);
    context.setVariable(CodeTemplateContextType.FIELD, fieldName);
    context.setVariable(CodeTemplateContextType.FIELD_TYPE, fieldType);
    context.setVariable(CodeTemplateContextType.BARE_FIELD_NAME, bareFieldName);
    context.setVariable(CodeTemplateContextType.PARAM, paramName);
    return evaluateTemplate(context, template);
}
Also used : CodeTemplateContext(org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext) Template(org.eclipse.jface.text.templates.Template)

Aggregations

CodeTemplateContext (org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext)28 Template (org.eclipse.jface.text.templates.Template)28 CoreException (org.eclipse.core.runtime.CoreException)6 BadLocationException (org.eclipse.jface.text.BadLocationException)6 Document (org.eclipse.jface.text.Document)6 IDocument (org.eclipse.jface.text.IDocument)6 TemplateBuffer (org.eclipse.jface.text.templates.TemplateBuffer)6 TemplateException (org.eclipse.jface.text.templates.TemplateException)6 TemplateVariable (org.eclipse.jface.text.templates.TemplateVariable)6 IJavaProject (org.eclipse.jdt.core.IJavaProject)4 ITypeParameter (org.eclipse.jdt.core.ITypeParameter)2 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)2 TypeParameter (org.eclipse.jdt.core.dom.TypeParameter)2