void changeTypeContext(ResolutionContext old, ResolutionContext new_, MethodDeclaration m) { m.setType(changeTypeContext(old, new_, m.getType())); m.setBody( new BlockStmt( Collections.singletonList( new ThrowStmt( new ObjectCreationExpr( null, new ClassOrInterfaceType("UnsupportedOperationException"), Collections.emptyList()))))); NodeUtil.forChildren( m, node -> { Expression scope = node.getScope(); // TODO: Currently guesses that it's a type name if first character is uppercase. // Should check for fields/variables which match instead if (scope instanceof NameExpr && Character.isUpperCase(((NameExpr) scope).getName().charAt(0))) { String name = ((NameExpr) scope).getName(); node.setScope(ASTHelper.createNameExpr(new_.typeToString(old.resolve(name)))); } }, MethodCallExpr.class); NodeUtil.forChildren( m, node -> node.setType(changeTypeContext(old, new_, node.getType())), VariableDeclarationExpr.class); NodeUtil.forChildren( m, node -> node.setType(changeTypeContext(old, new_, node.getType())), TypeExpr.class); NodeUtil.forChildren( m, node -> node.setType(changeTypeContext(old, new_, node.getType())), com.github.javaparser.ast.body.Parameter.class); }
@Override public void setTypeVariables(List<TypeVariable> typeVariables) { declaration.setTypeParameters( typeVariables .stream() .map(getContext()::unresolveTypeVariable) .collect(Collectors.toList())); }
@Override public List<TypeVariable> getTypeVariables() { return declaration .getTypeParameters() .stream() .map(getContext()::resolveTypeVariable) .collect(Collectors.toList()); }
@Override public void add(MethodInfo method) { MethodDeclaration methodDeclaration; if (method instanceof MethodDeclarationWrapper) { val wrapper = (MethodDeclarationWrapper) method; methodDeclaration = (MethodDeclaration) wrapper.declaration.clone(); methodDeclaration.setAnnotations(Collections.emptyList()); wrapper .getClassInfo() .changeTypeContext(wrapper.getContext(), getContext(), methodDeclaration); } else { methodDeclaration = new MethodDeclaration(); new MethodDeclarationWrapper(methodDeclaration).setAll(method); } addMember(methodDeclaration); }
@Override public List<Parameter> getParameters() { return declaration .getParameters() .stream() .map( (parameter) -> new Parameter( getContext().resolve(parameter.getType()), parameter.getId().getName())) .collect(Collectors.toList()); }
@Override public List<Annotation> getAnnotations() { return SourceInfo.this.getAnnotationsInternal(declaration.getAnnotations()); }
@Override public void setAccessFlags(AccessFlags accessFlags) { declaration.setModifiers(accessFlags.access); }
@Override public AccessFlags getAccessFlags() { return new AccessFlags(declaration.getModifiers()); }
@Override public void setName(String name) { declaration.setName(name); }
@Override public String getName() { return declaration.getName(); }
@Override public void setReturnType(Type type) { declaration.setType(setType(type, declaration.getType())); }
@Override public Type getReturnType() { return getContext().resolve(declaration.getType()); }