-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,10 +36,17 @@ import javax.lang.model.element.Element | |
import javax.lang.model.element.Modifier | ||
import javax.lang.model.type.DeclaredType | ||
|
||
class ObjectsImplFactory(override val env: ProcessingEnvironment) : JavaxUtil { | ||
class ObjectsClassFactory(override val env: ProcessingEnvironment) : JavaxUtil { | ||
|
||
fun create(scopeType: DeclaredType): ObjectsClass? { | ||
val objectsType = scopeType.annotatedInnerType(Objects::class) ?: return null | ||
if (objectsType.hasFieldsRecursive()) { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
Leland-Takamine
Collaborator
|
||
throw ParsingError(objectsType.asElement(), "@Objects-annotated class may not declare any fields.") | ||
} | ||
if (objectsType.hasNonDefaultConstructor()) { | ||
throw ParsingError(objectsType.asElement(), "@Objects-annotated class may not declare any non-default constructors.") | ||
} | ||
|
||
val methods = objectsType.methods() | ||
.onEach { | ||
if (it.isVoid) throw ParsingError(it.element, "Factory method must not return void") | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ import javax.lang.model.element.ExecutableElement | |
import javax.lang.model.element.TypeElement | ||
import javax.lang.model.type.DeclaredType | ||
import javax.lang.model.type.ExecutableType | ||
import javax.lang.model.type.TypeKind | ||
import javax.lang.model.type.TypeMirror | ||
import javax.lang.model.util.ElementFilter | ||
import kotlin.reflect.KClass | ||
|
@@ -73,6 +74,31 @@ interface JavaxUtil { | |
return ElementFilter.constructorsIn(asElement().enclosedElements) | ||
} | ||
|
||
fun DeclaredType.hasFieldsRecursive(): Boolean { | ||
val typeElement: TypeElement = asElement() as TypeElement | ||
if (ElementFilter.fieldsIn(typeElement.enclosedElements).isNotEmpty()) { | ||
return true | ||
} | ||
|
||
val superclass: TypeMirror = typeElement.superclass | ||
if (superclass.kind == TypeKind.DECLARED) { | ||
if ((superclass as DeclaredType).hasFieldsRecursive()) { | ||
return true | ||
} | ||
} else if (superclass.kind != TypeKind.NONE) { | ||
// TODO Is it possible for TypeElement.superclass to return a TypeMirror of TypeKind other than | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
Leland-Takamine
Collaborator
|
||
// DECLARED or NONE? If so, then we should not throw an Exception here and instead handle the | ||
// recursion properly. | ||
throw IllegalStateException("Unknown superclass type.") | ||
} | ||
|
||
return false | ||
} | ||
|
||
fun DeclaredType.hasNonDefaultConstructor(): Boolean { | ||
return constructors().find { !it.parameters.isEmpty() } != null | ||
} | ||
|
||
fun DeclaredType.packageName(): String { | ||
return MoreElements.getPackage(asElement()).qualifiedName.toString() | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright (c) 2018 Uber Technologies, Inc. | ||
* | ||
* 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 testcases.E019_objects_fields; | ||
|
||
@motif.Scope | ||
public interface Scope { | ||
|
||
@motif.Objects | ||
class Objects { | ||
|
||
private final String s = "s"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright (c) 2018 Uber Technologies, Inc. | ||
* | ||
* 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 testcases.E019_objects_fields; | ||
|
||
import motif.compiler.errors.parsing.ParsingError; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
public class Test { | ||
|
||
public static ParsingError parsingError; | ||
|
||
public static void run() { | ||
assertThat(parsingError).isNotNull(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright (c) 2018 Uber Technologies, Inc. | ||
* | ||
* 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 testcases.E020_objects_constructor; | ||
|
||
@motif.Scope | ||
public interface Scope { | ||
|
||
@motif.Objects | ||
class Objects { | ||
|
||
Objects(String s) {} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright (c) 2018 Uber Technologies, Inc. | ||
* | ||
* 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 testcases.E020_objects_constructor; | ||
|
||
import motif.compiler.errors.parsing.ParsingError; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
public class Test { | ||
|
||
public static ParsingError parsingError; | ||
|
||
public static void run() { | ||
assertThat(parsingError).isNotNull(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright (c) 2018 Uber Technologies, Inc. | ||
* | ||
* 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 testcases.E021_nullable_factory_method_return; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
@motif.Scope | ||
public interface Scope { | ||
|
||
@motif.Objects | ||
class Objects { | ||
|
||
@Nullable | ||
String string() { | ||
return "s"; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright (c) 2018 Uber Technologies, Inc. | ||
* | ||
* 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 testcases.E021_nullable_factory_method_return; | ||
|
||
import motif.compiler.errors.parsing.ParsingError; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
public class Test { | ||
|
||
public static ParsingError parsingError; | ||
|
||
public static void run() { | ||
assertThat(parsingError).isNotNull(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright (c) 2018 Uber Technologies, Inc. | ||
* | ||
* 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 testcases.E022_nullable_factory_method_param; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
@motif.Scope | ||
public interface Scope { | ||
|
||
@motif.Objects | ||
class Objects { | ||
|
||
Integer string(@Nullable String s) { | ||
return 1; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright (c) 2018 Uber Technologies, Inc. | ||
* | ||
* 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 testcases.E022_nullable_factory_method_param; | ||
|
||
import motif.compiler.errors.parsing.ParsingError; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
public class Test { | ||
|
||
public static ParsingError parsingError; | ||
|
||
public static void run() { | ||
assertThat(parsingError).isNotNull(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright (c) 2018 Uber Technologies, Inc. | ||
* | ||
* 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 testcases.E023_objects_fields_inherited; | ||
|
||
public class ObjectsParent { | ||
|
||
private final String s = "s"; | ||
|
||
public String s() { | ||
return s; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Copyright (c) 2018 Uber Technologies, Inc. | ||
* | ||
* 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 testcases.E023_objects_fields_inherited; | ||
|
||
@motif.Scope | ||
public interface Scope { | ||
|
||
@motif.Objects | ||
class Objects extends ObjectsParent {} | ||
} |
how about static fields?