Skip to content

Commit

Permalink
Fixed Incorrect number of arguments exception in case of advanced usa…
Browse files Browse the repository at this point in the history
…ge of generics with different number of parameters in interface and binded class. Added declared type to ExecutableContainer to get more flexibility.
  • Loading branch information
ashitikov committed Mar 13, 2017
1 parent 769d103 commit 17ec7bb
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 7 deletions.
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 17ec7bb

Please sign in to comment.