I'm having an issue where the following loop is throwing an error Incompatible types: Required: Workstation, Found: java.lang.Object
.
With everything being typed, I can't understand why it's finding an Object instead of the typed object.
private static WorkflowProcess getWorkflowProcessByWorkstationRecursive(Collection<WorkflowProcess> workflowProcesses) {
for (WorkflowProcess workflowProcess : workflowProcesses) {
for (Workstation workstation : workflowProcess.getWorkstations()) //ERROR IS HERE
WorkflowProcess.java
abstract public class WorkflowProcess<WorkstationType extends Workstation> {
private ArrayList<WorkstationType> workstationList = new ArrayList<WorkstationType>();
public ArrayList<WorkstationType> getWorkstations() {
return workstationList;
}
ServerWorkflowProcess.java
abstract public class ServerWorkflowProcess extends WorkflowProcess<ServerWorkstation> {
ServerWorkstation.java
abstract public class ServerWorkstation extends Workstation<ServerWorkflowProcess> {
It gets a little complicated, but essentially the class hierarchy looks like this:
SpecificWorkflowProcess
extends TypeOfWorkflowProcess<TypeOfWorkstaton>
extends WorkflowProcess
SpecificWorkstation
extends TypeOfWorkstation<TypeOfWorkflowProcess>
extends Workstation
WorkflowProcesses
to Workstation
= One to many
The problem is that you're using the raw type WorkflowProcess
. That means that the API you get is effectively the erasure of the normal API, so the return type of getWorkstations
becomes just ArrayList
.
You can fix this easily as:
private static WorkflowProcess getWorkflowProcessByWorkstationRecursive(
Collection<WorkflowProcess<?>> workflowProcesses) {
for (WorkflowProcess<?> workflowProcess : workflowProcesses) {
..
}
}
By using the wildcarding, you're basically saying "I know that generics are involved here, but I don't actually mind what the WorkstationType
type parameter is in the collection".
You would have to add the generic parameter to the type of workflowProcess
in the second line:
private static WorkflowProcess getWorkflowProcessByWorkstationRecursive(Collection<WorkflowProcess> workflowProcesses) {
for (WorkflowProcess<Workstation> workflowProcess : workflowProcesses) {
for (Workstation workstation : workflowProcess.getWorkstations())
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.