---
title: "未初始化及其影响"
author: "ljl.java"
date: "2012-11-21T23:32:27Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2012-11-21T23:32:27Z"
id: 1732781
url: "https://ljl-java.iteye.com/blog/1732781"
---

如下代码中及其不合理的地方

public void execute(Environment env, Map params, TemplateModel\[] loopVars,  
   TemplateDirectiveBody body) throws TemplateException, IOException {  
    
   
  Pagination page=new Pagination();  
  CmsSite site = FrontUtils.getSite(env);

  String orgName=DirectiveUtils.getString(PARAM\_ORG\_NAME, params);  
  int areaId=site.getAreaId().getId();  
  

  List list=reportMng.getList(areaId);  
    
    
   if(null!=page.getList()||null!=list)  //从这开始未对page 或list 初始化 导致编译为通过后条件大括号一直控制到结尾影从而影响代码的维护性，也导致后面无需用page list对象的条件也放进来  
  {  
   page = reportMng.queryOrg\_InfoCount(site.getId(),site.getAreaId().getId(),orgName,1, 10);

    Map&lt;String, TemplateModel&gt; paramWrap = new HashMap&lt;String, TemplateModel&gt;(  
    params);  
    
  paramWrap.put(OUT\_BEAN, DEFAULT\_WRAPPER.wrap(site));  
  paramWrap.put(OUT\_PAGINATION, DEFAULT\_WRAPPER.wrap(page));  
  paramWrap.put(OUT\_LIST,DEFAULT\_WRAPPER.wrap(list));  
    
  Map&lt;String, TemplateModel&gt; origMap = DirectiveUtils  
    .addParamsToVariable(env, paramWrap);  
  InvokeType type = DirectiveUtils.getInvokeType(params);  
  String listStyle = DirectiveUtils.getString(PARAM\_STYLE\_LIST, params);  
  if (InvokeType.sysDefined == type) {  
   if (StringUtils.isBlank(listStyle)) {  
    throw new ParamsRequiredException(PARAM\_STYLE\_LIST);  
   }  
   env.include(TPL\_STYLE\_LIST + listStyle + TPL\_SUFFIX, UTF8, true);  
  } else if (InvokeType.userDefined == type) {  
   if (StringUtils.isBlank(listStyle)) {  
    throw new ParamsRequiredException(PARAM\_STYLE\_LIST);  
   }  
   FrontUtils.includeTpl(TPL\_STYLE\_LIST, site, env);  
  } else if (InvokeType.custom == type) {  
   FrontUtils.includeTpl(TPL\_NAME, site, params, env);  
  } else if (InvokeType.body == type) {  
   body.render(env.getOut());  
  } else {  
   throw new RuntimeException("invoke type not handled: " + type);  
  }  
  DirectiveUtils.removeParamsFromVariable(env, paramWrap, origMap);  
 }  
 }

 

现在贴上修改后代码：

public void execute(Environment env, Map params, TemplateModel\[] loopVars,  
   TemplateDirectiveBody body) throws TemplateException, IOException {  
  //对page, list对象先初始化 使其对象有个地址指向  
   List&lt;Org\_Info&gt; list = new ArrayList();  
  Pagination page=new Pagination();  
  CmsSite site = FrontUtils.getSite(env);  
  

  String orgName=DirectiveUtils.getString(PARAM\_ORG\_NAME, params);  
  int areaId=site.getAreaId().getId();  
    list=reportMng.getList(areaId);  
    
  //初始化后虽然没值 但有地址指向 至使 对象不为null 能够编译通过  
  page = reportMng.queryOrg\_InfoCount(site.getId(),site.getAreaId().getId(),orgName,1, 10);  
//  if(null!=page.getList()||null!=list)  
//  {  
//     
//  }  
  Map&lt;String, TemplateModel&gt; paramWrap = new HashMap&lt;String, TemplateModel&gt;(  
    params);  
    
  paramWrap.put(OUT\_BEAN, DEFAULT\_WRAPPER.wrap(site));  
  paramWrap.put(OUT\_PAGINATION, DEFAULT\_WRAPPER.wrap(page));  
  paramWrap.put(OUT\_LIST,DEFAULT\_WRAPPER.wrap(list));  
    
  Map&lt;String, TemplateModel&gt; origMap = DirectiveUtils  
    .addParamsToVariable(env, paramWrap);  
  InvokeType type = DirectiveUtils.getInvokeType(params);  
  String listStyle = DirectiveUtils.getString(PARAM\_STYLE\_LIST, params);  
  if (InvokeType.sysDefined == type) {  
   if (StringUtils.isBlank(listStyle)) {  
    throw new ParamsRequiredException(PARAM\_STYLE\_LIST);  
   }  
   env.include(TPL\_STYLE\_LIST + listStyle + TPL\_SUFFIX, UTF8, true);  
  } else if (InvokeType.userDefined == type) {  
   if (StringUtils.isBlank(listStyle)) {  
    throw new ParamsRequiredException(PARAM\_STYLE\_LIST);  
   }  
   FrontUtils.includeTpl(TPL\_STYLE\_LIST, site, env);  
  } else if (InvokeType.custom == type) {  
   FrontUtils.includeTpl(TPL\_NAME, site, params, env);  
  } else if (InvokeType.body == type) {  
   body.render(env.getOut());  
  } else {  
   throw new RuntimeException("invoke type not handled: " + type);  
  }  
  DirectiveUtils.removeParamsFromVariable(env, paramWrap, origMap);  
   
 }

 

可见对象未初始化对其后面的代码修改及逻辑影响有着决定性的影响，当涉及到代码整体修改时，先自问对其初始化否，进而再下结论。