
org.netbeans.contrib.yenta.Yenta Maven / Gradle / Ivy
Show all versions of api Show documentation
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 2012 Oracle and/or its affiliates. All rights reserved.
*
* Oracle and Java are registered trademarks of Oracle and/or its affiliates.
* Other names may be trademarks of their respective owners.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common
* Development and Distribution License("CDDL") (collectively, the
* "License"). You may not use this file except in compliance with the
* License. You can obtain a copy of the License at
* http://www.netbeans.org/cddl-gplv2.html
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
* specific language governing permissions and limitations under the
* License. When distributing the software, include this License Header
* Notice in each file and include the License file at
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the GPL Version 2 section of the License file that
* accompanied this code. If applicable, add the following below the
* License Header, with the fields enclosed by brackets [] replaced by
* your own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
*
* If you wish your version of this file to be governed by only the CDDL
* or only the GPL Version 2, indicate your decision by adding
* "[Contributor] elects to include this software in this distribution
* under the [CDDL or GPL Version 2] license." If you do not indicate a
* single choice of license, a recipient has the option to distribute
* your version of this file under either the CDDL, the GPL Version 2 or
* to extend the choice of license to its licensees as provided above.
* However, if you add GPL Version 2 code and therefore, elected the GPL
* Version 2 license, then the option applies only if the new code is
* made subject to such option by the copyright holder.
*
* Contributor(s):
*
* Portions Copyrighted 2012 Sun Microsystems, Inc.
*/
package org.netbeans.contrib.yenta;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import org.openide.modules.Dependency;
import org.openide.modules.ModuleInfo;
import org.openide.modules.ModuleInstall;
import org.openide.modules.Modules;
/**
* Fixes up module system dependencies.
* First add dependencies on whatever API modules you want to use, even if you are
* not (yet) their “friend”. Setting {@code warn } allows you
* to still compile against them and declare a regular specification version dependency,
* but be careful since this also disables checks about transitive dependency usage.
*
You can also add dependencies on modules with no exported packages; declare a
* specification version dependency and use the same flag to suppress checks.
* The same applies to modules exporting friend packages from which you also/instead
* want to use implementation packages. In either case be very
* careful and preferably catch {@link LinkageError} to be defensive.
*
Then add a module installer and extend this class rather than {@link ModuleInstall} directly.
* Override {@link #friends} and/or {@link #siblings}.
* When {@link #validate} is called, module system errors will be suppressed.
*/
public abstract class Yenta extends ModuleInstall {
/** Constructor for subclasses. */
protected Yenta() {}
/**
* Specifies the modules with whom you would like to be friends.
* These modules must be among your declared dependencies and they must export friend packages.
* For each such module, if you are not already a friend, you will be treated as one,
* so you will be able to access friend (but not private) packages.
* @return a set of module code name bases (default implementation is empty)
*/
protected Set friends() {
return Collections.emptySet();
}
/**
* Specifies the modules from whom you need complete access.
* These modules must be among your declared dependencies.
* For each such module, you will be able to access all packages, as with an implementation dependency.
* Be careful to defend against unexpected signature changes!
* @return a set of module code name bases (default implementation is empty)
*/
protected Set siblings() {
return Collections.emptySet();
}
/**
* @inheritDoc
* @throws IllegalStateException if {@link #friends} and {@link #siblings} are misconfigured or if the module system cannot be manipulated
*/
@Override public void validate() throws IllegalStateException {
Set friends = friends();
Set siblings = siblings();
if (friends.isEmpty() && siblings.isEmpty()) {
throw new IllegalStateException("Must specify some friends and/or siblings");
}
ModuleInfo me = Modules.getDefault().ownerOf(getClass());
if (me == null) {
throw new IllegalStateException("No apparent module owning " + getClass());
}
try {
Object manager = me.getClass().getMethod("getManager").invoke(me);
for (String m : friends) {
if (siblings.contains(m)) {
throw new IllegalStateException("Cannot specify the same module " + m + " in both friends and siblings");
}
Object data = data(findDependency(manager, m));
Field friendNamesF = Class.forName("org.netbeans.ModuleData", true, data.getClass().getClassLoader()).getDeclaredField("friendNames");
friendNamesF.setAccessible(true);
Set> names = (Set>) friendNamesF.get(data);
Set