Search in sources :

Example 1 with AddUnimplementedConstructorsOperation

use of org.eclipse.jdt.internal.corext.codemanipulation.AddUnimplementedConstructorsOperation in project bndtools by bndtools.

the class NewTypeWizardPage method createInheritedMethods.

/**
     * Creates the bodies of all unimplemented methods and constructors and adds them to the type. Method is typically
     * called by implementers of <code>NewTypeWizardPage</code> to add needed method and constructors.
     *
     * @param type
     *            the type for which the new methods and constructor are to be created
     * @param doConstructors
     *            if <code>true</code> unimplemented constructors are created
     * @param doUnimplementedMethods
     *            if <code>true</code> unimplemented methods are created
     * @param imports
     *            an import manager to add all needed import statements
     * @param monitor
     *            a progress monitor to report progress
     * @return the created methods.
     * @throws CoreException
     *             thrown when the creation fails.
     */
protected IMethod[] createInheritedMethods(IType type, boolean doConstructors, boolean doUnimplementedMethods, ImportsManager imports, IProgressMonitor monitor) throws CoreException {
    final ICompilationUnit cu = type.getCompilationUnit();
    JavaModelUtil.reconcile(cu);
    IMethod[] typeMethods = type.getMethods();
    Set<String> handleIds = new HashSet<String>(typeMethods.length);
    for (int index = 0; index < typeMethods.length; index++) handleIds.add(typeMethods[index].getHandleIdentifier());
    ArrayList<IMethod> newMethods = new ArrayList<IMethod>();
    CodeGenerationSettings settings = JavaPreferencesSettings.getCodeGenerationSettings(type.getJavaProject());
    settings.createComments = isAddComments();
    ASTParser parser = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
    parser.setResolveBindings(true);
    parser.setSource(cu);
    CompilationUnit unit = (CompilationUnit) parser.createAST(new SubProgressMonitor(monitor, 1));
    final ITypeBinding binding = ASTNodes.getTypeBinding(unit, type);
    if (binding != null) {
        if (doUnimplementedMethods) {
            AddUnimplementedMethodsOperation operation = new AddUnimplementedMethodsOperation(unit, binding, null, -1, false, true, false);
            operation.setCreateComments(isAddComments());
            operation.run(monitor);
            createImports(imports, operation.getCreatedImports());
        }
        if (doConstructors) {
            AddUnimplementedConstructorsOperation operation = new AddUnimplementedConstructorsOperation(unit, binding, null, -1, false, true, false);
            operation.setOmitSuper(true);
            operation.setCreateComments(isAddComments());
            operation.run(monitor);
            createImports(imports, operation.getCreatedImports());
        }
    }
    JavaModelUtil.reconcile(cu);
    typeMethods = type.getMethods();
    for (int index = 0; index < typeMethods.length; index++) if (!handleIds.contains(typeMethods[index].getHandleIdentifier()))
        newMethods.add(typeMethods[index]);
    IMethod[] methods = newMethods.toArray(new IMethod[0]);
    return methods;
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) AddUnimplementedMethodsOperation(org.eclipse.jdt.internal.corext.codemanipulation.AddUnimplementedMethodsOperation) CodeGenerationSettings(org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings) ArrayList(java.util.ArrayList) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) AddUnimplementedConstructorsOperation(org.eclipse.jdt.internal.corext.codemanipulation.AddUnimplementedConstructorsOperation) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) IMethod(org.eclipse.jdt.core.IMethod) ASTParser(org.eclipse.jdt.core.dom.ASTParser) HashSet(java.util.HashSet)

Aggregations

ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)1 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1 IMethod (org.eclipse.jdt.core.IMethod)1 ASTParser (org.eclipse.jdt.core.dom.ASTParser)1 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)1 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)1 AddUnimplementedConstructorsOperation (org.eclipse.jdt.internal.corext.codemanipulation.AddUnimplementedConstructorsOperation)1 AddUnimplementedMethodsOperation (org.eclipse.jdt.internal.corext.codemanipulation.AddUnimplementedMethodsOperation)1 CodeGenerationSettings (org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings)1