matthiasblaesing commented on code in PR #8253:
URL: https://github.com/apache/netbeans/pull/8253#discussion_r2019239706


##########
java/java.hints/src/org/netbeans/modules/java/hints/errors/MagicSurroundWithTryCatchFix.java:
##########
@@ -396,36 +417,65 @@ private static StatementTree 
createLogStatement(CompilationInfo info, TreeMaker
         boolean useFQN = false;
         for (ImportTree dovoz : info.getCompilationUnit().getImports()) {
             MemberSelectTree id = (MemberSelectTree) 
dovoz.getQualifiedIdentifier();
-            if ("Logger".equals(id.getIdentifier()) && 
!"java.util.logging.Logger".equals(id.toString())) {
+            if ("Logger".equals(id.getIdentifier()) && 
!loggerFQN.equals(id.toString())) {
                 useFQN = true;
             }
         }
+
+        // check if there's a declared logger to use
+        VariableElement existingLogger = null;
+        if (info instanceof CompilationController controller
+                && 
ErrorFixesFakeHint.isUseExistingLogger(ErrorFixesFakeHint.getPreferences(info.getFileObject(),
 FixKind.SURROUND_WITH_TRY_CATCH))) {
+            try {
+                controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
+                TreePath typePath = 
info.getTrees().getPath(info.getCompilationUnit(), containingTopLevel);
+                TypeElement typeElement = (TypeElement) 
info.getTrees().getElement(typePath);
+                for (VariableElement ve : 
ElementFilter.fieldsIn(typeElement.getEnclosedElements())) {
+                    TypeMirror type = ve.asType();
+                    // System.Logger.class.getName() is 
"java.lang.System$Logger", note '$',
+                    // and "contentEquals" fails, so HACK! use litteral class 
name.
+                    if (type.getKind() == TypeKind.DECLARED && 
((TypeElement)((DeclaredType)type).asElement()).getQualifiedName().contentEquals(loggerFQN))
 {
+                        existingLogger = ve;
+                    }
+                }
+            } catch(IOException ex) {
+            }
+        }
+
         // finally, make the invocation
-        ExpressionTree etExpression = make.MethodInvocation(
-                Collections.<ExpressionTree>emptyList(),
-                make.MemberSelect(
-                useFQN ? make.Identifier(logger.toString()) : 
make.QualIdent(logger),
-                "getLogger"),
-                Collections.<ExpressionTree>singletonList(arg));
-        ExpressionTree levelExpression = 
make.MemberSelect(make.QualIdent(level), "SEVERE");
-
-        return 
make.ExpressionStatement(make.MethodInvocation(Collections.<ExpressionTree>emptyList(),
 make.MemberSelect(etExpression, "log"), Arrays.asList(levelExpression, 
make.Literal(null), make.Identifier(name))));
-    }
+        ExpressionTree etExpression;
+        if (existingLogger != null) {
+            etExpression = make.Identifier(existingLogger);
+        } else {
+            etExpression = make.MethodInvocation(
+                    Collections.<ExpressionTree>emptyList(),
+                    make.MemberSelect(
+                        useFQN ? make.Identifier(loggerFactory.toString()) : 
make.QualIdent(loggerFactory),
+                        "getLogger"),
+                    Collections.<ExpressionTree>singletonList(arg));
+        }
+        ExpressionTree levelExpression = 
make.MemberSelect(make.QualIdent(level), levelName);
+        ExpressionTree nullMsgArg = isSystemLogger
+                ? 
(ExpressionTree)org.netbeans.modules.java.hints.spiimpl.Utilities.parseAndAttribute(info,
 "(String)null", null)
+                : make.Literal(null);
         
+        return 
make.ExpressionStatement(make.MethodInvocation(Collections.<ExpressionTree>emptyList(),
 make.MemberSelect(etExpression, "log"), Arrays.asList(levelExpression, 
nullMsgArg, make.Identifier(name))));
+    }
+    
     private static StatementTree 
createRethrowAsRuntimeExceptionStatement(WorkingCopy info, TreeMaker make, 
String name) {
         if 
(!ErrorFixesFakeHint.isRethrowAsRuntimeException(ErrorFixesFakeHint.getPreferences(info.getFileObject(),
 FixKind.SURROUND_WITH_TRY_CATCH))) {
             return null;
         }
-
+        
         TypeElement runtimeException = 
info.getElements().getTypeElement("java.lang.RuntimeException");
-
+        
         if (runtimeException == null) {
             return null;
         }
-
+        
         ExpressionTree exceptionName = make.QualIdent(runtimeException);
         StatementTree result = make.Throw(make.NewClass(null, 
Collections.<ExpressionTree>emptyList(), exceptionName, 
Arrays.asList(make.Identifier(name)), null));
-
+        

Review Comment:
   Nitpick: New whitespace introduced.



##########
java/java.editor/src/org/netbeans/modules/java/editor/codegen/LoggerGenerator.java:
##########
@@ -152,15 +164,66 @@ public void run(WorkingCopy copy) throws IOException {
         }
     }
 
+    public static String getBaseLoggerName() {
+        // // Undocumented feature/property subject to change/removal. Need a 
UI.
+        // String name = 
System.getProperty("LOGGER_GENERATOR_BASE_LOGGER_NAME");
+        // return name == null ? "LOG" : name;
+        return "LOG";
+    }

Review Comment:
   I can accept, that this akin to the tab-vs-space or "the right IDE" wars, 
but if we want to parametrize this, should this be project level configurable?
   
   And for the wording I would suggest:
   
   ```suggestion
       public static String getBaseLoggerName() {
           // Preparation for a potential future configurability of the loggers 
field name
           return "LOG";
       }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscr...@netbeans.apache.org
For additional commands, e-mail: notifications-h...@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to