Browse Source

Improve public API of Extention* classes (#175)

pull/176/head
Sergey Tyurin 7 years ago committed by Decebal Suiu
parent
commit
5529c4dd08
  1. 9
      pf4j/src/main/java/org/pf4j/AbstractExtensionFinder.java
  2. 19
      pf4j/src/main/java/org/pf4j/ExtensionDescriptor.java
  3. 19
      pf4j/src/main/java/org/pf4j/ExtensionWrapper.java

9
pf4j/src/main/java/org/pf4j/AbstractExtensionFinder.java

@ -219,18 +219,13 @@ public abstract class AbstractExtensionFinder implements ExtensionFinder, Plugin
} }
private ExtensionWrapper createExtensionWrapper(Class<?> extensionClass) { private ExtensionWrapper createExtensionWrapper(Class<?> extensionClass) {
ExtensionDescriptor descriptor = new ExtensionDescriptor();
int ordinal = 0; int ordinal = 0;
if (extensionClass.isAnnotationPresent(Extension.class)) { if (extensionClass.isAnnotationPresent(Extension.class)) {
ordinal = extensionClass.getAnnotation(Extension.class).ordinal(); ordinal = extensionClass.getAnnotation(Extension.class).ordinal();
} }
descriptor.setOrdinal(ordinal); ExtensionDescriptor descriptor = new ExtensionDescriptor(ordinal, extensionClass);
descriptor.setExtensionClass(extensionClass);
ExtensionWrapper extensionWrapper = new ExtensionWrapper<>(descriptor); return new ExtensionWrapper<>(descriptor, pluginManager.getExtensionFactory());
extensionWrapper.setExtensionFactory(pluginManager.getExtensionFactory());
return extensionWrapper;
} }
private void checkDifferentClassLoaders(Class<?> type, Class<?> extensionClass) { private void checkDifferentClassLoaders(Class<?> type, Class<?> extensionClass) {

19
pf4j/src/main/java/org/pf4j/ExtensionDescriptor.java

@ -20,23 +20,12 @@ package org.pf4j;
*/ */
public class ExtensionDescriptor { public class ExtensionDescriptor {
private int ordinal; public final int ordinal;
private Class<?> extensionClass; public final Class<?> extensionClass;
public Class<?> getExtensionClass() { public ExtensionDescriptor(int ordinal, Class<?> extensionClass) {
return extensionClass;
}
public int getOrdinal() {
return ordinal;
}
void setExtensionClass(Class<?> extensionClass) {
this.extensionClass = extensionClass;
}
void setOrdinal(int ordinal) {
this.ordinal = ordinal; this.ordinal = ordinal;
this.extensionClass = extensionClass;
} }
} }

19
pf4j/src/main/java/org/pf4j/ExtensionWrapper.java

@ -22,18 +22,19 @@ package org.pf4j;
*/ */
public class ExtensionWrapper<T> implements Comparable<ExtensionWrapper<T>> { public class ExtensionWrapper<T> implements Comparable<ExtensionWrapper<T>> {
ExtensionDescriptor descriptor; private final ExtensionDescriptor descriptor;
ExtensionFactory extensionFactory; private final ExtensionFactory extensionFactory;
T extension; // cache private T extension; // cache
public ExtensionWrapper(ExtensionDescriptor descriptor) { public ExtensionWrapper(ExtensionDescriptor descriptor, ExtensionFactory extensionFactory) {
this.descriptor = descriptor; this.descriptor = descriptor;
} this.extensionFactory = extensionFactory;
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public T getExtension() { public T getExtension() {
if (extension == null) { if (extension == null) {
extension = (T) extensionFactory.create(descriptor.getExtensionClass()); extension = (T) extensionFactory.create(descriptor.extensionClass);
} }
return extension; return extension;
@ -44,7 +45,7 @@ public class ExtensionWrapper<T> implements Comparable<ExtensionWrapper<T>> {
} }
public int getOrdinal() { public int getOrdinal() {
return descriptor.getOrdinal(); return descriptor.ordinal;
} }
@Override @Override
@ -52,8 +53,4 @@ public class ExtensionWrapper<T> implements Comparable<ExtensionWrapper<T>> {
return (getOrdinal() - o.getOrdinal()); return (getOrdinal() - o.getOrdinal());
} }
void setExtensionFactory(ExtensionFactory extensionFactory) {
this.extensionFactory = extensionFactory;
}
} }

Loading…
Cancel
Save