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


##########
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:
   That's weird. Code I didn't modify. Wonder why NB decided to do that. I 
notice that there are scattered places around the code where there are blank 
lines with spaces in them.
   
   Fixing.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

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

Reply via email to