Skip to content

Commit

Permalink
Merge pull request #28 from ashitikov/generics_advanced
Browse files Browse the repository at this point in the history
Generics advanced
  • Loading branch information
ashitikov authored Mar 13, 2017
2 parents 769d103 + e28660d commit 2b5eb3e
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 12 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,16 +192,18 @@ And add the dependency:
```
dependencies {
// Use compile for processor instead of apt if you haven't apt dependency.
apt 'com.github.trollsoftware.jcomposition:processor:1.1.1'
compile 'com.github.trollsoftware.jcomposition:api:1.1.1'
apt 'com.github.trollsoftware.jcomposition:processor:1.1.2'
compile 'com.github.trollsoftware.jcomposition:api:1.1.2'
}
```

## Ideas
1. Support of final class
2. Check how jcomposition works on java 8-9
3. Inherit java docs in generated files
4. Add more documentation and examples
2. Custom constructor support
3. Optional type casting in advanced generics
4. Check how jcomposition works on java 8-9
5. Inherit java docs in generated files
6. Add more documentation and examples

## License
This library is distributed under the Apache 2.0 license found in the [LICENSE](./LICENSE) file.
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ private MethodSpec getMethodSpec(Map.Entry<ExecutableElementContainer, List<Type
*/
TypeElementPairContainer container = overriders.get(0);

DeclaredType declaredType = container.getDeclaredType();
DeclaredType declaredType = entry.getKey().getDeclaredType();
MethodSpec.Builder builder = MethodSpecUtils.getBuilder(executableElement, declaredType, getProcessingEnv().getTypeUtils());

if (container.getRelationShip() == TypeElementPairContainer.ExecutableRelationShip.Overriding
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,20 @@

import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.Types;

import java.util.Objects;

public class ExecutableElementContainer {
private ExecutableElement executableElement;
private DeclaredType declaredType;
private ProcessingEnvironment env;

public ExecutableElementContainer(ExecutableElement executableElement, ProcessingEnvironment env) {
public ExecutableElementContainer(ExecutableElement executableElement, DeclaredType declaredType, ProcessingEnvironment env) {
this.executableElement = executableElement;
this.declaredType = declaredType;
this.env = env;
}

Expand All @@ -40,6 +43,14 @@ public void setExecutableElement(ExecutableElement executableElement) {
this.executableElement = executableElement;
}

public DeclaredType getDeclaredType() {
return declaredType;
}

public void setDeclaredType(DeclaredType declaredType) {
this.declaredType = declaredType;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ private static List<MethodSpec> getShareMethodSpecs(TypeElementPairContainer tCo
List<MethodSpec> result = new ArrayList<MethodSpec>();

ExecutableElement executableElement = entry.getKey().getExecutableElement();
// FIXME: Why 0?
DeclaredType declaredType = entry.getValue().get(0).getDeclaredType();
DeclaredType declaredType = entry.getKey().getDeclaredType();

MethodSpec.Builder builder = MethodSpec.overriding(executableElement, declaredType, env.getTypeUtils());
String statement = getShareExecutableStatement(executableElement, compositionName + ".this");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ private static void processInterfaceElements(List<ExecutableElement> elementsFro
ProcessingEnvironment env) {
DeclaredType baseDt = MoreTypes.asDeclared(baseIntf.asType());
for (ExecutableElement method : elementsFromInterface) {
ExecutableElementContainer container = new ExecutableElementContainer(method, env);
ExecutableElementContainer container = new ExecutableElementContainer(method, baseDt, env);
RelationShipResult result = findRelation(method, elementsFromBind, bind, false, env);

if (result.isDuplicateFound()) {
Expand All @@ -171,7 +171,7 @@ private static void processBindElements(List<ExecutableElement> elementsFromInte
DeclaredType concreteDt = Util.getDeclaredType(baseDt, concreteIntf, bind, env);

for (ExecutableElement method : elementsFromBind) {
ExecutableElementContainer container = new ExecutableElementContainer(method, env);
ExecutableElementContainer container = new ExecutableElementContainer(method, concreteDt, env);
RelationShipResult result = findRelation(method, elementsFromInterface, baseIntf, true, env);

if (!result.isDuplicateFound()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package jcomposition.processor.utils;

import com.google.auto.common.MoreElements;
import com.google.auto.common.MoreTypes;
import com.google.common.base.Predicate;

import javax.annotation.processing.ProcessingEnvironment;
Expand All @@ -26,6 +27,7 @@
import javax.lang.model.element.TypeParameterElement;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.TypeMirror;
import javax.tools.Diagnostic;

import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -78,6 +80,10 @@ public static DeclaredType getDeclaredType(DeclaredType baseDt, TypeElement intf
params[i] = env.getTypeUtils().asMemberOf(baseDt, parameterElement);
}

return env.getTypeUtils().getDeclaredType(bind, params);
try {
return env.getTypeUtils().getDeclaredType(bind, params);
} catch (IllegalArgumentException e) {
return MoreTypes.asDeclared(bind.asType());
}
}
}
20 changes: 20 additions & 0 deletions src/main/java/jcomposition/example/generics/Advanced.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright 2017 TrollSoftware ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package jcomposition.example.generics;

public class Advanced extends AdvancedBase {
}
20 changes: 20 additions & 0 deletions src/main/java/jcomposition/example/generics/AdvancedComponent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright 2017 TrollSoftware ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package jcomposition.example.generics;

public class AdvancedComponent extends AdvancedComponentBase {
}
25 changes: 25 additions & 0 deletions src/main/java/jcomposition/example/generics/IAdvanced.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2017 TrollSoftware ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package jcomposition.example.generics;

import jcomposition.api.annotations.Bind;
import jcomposition.api.annotations.Composition;

@Bind(Advanced.class)
@Composition(name = "AdvancedBase")
public interface IAdvanced extends IAdvancedComponent, IRouter {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2017 TrollSoftware ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package jcomposition.example.generics;

import jcomposition.api.annotations.Bind;
import jcomposition.api.annotations.Composition;
import jcomposition.example.generics.base.IViewBase;
import jcomposition.example.generics.base.ViewBase;

@Bind(AdvancedComponent.class)
@Composition(name = "AdvancedComponentBase")
public interface IAdvancedComponent<VB extends IViewBase> extends IView<VB> {
}

0 comments on commit 2b5eb3e

Please sign in to comment.