Skip to content

Commit

Permalink
Merge pull request #28 from kejunxia/AllowGetBeanFromGraphDirectly
Browse files Browse the repository at this point in the history
Allow get bean from graph directly
  • Loading branch information
kejunxia committed Sep 17, 2015
2 parents a518fca + b84a5c2 commit 911765f
Show file tree
Hide file tree
Showing 23 changed files with 253 additions and 137 deletions.
5 changes: 5 additions & 0 deletions ChangeLog.md
@@ -1,3 +1,8 @@
Version: 1.2.0
* MvcGraph.inject throws runtime exception - MvcGraphException now. So no need to catch poke exceptions any more.
* MvcGraph.get method to get an instance. It provides cached instance if there is already an existing one, otherwise the newly created instance.
* Improve exception handling in library poke.

Version: 1.1.9
Fix bug
* When a recovering fragment in view pager its onViewReady is not called when its holding activity resume by popping out from another above activity.
Expand Down
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -33,13 +33,13 @@ The library is currently release to jCenter and MavenCentral
<dependency>
<groupId>com.shipdream</groupId>
<artifactId>android-mvc</artifactId>
<version>1.1.9</version>
<version>1.2.0</version>
</dependency>
```

**Gradle:**
```groovy
compile "com.shipdream:android-mvc:1.1.9"
compile "com.shipdream:android-mvc:1.2.0"
```

## Dependency injection with reference count
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Expand Up @@ -74,8 +74,8 @@ ext {
gitUrl = 'https://github.com/kejunxia/AndroidMvc.git' // Git repository URL
version = [
major: 1,
minor: 1,
patch : 9
minor: 2,
patch : 0
]
libGroup = 'com.shipdream'
libVersion = "${version.major}.${version.minor}.${version.patch}"
Expand Down
4 changes: 2 additions & 2 deletions documents/sites/Site-MarkDown.md
Expand Up @@ -40,13 +40,13 @@ The library is currently release to jCenter and MavenCentral
<dependency>
<groupId>com.shipdream</groupId>
<artifactId>android-mvc</artifactId>
<version>1.1.9</version>
<version>1.2.0</version>
</dependency>
```

**Gradle:**
```groovy
compile "com.shipdream:android-mvc:1.1.9"
compile "com.shipdream:android-mvc:1.2.0"
```

## Samples
Expand Down
Expand Up @@ -27,7 +27,7 @@
import com.shipdream.lib.poke.Graph;
import com.shipdream.lib.poke.ImplClassLocator;
import com.shipdream.lib.poke.ImplClassLocatorByPattern;
import com.shipdream.lib.poke.LocateClassException;
import com.shipdream.lib.poke.ImplClassNotFoundException;
import com.shipdream.lib.poke.Provider;
import com.shipdream.lib.poke.ProviderByClassType;
import com.shipdream.lib.poke.ProviderFinderByRegistry;
Expand Down Expand Up @@ -171,15 +171,45 @@ public void clearOnProviderFreedListeners() {
graph.clearOnProviderFreedListeners();
}

/**
* Get an instance matching the type and qualifier. If there is an instance cached, the cached
* instance will be returned otherwise a new instance will be created.
*
* <p>Note that, not like {@link #inject(Object)} (Object)} this method will <b>NOT</b> increment
* reference count for the injectable object with the same type and qualifier.</p>
* @param type the type of the object
* @param qualifier the qualifier of the injected object. Null is allowed if no qualifier is specified
* @return The cached object or a new instance matching the type and qualifier
* @throws MvcGraphException throw if exception occurs during getting the instance
*/
public <T> T get(Class<T> type, Annotation qualifier) {
try {
return graph.get(type, qualifier, Inject.class);
} catch (ProviderMissingException e) {
throw new MvcGraphException(e.getMessage(), e);
} catch (ProvideException e) {
throw new MvcGraphException(e.getMessage(), e);
} catch (CircularDependenciesException e) {
throw new MvcGraphException(e.getMessage(), e);
}
}

