1. Лучший говнокод

    В номинации:
    За время:
  2. Java / Говнокод #16085

    +72

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    60. 60
    61. 61
    62. 62
    63. 63
    64. 64
    65. 65
    66. 66
    67. 67
    68. 68
    69. 69
    70. 70
    71. 71
    72. 72
    73. 73
    74. 74
    75. 75
    76. 76
    77. 77
    78. 78
    79. 79
    80. 80
    public class PLock {
        private Map<Thread, Integer> readLocks = new HashMap<Thread, Integer>();
        private Thread writeLock = null;
        private int writeLockCount = 0;
        
        public synchronized void getReadLock() {
            Thread currentThread = Thread.currentThread();
            long startTimeMillis = System.currentTimeMillis();
            boolean gotStuck = false;
            while (canClaimReadLock(currentThread) == false) {
                gotStuck = true;
                try {
                    wait();
                } catch (InterruptedException ex) {
                    Log.warn("Interrupted while attempting to get read lock.", ex);
                }
            }
            report(gotStuck, startTimeMillis, "read");
            if (readLocks.containsKey(currentThread)) {
                readLocks.put(currentThread, 1 + readLocks.get(currentThread));
            } else {
                readLocks.put(currentThread, 1);
            }
        }
        
        ...
        
        public synchronized void relinquishReadLock() {
            Thread currentThread = Thread.currentThread();
            if (readLocks.containsKey(currentThread) == false) {
                throw new RuntimeException("Cannot relinquish read lock on thread " + currentThread + " because it does not hold a lock.");
            }
            int newLockCount = readLocks.get(currentThread) - 1;
            if (newLockCount == 0) {
                readLocks.remove(currentThread);
                notifyAll();  // IMPORTANT: allow other threads to wake up and check if they can get locks now.
            } else {
                readLocks.put(currentThread, newLockCount);
            }
        }
        
        public synchronized void getWriteLock() {
            //Log.warn("getWriteLock() in thread " + Thread.currentThread());
            //dumpLocks();
            Thread currentThread = Thread.currentThread();
            long startTimeMillis = System.currentTimeMillis();
            boolean gotStuck = false;
            while (canClaimWriteLock(currentThread) == false) {
                gotStuck = true;
                try {
                    wait();
                } catch (InterruptedException ex) {
                    Log.warn("Interrupted while attempting to get write lock.", ex);
                }
            }
            report(gotStuck, startTimeMillis, "write");
            writeLock = currentThread;
            writeLockCount++;
        }
        
        ...
        
        public synchronized void relinquishWriteLock() {
            //Log.warn("relinquishWriteLock() in thread " + Thread.currentThread());
            Thread currentThread = Thread.currentThread();
            if (writeLock != currentThread) {
                throw new RuntimeException("Cannot relinquish write lock on thread " + currentThread + " because it does not hold the lock.");
            }
            if (writeLockCount <= 0) {
                throw new RuntimeException("Tried to relinquish write lock on thread " + currentThread + " while write lock count is " + writeLockCount);
            }
            writeLockCount--;
            if (writeLockCount == 0) {
                writeLock = null;
                notifyAll();  // IMPORTANT: allow other threads to wake up and check if they can get locks now.
            }
        }
        
        ...
    }

    А у вас уже есть свой теплый ламповый ReentrantReadWriteLock?
    Нет? Тогда https://github.com/orph/jujitsu/blob/master/src/e/ptextarea/PLock.java идет к вам...

    kostoprav, 30 Мая 2014

    Комментарии (20)
  3. Java / Говнокод #15977

    +72

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    8. 8
    @Test(timeout = 120000)
    public void testFlow() throws MuleException, InterruptedException {
    	MuleClient client = new MuleClient(muleContext);
    	MuleMessage message = client.send("vm://sfdc.process", new DefaultMuleMessage("abakadabra", muleContext));
    
    	Thread.sleep(30000);
    	Assert.assertNotNull(message);
    }

    Боремся с потусторонними силами с помощью священных sleep'ов.

    pingw33n, 13 Мая 2014

    Комментарии (8)
  4. Java / Говнокод #15851

    +72

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    if(error == CABtEngineErrors.K_ERR_BTENGINE_CANCEL) {
    	mObserver.btEngineEncryptError(CABtEngineErrors.K_ERR_BTENGINE_CANCEL);
    } else {
    	mObserver.btEngineEncryptError(error);
    }

    busylee, 26 Апреля 2014

    Комментарии (2)
  5. C++ / Говнокод #15778

    +72

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    auto val = map_.at(std::distance(map_.begin(),
    	std::min_element(map_.begin(), map_.end(), [](std::vector<int> a, std::vector<int> b)
    	{
    		return b.at(std::distance(b.begin(), std::min_element(b.begin(), b.end()))) > a.at(std::distance(a.begin(), std::min_element(a.begin(), a.end())));
    	})));
    
    return val.at(std::distance(val.begin(), std::min_element(val.begin(), val.end())));

    Разыменование итератора для слабаков!

    runewalsh, 18 Апреля 2014

    Комментарии (31)
  6. Java / Говнокод #15773

    +72

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    if (match) {
    	formCell(sheet, rowPlus2, 0, 0, fulBorderCalignFont10, 1, false);
    	formCell(sheet, rowPlus2, 8, 8, fulBorderCalignFont10, 2, false);
    	formCell(sheet, rowPlus2, 14, 14, fulBorderCalignFont10, 3, false);
    	formCell(sheet, rowPlus2, 20, 20, fulBorderCalignFont10, 4, false);
    	formCell(sheet, rowPlus2, 26, 26, fulBorderCalignFont10, 5, false);
    	formCell(sheet, rowPlus2, 32, 32, fulBorderCalignFont10, 6, false);
    	formCell(sheet, rowPlus2, 42, 42, fulBorderCalignFont10, 7, false);
    	formCell(sheet, rowPlus2, 48, 48, fulBorderCalignFont10, 8, false);
    	formCell(sheet, rowPlus2, 54, 54, fulBorderCalignFont10, 9, false);
    	formCell(sheet, rowPlus2, 60, 60, fulBorderCalignFont10, 10, false);
    	formCell(sheet, rowPlus2, 66, 66, fulBorderCalignFont10, 11, false);
    	formCell(sheet, rowPlus2, 72, 72, fulBorderCalignFont10, 12, false);
    } else {
    	formCell(sheet, rowPlus2, 0, 0, fulBorderCalignFont10, 1, false);
    	formCell(sheet, rowPlus2, 8, 8, fulBorderCalignFont10, 2, false);
    	formCell(sheet, rowPlus2, 14, 14, fulBorderCalignFont10, 3, false);
    	formCell(sheet, rowPlus2, 20, 20, fulBorderCalignFont10, 4, false);
    	formCell(sheet, rowPlus2, 26, 26, fulBorderCalignFont10, 5, false);
    	formCell(sheet, rowPlus2, 32, 32, fulBorderCalignFont10, 6, false);
    	formCell(sheet, rowPlus2, 38, 38, fulBorderCalignFont10, 7, false);
    	formCell(sheet, rowPlus2, 42, 42, fulBorderCalignFont10, 8, false);
    	formCell(sheet, rowPlus2, 48, 48, fulBorderCalignFont10, 9, false);
    	formCell(sheet, rowPlus2, 54, 54, fulBorderCalignFont10, 10, false);
    	formCell(sheet, rowPlus2, 60, 60, fulBorderCalignFont10, 11, false);
    	formCell(sheet, rowPlus2, 66, 66, fulBorderCalignFont10, 12, false);
    	formCell(sheet, rowPlus2, 72, 72, fulBorderCalignFont10, 13, false);
    	formCell(sheet, rowPlus2, 78, 78, fulBorderCalignFont10, 14, false);
    }

    evg_ever, 17 Апреля 2014

    Комментарии (2)
  7. Java / Говнокод #15485

    +72

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    public boolean fastItemEquals(ItemStack st, ItemStack nd) {
            if(nd == null) return false;
            if(st.hashCode() != nd.hashCode()) return false;
            if(st.getType() != nd.getType()) return false;
            if(!st.getItemMeta().getDisplayName().equals(nd.getItemMeta().getDisplayName())) return false;
            if(st.getEnchantments().size() != nd.getEnchantments().size()) return false;
            if(st.getItemMeta().getLore().size() != nd.getItemMeta().getLore().size()) return false;
            final List<String> 
                    lst = st.getItemMeta().getLore(),
                    lnd = nd.getItemMeta().getLore();
            for(int i = 0 ; i < st.getItemMeta().getLore().size() ; i++)
                if(!lst.get(i).equals(lnd.get(i))) return false;
            
            //return st.isSimilar(nd);
            return true;
        }
        
        public void fastItemRemove(Inventory inv, ItemStack st) {
            for(int i = 0 ; i < inv.getContents().length ; i++)
                if(fastItemEquals(st, inv.getContents()[i])) inv.clear(i); 
        }

    DiaLight, 15 Марта 2014

    Комментарии (9)
  8. Java / Говнокод #15484

    +72

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    public boolean fastItemEquals(ItemStack st, ItemStack nd) {
            if(st.hashCode() == nd.hashCode()) return true;
            if(st.getType() != nd.getType()) return false;
            if(!st.getItemMeta().getDisplayName().equals(nd.getItemMeta().getDisplayName())) return false;
            if(st.getEnchantments().size() != nd.getEnchantments().size()) return false;
            if(st.getItemMeta().getLore().size() != nd.getItemMeta().getLore().size()) return false;
            final List<String> 
                    lst = st.getItemMeta().getLore(),
                    lnd = nd.getItemMeta().getLore();
            for(int i = 0 ; i < st.getItemMeta().getLore().size() ; i++)
                if(!lst.get(i).equals(lnd.get(i))) return false;
            
            //return st.isSimilar(nd);
            return true;
        }
        
        /* оригинал
        @Override
        public boolean isSimilar(ItemStack stack) {
            if (stack == null) {
                return false;
            }
            if (stack == this) {
                return true;
            }
            if (!(stack instanceof CraftItemStack)) {
                return stack.getClass() == ItemStack.class && stack.isSimilar(this);
            }
    
            CraftItemStack that = (CraftItemStack) stack;
            if (handle == that.handle) {
                return true;
            }
            if (handle == null || that.handle == null) {
                return false;
            }
            if (!(that.getTypeId() == getTypeId() && getDurability() == that.getDurability())) {
                return false;
            }
            return hasItemMeta() ? that.hasItemMeta() && handle.tag.equals(that.handle.tag) : !that.hasItemMeta();
        }
        */

    DiaLight, 15 Марта 2014

    Комментарии (0)
  9. Java / Говнокод #15457

    +72

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    if (button.getSelection()) {
        combo.getCombo().setVisible(false);
        text.setVisible(true);
    } else {
        combo.getCombo().setVisible(true);
        text.setVisible(false);
    }

    evg_ever, 13 Марта 2014

    Комментарии (11)
  10. Java / Говнокод #14429

    +72

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    60. 60
    61. 61
    @Entity
    public class MyObject {
        @Column
        private int type;
    
        @Id
        private long id;
    
        @Column
        private String name;
    
        // и ещё другие поля, а также геттеры-сеттеры для них, в общем, обычная сущность
    }
    
    // managed bean в jsp 1.2 (legacy проект)
    public class MyList {
        private List<MyObject> oList;
        private SimpleDateFormat filterDateFormat;
    
        public MyList() {
            filterDateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
        }
    
        public List<MyObject> getMyList() {
            if (oList == null) {
                oList = DAO.getDAO().findAllMyObjects();
                String name = ...; // берётся из формы
                if (name != null && name != "") {
                    oList = getObjectsByName(name, oList);
                }
                Integer type = ...; // тоже берётся из формы
                if (type != null) {
                    oList = getObjectsByType(type, oList);
                }
                // и здесь ещё четыре куска такого же говнокода для других свойств MyObject
            }
            return oList;
        }
    
        private List<MyObject> getObjectsByType(Integer type, List<MyObject> oList) {
            List<MyObject> queriesByType = new ArrayList<MyObject>();
            for (MyObject o : oList) {
                if (o.getType() == type) {
                    queriesByType.add(o);
                }
            }
            return queriesByType;
        }
    
        private List<MyObject> getObjectsByName(String name, List<MyObject> oList) {
            List<MyObject> queriesByName = new ArrayList<MyObject>();
            for (MyObject o : oList) {
                if (o.getName() == name) {
                    queriesByName.add(o);
                }
            }
            return queriesByName;
        }
    
        // и ещё четыре таких же говнометода для других свойств MyObject
    }

    Наглядное руководство, как не надо работать с JPA

    evg_ever, 28 Января 2014

    Комментарии (31)
  11. Java / Говнокод #14228

    +72

    1. 1
    2. 2
    3. 3
    4. 4
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBEWITHMD5ANDTRIPLEDES");
    PBEKeySpec spec = new PBEKeySpec(password, salt, 1024, 128);
    SecretKey key = factory.generateSecret(spec);
    hexdump(key.getEncoded());

    http://ideone.com/bVElQG

    Не, ну я все понимаю, PKCS #5 1.5 аля PBE, MD5 и DES не считаются безопасными алгоритмами... но не настолько же...

    bormand, 14 Декабря 2013

    Комментарии (19)