001: /*
002: * Hammurapi
003: * Automated Java code review system.
004: * Copyright (C) 2004 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.org
021: * e-Mail: support@hammurapi.biz
022: */
023: package org.hammurapi.inspectors;
024:
025: import java.util.HashSet;
026: import java.util.Set;
027:
028: import org.hammurapi.InspectorBase;
029:
030: import com.pavelvlasov.jsel.JselException;
031: import com.pavelvlasov.jsel.LanguageElement;
032: import com.pavelvlasov.jsel.OperationInfo;
033: import com.pavelvlasov.jsel.expressions.MethodCall;
034: import com.pavelvlasov.jsel.expressions.NewObject;
035: import com.pavelvlasov.review.SourceMarker;
036: import com.pavelvlasov.util.Visitor;
037:
038: /**
039: * ER-085
040: * Avoid starting, stopping, or managing threads in any way
041: * @author Janos Czako
042: * @version $Revision: 1.3 $
043: */
044: public class ManageThreadsFromEjbRule extends InspectorBase {
045:
046: /**
047: * method which is violations
048: */
049: private Set allowedMethods = new HashSet();
050:
051: /**
052: * Type which's method must not be used.
053: */
054: private static final String VIOLATION_CLASS = "java.lang.Thread";
055:
056: /**
057: * Reviews the type definition if it is an Enterprise Bean and in that case
058: * if it violates against the rule.
059: *
060: * @param clazz the class object of the type definition
061: */
062: public void visit(final com.pavelvlasov.jsel.Class clazz) {
063: try {
064: if (clazz.isKindOf("javax.ejb.EnterpriseBean")) {
065: clazz.accept(new Visitor() {
066: public boolean visit(Object target) {
067: if (target instanceof MethodCall) {
068: try {
069: OperationInfo provider = ((MethodCall) target)
070: .getProvider();
071:
072: if (provider == null) {
073: context
074: .warn(
075: (SourceMarker) target,
076: "Provider is null for "
077: + target
078: + " at "
079: + ((LanguageElement) target)
080: .getLocation());
081: } else {
082: if (provider.getDeclaringType()
083: .isKindOf(VIOLATION_CLASS)
084: && !allowedMethods
085: .contains(provider
086: .getSignature())) {
087: context
088: .reportViolation((SourceMarker) target);
089: }
090: }
091: } catch (JselException e) {
092: context.warn((SourceMarker) target, e);
093: }
094: } else if (target instanceof NewObject) {
095: NewObject newObject = (NewObject) target;
096: try {
097: if (newObject.getTypeSpecification()
098: .getType().isKindOf(
099: VIOLATION_CLASS)) {
100: context
101: .reportViolation((SourceMarker) target);
102: }
103: } catch (JselException e) {
104: context.warn((SourceMarker) target, e);
105: }
106: }
107: return true;
108: }
109: });
110: }
111: } catch (JselException e) {
112: context.warn(clazz, e);
113: }
114: }
115:
116: // TODO Make this inspector parameterizable and accept allowed thread methods, e.g. getName()
117: }
|