/**
* Inject all fields annotated by {@link Inject}. References of controllers will be
* incremented.
*
* @param target The target object whose fields annotated by {@link Inject} will be injected.
* @throws ProvideException
*/
public void inject(Object target) throws ProvideException, CircularDependenciesException, ProviderMissingException {
graph.inject(target, Inject.class);
public void inject(Object target) {
try {
graph.inject(target, Inject.class);
} catch (ProvideException e) {
throw new MvcGraphException(e.getMessage(), e);
} catch (ProviderMissingException e) {
throw new MvcGraphException(e.getMessage(), e);
} catch (CircularDependenciesException e) {
throw new MvcGraphException(e.getMessage(), e);
}
}

/**
Expand All @@ -190,7 +220,11 @@ public void inject(Object target) throws ProvideException, CircularDependenciesE
* @param target of which the object fields will be released.
*/
public void release(Object target) {
graph.release(target, Inject.class);
try {
graph.release(target, Inject.class);
} catch (ProviderMissingException e) {
throw new MvcGraphException(e.getMessage(), e);
}
}

/**
Expand Down Expand Up @@ -326,7 +360,7 @@ private DefaultProviderFinder(MvcGraph mvcGraph) {

@SuppressWarnings("unchecked")
@Override
public <T> Provider<T> findProvider(Class<T> type, Annotation qualifier) {
public <T> Provider<T> findProvider(Class<T> type, Annotation qualifier) throws ProviderMissingException {
Provider<T> provider = super.findProvider(type, qualifier);
if (provider == null) {
provider = providers.get(type);
Expand All @@ -336,8 +370,8 @@ public <T> Provider<T> findProvider(Class<T> type, Annotation qualifier) {
provider = new MvcProvider<>(mvcGraph.stateManagedObjects, type, impClass);
provider.setScopeCache(defaultImplClassLocator.getScopeCache());
providers.put(type, provider);
} catch (LocateClassException e) {
throw new RuntimeException(new ProviderMissingException(e.getMessage(), e));
} catch (ImplClassNotFoundException e) {
throw new ProviderMissingException(type, qualifier, e);
}
}
}
Expand Down
@@ -0,0 +1,11 @@
package com.shipdream.lib.android.mvc;

public class MvcGraphException extends RuntimeException {
public MvcGraphException(String message) {
super(message);
}

public MvcGraphException(String message, Throwable cause) {
super(message, cause);
}
}
Expand Up @@ -17,7 +17,7 @@
package com.shipdream.lib.android.mvc.event;

public class BaseEvent {
private Object sender;
private final Object sender;

public BaseEvent(Object sender) {
this.sender = sender;
Expand Down
Expand Up @@ -17,8 +17,8 @@
package com.shipdream.lib.android.mvc.event;

public class ValueChangeEventC2C<T> extends BaseEventC2C{
private T lastValue;
private T currentValue;
private final T lastValue;
private final T currentValue;

public ValueChangeEventC2C(Object sender, T lastValue, T currentValue) {
super(sender);
Expand Down
Expand Up @@ -17,8 +17,8 @@
package com.shipdream.lib.android.mvc.event;

public class ValueChangeEventC2V<T> extends BaseEventC2V{
private T lastValue;
private T currentValue;
private final T lastValue;
private final T currentValue;

public ValueChangeEventC2V(Object sender, T lastValue, T currentValue) {
super(sender);
Expand Down
Expand Up @@ -17,8 +17,8 @@
package com.shipdream.lib.android.mvc.event;

public class ValueChangeEventV2V<T> extends BaseEventV2V{
private T lastValue;
private T currentValue;
private final T lastValue;
private final T currentValue;

public ValueChangeEventV2V(Object sender, T lastValue, T currentValue) {
super(sender);
Expand Down
Expand Up @@ -21,7 +21,6 @@
import android.support.v4.app.DialogFragment;

import com.shipdream.lib.android.mvc.event.BaseEventV2V;
import com.shipdream.lib.poke.exception.PokeException;

/**
* This dialog fragment can either use {@link AlertDialog.Builder} to build a alert dialog or use
Expand All @@ -41,11 +40,8 @@ public void onCreate(Bundle savedInstanceState) {
setRetainInstance(true);
}

try {
AndroidMvc.graph().inject(this);
} catch (PokeException e) {
throw new RuntimeException(e);
}
AndroidMvc.graph().inject(this);

eventRegister = new EventRegister(this);
eventRegister.registerEventBuses();
}
Expand Down
Expand Up @@ -23,7 +23,6 @@
import android.view.ViewGroup;

import com.shipdream.lib.android.mvc.event.BaseEventV2V;
import com.shipdream.lib.poke.exception.PokeException;

import java.util.concurrent.CopyOnWriteArrayList;

Expand Down Expand Up @@ -108,12 +107,8 @@ protected int getCurrentOrientation() {

void injectDependencies() {
if (!dependenciesInjected) {
try {
AndroidMvc.graph().inject(this);
dependenciesInjected = true;
} catch (PokeException e) {
throw new RuntimeException(e);
}
AndroidMvc.graph().inject(this);
dependenciesInjected = true;
}
}

Expand Down
Expand Up @@ -19,7 +19,6 @@
import android.app.Service;

import com.shipdream.lib.android.mvc.event.BaseEventV2V;
import com.shipdream.lib.poke.exception.PokeException;

/**
* Android service can be thought as a kind of view sitting on top and driven by controller which
Expand All @@ -34,11 +33,9 @@ public abstract class MvcService extends Service{
@Override
public void onCreate() {
super.onCreate();
try {
AndroidMvc.graph().inject(this);
} catch (PokeException e) {
throw new RuntimeException(e);
}

AndroidMvc.graph().inject(this);

eventRegister = new EventRegister(this);
eventRegister.registerEventBuses();
}
Expand Down

0 comments on commit 911765f

Please sign in to comment.