1 /*******************************************************************************
2  * Copyright (c) 2000, 2017 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.jdt.internal.corext.refactoring.binary;
15 
16 import java.net.URI;
17 import java.util.List;
18 
19 import org.eclipse.core.filesystem.IFileStore;
20 
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.IProgressMonitor;
23 import org.eclipse.core.runtime.SubProgressMonitor;
24 
25 import org.eclipse.jdt.core.IClassFile;
26 import org.eclipse.jdt.core.IOrdinaryClassFile;
27 import org.eclipse.jdt.core.IPackageFragment;
28 import org.eclipse.jdt.core.IType;
29 
30 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
31 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
32 
33 /**
34  * Operation, which run, creates structurally equivalent stub types for a list
35  * of binary package fragments.
36  *
37  * @since 3.2
38  */
39 public class StubCreationOperation extends AbstractCodeCreationOperation {
40 
41 	/** Should stubs for private member be generated as well? */
42 	protected final boolean fStubInvisible;
43 
44 	/**
45 	 * Creates a new stub creation operation.
46 	 *
47 	 * @param uri
48 	 *            the URI where to output the stubs
49 	 * @param packages
50 	 *            the list of packages to create stubs for
51 	 */
StubCreationOperation(final URI uri, final List<IPackageFragment> packages)52 	public StubCreationOperation(final URI uri, final List<IPackageFragment> packages) {
53 		this(uri, packages, false);
54 	}
55 
56 	/**
57 	 * Creates a new stub creation operation.
58 	 *
59 	 * @param uri
60 	 *            the URI where to output the stubs
61 	 * @param packages
62 	 *            the list of packages to create stubs for
63 	 * @param stub
64 	 *            <code>true</code> to generate stubs for private and package
65 	 *            visible members as well, <code>false</code> otherwise
66 	 */
StubCreationOperation(final URI uri, final List<IPackageFragment> packages, final boolean stub)67 	public StubCreationOperation(final URI uri, final List<IPackageFragment> packages, final boolean stub) {
68 		super(uri, packages);
69 		fStubInvisible= stub;
70 	}
71 
72 	@Override
getOperationLabel()73 	protected String getOperationLabel() {
74 		return RefactoringCoreMessages.StubCreationOperation_creating_type_stubs;
75 	}
76 
77 	/**
78 	 * Runs the stub generation on the specified class file.
79 	 *
80 	 * @param file
81 	 *            the class file
82 	 * @param parent
83 	 *            the parent store
84 	 * @param monitor
85 	 *            the progress monitor to use
86 	 * @throws CoreException
87 	 *             if an error occurs
88 	 */
89 	@Override
run(final IClassFile file, final IFileStore parent, final IProgressMonitor monitor)90 	protected void run(final IClassFile file, final IFileStore parent, final IProgressMonitor monitor) throws CoreException {
91 		try {
92 			monitor.beginTask(RefactoringCoreMessages.StubCreationOperation_creating_type_stubs, 2);
93 			SubProgressMonitor subProgressMonitor= new SubProgressMonitor(monitor, 1);
94 			if (file instanceof IOrdinaryClassFile) {
95 				final IType type= ((IOrdinaryClassFile) file).getType();
96 				if (type.isAnonymous() || type.isLocal() || type.isMember())
97 					return;
98 				String source= new StubCreator(fStubInvisible).createStub(type, subProgressMonitor);
99 				createCompilationUnit(parent, type.getElementName() + JavaModelUtil.DEFAULT_CU_SUFFIX, source, monitor);
100 			}
101 		} finally {
102 			monitor.done();
103 		}
104 	}
105 }
106