You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

FunctionEntity.java 4.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. MIT License
  3. Copyright (c) 2018-2019 Gang ZHANG
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. */
  20. package depends.entity;
  21. import depends.relations.IBindingResolver;
  22. import java.util.ArrayList;
  23. import java.util.Collection;
  24. import java.util.List;
  25. public class FunctionEntity extends ContainerEntity{
  26. private List<GenericName> returnTypeIdentifiers = new ArrayList<>();
  27. Collection<VarEntity> parameters;
  28. Collection<GenericName> throwTypesIdentifiers = new ArrayList<>();
  29. private Collection<Entity> returnTypes = new ArrayList<>();
  30. private Collection<Entity> throwTypes = new ArrayList<>();
  31. public FunctionEntity() {
  32. this.parameters = new ArrayList<>();
  33. }
  34. public FunctionEntity(GenericName simpleName, Entity parent, Integer id, GenericName returnType) {
  35. super(simpleName, parent,id);
  36. this.returnTypes = new ArrayList<>();
  37. returnTypeIdentifiers = new ArrayList<>();
  38. this.parameters = new ArrayList<>();
  39. throwTypesIdentifiers = new ArrayList<>();
  40. addReturnType(returnType);
  41. }
  42. public Collection<Entity> getReturnTypes() {
  43. return returnTypes;
  44. }
  45. @Override
  46. public TypeEntity getType() {
  47. if (returnTypes.size()>0){
  48. Object type = returnTypes.iterator().next();
  49. if (type instanceof TypeEntity)
  50. return (TypeEntity)type;
  51. }
  52. return null;
  53. }
  54. public void addReturnType(GenericName returnType) {
  55. if (returnType==null) return;
  56. this.returnTypeIdentifiers.add(returnType);
  57. }
  58. public void addReturnType(TypeEntity returnType) {
  59. if (returnType==null) return;
  60. if (!this.returnTypeIdentifiers.contains(returnType.rawName)){
  61. this.returnTypeIdentifiers.add(returnType.rawName);
  62. this.returnTypes.add(returnType);
  63. }
  64. }
  65. public void addThrowTypes(List<GenericName> throwedType) {
  66. throwTypesIdentifiers.addAll(throwedType);
  67. }
  68. @Override
  69. public void inferLocalLevelEntities(IBindingResolver bindingResolver) {
  70. for (VarEntity param:parameters) {
  71. param.fillCandidateTypes(bindingResolver);
  72. param.inferLocalLevelEntities(bindingResolver);
  73. }
  74. if (returnTypes.size()<returnTypeIdentifiers.size()) {
  75. returnTypes = identiferToEntities(bindingResolver,this.returnTypeIdentifiers);
  76. for ( GenericName returnTypeName: returnTypeIdentifiers) {
  77. Collection<Entity> typeEntities = typeParametersToEntities(bindingResolver, returnTypeName);
  78. this.appendTypeParameters(typeEntities);
  79. }
  80. }
  81. if (throwTypes.size()<throwTypesIdentifiers.size())
  82. throwTypes = identiferToEntities(bindingResolver,this.throwTypesIdentifiers);
  83. super.inferLocalLevelEntities(bindingResolver);
  84. }
  85. private Collection<Entity> typeParametersToEntities(IBindingResolver bindingResolver, GenericName name) {
  86. ArrayList<Entity> r = new ArrayList<>();
  87. for (GenericName typeParameter:name.getArguments()) {
  88. toEntityList(bindingResolver, r,typeParameter);
  89. }
  90. return r;
  91. }
  92. public Collection<VarEntity> getParameters() {
  93. return parameters;
  94. }
  95. public Collection<Entity> getThrowTypes() {
  96. return throwTypes;
  97. }
  98. @Override
  99. public Entity lookupVarInVisibleScope(GenericName varName) {
  100. for (VarEntity param:parameters) {
  101. if (varName.equals(param.getRawName())) {
  102. return param;
  103. }
  104. }
  105. return super.lookupVarInVisibleScope(varName);
  106. }
  107. public void addParameter(VarEntity var) {
  108. this.parameters.add(var);
  109. }
  110. @Override
  111. public String getDisplayName() {
  112. FileEntity f = (FileEntity) this.getAncestorOfType(FileEntity.class);
  113. return f.getRawName()+"("+this.getQualifiedName()+")";
  114. }
  115. @Override
  116. public VarEntity lookupVarLocally(GenericName varName) {
  117. for (VarEntity var:this.parameters) {
  118. if (var.getRawName().equals(varName))
  119. return var;
  120. }
  121. return super.lookupVarLocally(varName);
  122. }
  123. public void linkReturnToLastExpression() {
  124. if (expressionList()==null) return;
  125. for (int i = expressionList().size() - 1; i >= 0; i--) {
  126. Expression expr = expressionList().get(i);
  127. if (expr.isStatement())
  128. expr.addDeducedTypeFunction(this);
  129. }
  130. }
  131. }

人工智能研发终端

Contributors (2)