1. Java / Говнокод #11387

    +119

    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
    /**
     * Imbues the given {@link Font} with support for fallback fonts,
     * needed to display CJK characters in fonts that do not support them.
     * 
     * This is an ugly mess that depends on internal Sun APIs. Use sparingly!
     *
     * @param font the font
     * @return the composite font UI resource
     */
    public static FontUIResource getCompositeFontUIResource(final Font font) {
    	try {
    		Class<?> klass;
    		
    		try {
    			// Java 7
    			klass = Class.forName("sun.font.FontUtilities");
    		} catch (final ClassNotFoundException e) {
    			// Java 6
    			klass = Class.forName("sun.font.FontManager");
    		}
    		
    		// Invoke static method that wraps the font
    		val method = klass.getMethod("getCompositeFontUIResource", Font.class);
    		return (FontUIResource) method.invoke(null, font);
    	} catch (final ClassNotFoundException e) {
    		// Long block of catches that cannot happen on a Sun JRE
    		throw new AssertionError(e);
    	} catch (final IllegalAccessException e) {
    		throw new AssertionError(e);
    	} catch (final IllegalArgumentException e) {
    		throw new AssertionError(e);
    	} catch (final InvocationTargetException e) {
    		throw new AssertionError(e);
    	} catch (final NoSuchMethodException e) {
    		throw new AssertionError(e);
    	} catch (final SecurityException e) {
    		throw new AssertionError(e);
    	}
    }

    someone, 10 Июля 2012

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

    +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
    public abstract class SomeActivity extends Activity implements SomeEventListener {
    
        @Override
        protected void onResume() {
            super.onResume();
            application.addSomeListener(this);
        }
    
        @Override
        protected void onPause() {
            application.removeSomeListener(this);
            super.onPause();
        }

    В дополнение к #11379
    Вопрос пользователю нужно задать поверх любой нашей активити...

    rat4, 08 Июля 2012

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

    +69

    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
    public abstract class SomeActivity extends Activity implements SomeEventListener {
        private volatile Integer someEventAnswer;
    
        @Override
        public Integer onSomeEvent(final Collection<String> someData) {
            final CountDownLatch latch = new CountDownLatch(1);
    
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    showSomeDialog(someData, latch);
                }
            });
    
            try {
                latch.await();
            } catch (InterruptedException ignored) {
            }
    
            Integer ret = someEventAnswer;
            someEventAnswer = null;
            return ret;
        }
    
        private void showSomeDialog(final Collection<String> someData, final CountDownLatch latch) {
            String[] items = someData.toArray(new String[someData.size()]);
    
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Select some data");
            builder.setItems(items, new OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    someEventAnswer = which;
                    latch.countDown();
                }
            });
    
            Dialog dialog = builder.create();
            dialog.setOwnerActivity(this);
            dialog.show();
        }

    Android API Level 8
    Суть: из фонового потока задать вопрос пользователю

    rat4, 08 Июля 2012

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

    +82

    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
    protected string buildInsertString(ForecastTransactionsBean ft)
     {
        String sql = (new StringBuilder("insert into ")).append(getTableName()).append(" (").toString();
        sql = (new StringBuilder(string.valueOf(sql))).append("year,version_num,corp,acct_num,dept_num,cc,").toString();
        sql = (new StringBuilder(string.valueOf(sql))).append("epd_num,est_mar,est_apr,est_may,").toString();
        sql = (new StringBuilder(string.valueOf(sql))).append("est_jun,est_jul,est_aug,est_sep,est_oct,est_nov,").toString();
        sql = (new StringBuilder(string.valueOf(sql))).append("est_dec,est_jan,est_feb,inputby,batch_id,inputdate,").toString();
        sql = (new StringBuilder(string.valueOf(sql))).append("manualetc,forecastnotes,acct_class,resource_group").toString();
        sql = (new StringBuilder(string.valueOf(sql))).append(") values ('").toString();
        sql = (new StringBuilder(string.valueOf(sql))).append(ft.getYear().toString()).append("',").toString();
        sql = (new StringBuilder(string.valueOf(sql))).append(ft.getVersionNum().toString()).append(",'").toString();
        sql = (new StringBuilder(string.valueOf(sql))).append(ft.getCorp().toString()).append("','").toString();
        sql = (new StringBuilder(string.valueOf(sql))).append(ft.getAcctNum().toString()).append("','").toString();
        sql = (new StringBuilder(string.valueOf(sql))).append(ft.getDeptNum().toString()).append("','").toString();
        sql = (new StringBuilder(string.valueOf(sql))).append(ft.getCc().toString()).append("','").toString();
        sql = (new StringBuilder(string.valueOf(sql))).append(ft.getEpdNum().toString()).append("',").toString();
        sql = (new StringBuilder(string.valueOf(sql))).append(ft.getEstMar().toString()).append(",").toString();
        sql = (new StringBuilder(string.valueOf(sql))).append(ft.getEstApr().toString()).append(",").toString();
        sql = (new StringBuilder(string.valueOf(sql))).append(ft.getEstMay().toString()).append(",").toString();
        sql = (new StringBuilder(string.valueOf(sql))).append(ft.getEstJun().toString()).append(",").toString();
        sql = (new StringBuilder(string.valueOf(sql))).append(ft.getEstJul().toString()).append(",").toString();
        sql = (new StringBuilder(string.valueOf(sql))).append(ft.getEstAug().toString()).append(",").toString();
        sql = (new StringBuilder(string.valueOf(sql))).append(ft.getEstSep().toString()).append(",").toString();
        sql = (new StringBuilder(string.valueOf(sql))).append(ft.getEstOct().toString()).append(",").toString();
        sql = (new StringBuilder(string.valueOf(sql))).append(ft.getEstNov().toString()).append(",").toString();
        sql = (new StringBuilder(string.valueOf(sql))).append(ft.getEstDec().toString()).append(",").toString();
        sql = (new StringBuilder(string.valueOf(sql))).append(ft.getEstJan().toString()).append(",").toString();
        sql = (new StringBuilder(string.valueOf(sql))).append(ft.getEstFeb().toString()).append(",'").toString();
        sql = (new StringBuilder(string.valueOf(sql))).append(ft.getInputBy().toString()).append("','").toString();
        sql = (new StringBuilder(string.valueOf(sql))).append(ft.getBatchId().toString()).append("','").toString();
        sql = (new StringBuilder(string.valueOf(sql))).append(ft.getInputDate().toString()).append("',").toString();
        sql = (new StringBuilder(string.valueOf(sql))).append("0,'Clarity','','')").toString();
        return sql;
     }

    denis90, 05 Июля 2012

    Комментарии (12)
  5. Java / Говнокод #11364

    +118

    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
    private String compileOwnerName() {
    		return Joiner.on(' ').skipNulls().
    		String firstName = ownerFirstName.getValue();
    		String lastName = ownerLastName.getValue();
    		
    		final StringBuilder buffer = new StringBuilder();
    		boolean first = true;
    		
    		if (firstName != null) {
    			firstName = firstName.trim();
    			
    			if (!"".equals(firstName)) {
    				buffer.append(firstName);
    				first = false;
    			}
    		}
    
    		if (lastName != null) {
    			lastName = lastName.trim();
    			
    			if (!"".equals(lastName)) {
    				if (!first) {
    					buffer.append(" ");
    				}
    				
    				buffer.append(lastName);
    			}
    		}
    		
    		return buffer.toString();
    	}

    В итоге заменено однострочником на Perl:

    return Joiner.on(' ').skipNulls().join(Strings.emptyToNull(ownerFirstName.getValue().trim()),
    ownerLastName.getValue().trim()));

    someone, 05 Июля 2012

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

    +77

    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
    /**
         * Returns the an array (length 1) containing the checkbox menu item
         * label or null if the checkbox is not selected.
         * @see ItemSelectable
         */
        public synchronized Object[] getSelectedObjects() {
            if (state) {
                Object[] items = new Object[1];
                items[0] = label;
                return items;
            }
            return null;
        }

    java.awt.CheckboxMenuItem

    Lure Of Chaos, 03 Июля 2012

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

    +74

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    8. 8
    9. 9
    reader = new BufferedReader(new FileReader(file));
    //null means file end
    while ((tempString = reader.readLine()) != null) {
        if(tempString !=null && tempString.indexOf('=')>0){
            sheet.addCell(new Label(KEY_COLUMN,++ROW, tempString.substring(0,tempString.indexOf('='))));
            sheet.addCell(new Label(ENGLISH_COLUMN,ROW, tempString.substring(tempString.indexOf('=')+1)));
        }
    }
    reader.close();

    Вот так мы парсим файл *.properties в Java.

    roman-kashitsyn, 03 Июля 2012

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

    +71

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    8. 8
    protected T mutex;
    
    	public Mutex(final Class<?> clazz) {
    		if (this.mutex != null) {
    			this.mutex = null;
    		}
    		Runtime.getRuntime().addShutdownHook(this);
    	}

    ну а вдруг?

    Lure Of Chaos, 03 Июля 2012

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

    +71

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    @SuppressWarnings("deprecation")
    		static long startHourInMS() {
    			final Date date = new Date();
    			date.setMinutes(0);
    			date.setSeconds(0);
    			return date.getTime();
    		}

    Where the Time Stood Still...

    Lure Of Chaos, 03 Июля 2012

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

    +75

    1. 1
    2. 2
    3. 3
    4. 4
    status.setCounter(new Number(
     Number.nullToZero(
    status.getCounter()).add(
    value.movePointRight(2))));

    Действительно, кому нужна перегрузка операторов?

    Zozopy, 03 Июля 2012

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